473,382 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

why the objects in the list giving a different address

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<list>
  3. using namespace std;
  4. class a
  5. {
  6. private:
  7.     int x;
  8. public:
  9.     void setx(int xx)
  10.     {
  11.         x=xx;
  12.     }
  13.     void showx()
  14.     {
  15.         cout<<" x is "<<x;
  16.     }
  17.     void display(){
  18.         cout<<"i am in display"<<endl;
  19.         cout<<" x is "<<x <<endl;
  20.     }
  21.     void display2(){
  22.         cout<<"i am in  display2"<<endl;
  23.     }
  24.     ~a()
  25.     {
  26.         cout<<"destructor of a"<<endl;
  27.     }
  28. };
  29.  
  30. list<a> lst;
  31.     int main()
  32.     {
  33.  
  34.  
  35.         a *a1;  
  36.  
  37.         a1=new a();
  38.         a1->setx (3);
  39.  
  40.         cout<<"Address1...."<<&(*a1)<<endl;
  41.         cout<<"Address1.1..."<<a1<<endl;
  42.  
  43.         lst.push_back(*a1);
  44.  
  45.         list<a>::iterator p=lst.begin();
  46.  
  47.          cout<<"Address2......"<<&(*p)<<endl;
  48.         p->display();
  49.  
  50.         cout<<lst.size()<<endl;
  51.  
  52.         return 0;
  53.     }
  54.  
Mar 13 '07 #1
1 1608
sicarie
4,677 Expert Mod 4TB
The objects in the list are giving different addresses because they are all stored at different locations - the list is what has the references to tie them together. If you used a vector, you would find that they were all contiguous in memory (if you returned many of them, there would be a pattern as the containers of the individual objects in the list are all of the same size - so there is the same amount of memory between them...). I don't believe (please anyone correct me if I'm wrong!) Lists need to have the contiguous memory space (though they can...). But each element in the list, vector, or STL container will have a different memory location because each element takes up space, and the list is ready to grow/shrink as you want it to, it just references those places the containers are at.
Mar 13 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Bryan J Gudorf | last post by:
PDO, an open source python module for interfacing with RDBMS (SQL databases), has now reached 1.2.0! PDO provides an object oriented API, similar to that of ADO or JDBC, to python developers. PDO...
14
by: Dennis | last post by:
Hi I have a little problem as stated above (haven't found any solution on the www). I have a list of objects (particles) where I have a member of a template vector (array1d from TNT). I...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
1
by: Glen Able | last post by:
Hi, I have a collection of lists, something like this: std::list<Obj*> lists; Now I add an Obj to one of the lists: Obj* gary; lists.push_front(gary);
10
by: Jax | last post by:
I dont seem to fully comprehend references to objects yet. Lets say for example I do this; Customer c = new Customer(); Customer c1 = c; I understand that if I change c1, I also change c as...
1
by: Garris, Nicole | last post by:
We've actually implemented this kind of thing in a different DBMS. The physical design consists of a single "organization" table that's something like: Org_key (primary key) Org_type (group,...
3
by: orkonoid | last post by:
Hello, I am having trouble with a Polymorphism issue using container classes. I have a longwinded and shortwinded version of my question: Shortwinded version: How can I store base class...
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
2
by: BobLewiston | last post by:
The concept of delegates (references to methods) per se is new to me (although I used function pointers in C years ago), and although the literature acknowledges that there are significant...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.