Question about vector and list

J

John

Hi all:

I write the following code about vector and list. Why the result is
not correct?

Thanks.

John

--------------------------------------------------------------
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

typedef struct{
int ID;
double time;
}node;

void myvector(vector<node*> &v0){
node n1, n2;
n1.ID = 1;
n1.time = 1.1;
n2.ID = 2;
n2.time = 2.2;

v0.push_back(&n1);
v0.push_back(&n2);
cout<<"v0:"<<v0[0]<<endl;
cout<<"ID1:"<< (*v0.begin())->ID <<" time1:"<<v0[0]->time <<endl;
cout<<"ID2:"<< v0[1]->ID <<" time2:"<<v0[1]->time <<endl;
}

list< node* > mylist()
{
list< node* > l;

node n1, n2;
n1.ID = 5;
n1.time = 5.1;
n2.ID = 6;
n2.time = 6.2;
l.push_back(&n1);
l.push_back(&n2);
list<node*>::iterator pos;
pos = l.begin();
cout<<"pos ID1:"<<(*pos)->ID<<" time1:"<<(*pos)->time<<endl;
pos++;
cout<<"pos ID2:"<<(*pos)->ID<<" time2:"<<(*pos)->time<<endl;
return l;
}

int main()
{
vector<node*> v1;
myvector(v1);

if(!v1.empty()){
cout<<"v1:"<<v1[0]<<endl;
cout<<"ID1:"<< (v1.front())->ID <<" time1:"<<v1[0]->time <<endl;
}
if(!v1.empty()){
cout<<"v1_1:"<<endl;
cout<<"ID2:"<< v1[1]->ID <<" time2:"<<v1[1]->time <<endl;
}

list< node* > l1;
l1 = mylist();
cout<<"l1:"<<endl;
list<node*>::iterator pos;
pos = l1.begin();
cout<<"pos ID1:"<<(*pos)->ID<<" time1:"<<(*pos)->time<<endl;
pos++;
cout<<"pos ID2:"<<(*pos)->ID<<" time2:"<<(*pos)->time<<endl;
}

------------------------------
The output is:

v0:0xbffffc3c
ID1:1 time1:1.1
ID2:2 time2:2.2
v1:0xbffffc3c
ID1:1074040948 time1:4.87143e-270
v1_1:
ID2:134518728 time2:6.64672e-316
pos ID1:5 time1:5.1
pos ID2:6 time2:6.2
l1:
pos ID1:134531200 time1:4.87361e-270
pos ID2:32 time2:6.40993

The result is correct in function myvector() and mylist(). But after
the arguements are past back to main(), the result is wrong. What is
the problem?

Thanks a lot.

John
 
C

Cy Edmunds

John said:
Hi all:

I write the following code about vector and list. Why the result is
not correct?

Thanks.

John

--------------------------------------------------------------
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

typedef struct{
int ID;
double time;
}node;

void myvector(vector<node*> &v0){
node n1, n2;
n1.ID = 1;
n1.time = 1.1;
n2.ID = 2;
n2.time = 2.2;

v0.push_back(&n1);
v0.push_back(&n2);
cout<<"v0:"<<v0[0]<<endl;
cout<<"ID1:"<< (*v0.begin())->ID <<" time1:"<<v0[0]->time <<endl;
cout<<"ID2:"<< v0[1]->ID <<" time2:"<<v0[1]->time <<endl;
}

list< node* > mylist()
{
list< node* > l;

node n1, n2;
n1.ID = 5;
n1.time = 5.1;
n2.ID = 6;
n2.time = 6.2;
l.push_back(&n1);

By taking the address of a local variable you are setting yourself up for
trouble. When the function ends the local variables are destroyed and the
pointers are pointing at nothing.

[snip]

Let's try again:

// using namespace std; -- why pollute the global namespace?

class node // let's do this in C++
{
private:
int m_ID;
double m_time;
public:
node(int i_ID, double i_time) : m_ID(i_ID), m_time(i_time) {}
int ID() const {return m_ID;}
double time() const {return m_time;}
};

std::list<node> mylist() // not std::list<node*>
{
std::list<node> answer;
answer.push_back(node(5, 5.1));
answer.push_back(node(6, 6.2));
return answer;
}


Warning: untested code.
 
D

David Harmon

On 31 May 2004 20:21:58 -0700 in comp.lang.c++, (e-mail address removed)
(John) wrote,
The result is correct in function myvector() and mylist(). But after
the arguements are past back to main(), the result is wrong. What is
the problem?

You are storing (node *) pointers to local variables in functions
myvector() and mylist(). After the function returns, those local
variables no longer exist and the former pointers to them are unusable.
 
J

John

Thanks. I understand now.
I will use vector or list to bring pointers from one function to
another. So I write a simple code to learn it.
I change the code as below:

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

class node{//simple class, just for test purpose.:)
int ID;
double time;
};

void myvector(std::vector<node*> &v0){
node *n1 = new node;
node *n2 = new node;

n1->ID = 1;
n1->time = 1.1;
n2->ID = 2;
n2->time = 2.2;

v0.push_back(n1);
v0.push_back(n2);
cout<<"v0:"<<v0[0]<<endl;
cout<<"ID1:"<< (*v0.begin())->ID <<" time1:"<<v0[0]->time <<endl;
cout<<"ID2:"<< v0[1]->ID <<" time2:"<<v0[1]->time <<endl;
}

int main()
{
std::vector<node*> v1;
myvector(v1);

node *n3, *n4;

cout<<"v1:"<<v1[0]<<endl;
cout<<"ID1:"<< (v1.front())->ID <<" time1:"<<v1[0]->time <<endl;
cout<<"ID2:"<< v1[1]->ID <<" time2:"<<v1[1]->time <<endl;

v1.clear();//Can it deallocate the memory allocated in myvector()?

n3 = v1[0];
n4 = v1[1];

delete n3;//Are there other methods to deallocate the memory?
delete n4;

}

The above code works. I am not sure about memory deallocation.

Thanks a lot.

John

Cy Edmunds said:
John said:
Hi all:

I write the following code about vector and list. Why the result is
not correct?

Thanks.

John

--------------------------------------------------------------
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

typedef struct{
int ID;
double time;
}node;

void myvector(vector<node*> &v0){
node n1, n2;
n1.ID = 1;
n1.time = 1.1;
n2.ID = 2;
n2.time = 2.2;

v0.push_back(&n1);
v0.push_back(&n2);
cout<<"v0:"<<v0[0]<<endl;
cout<<"ID1:"<< (*v0.begin())->ID <<" time1:"<<v0[0]->time <<endl;
cout<<"ID2:"<< v0[1]->ID <<" time2:"<<v0[1]->time <<endl;
}

list< node* > mylist()
{
list< node* > l;

node n1, n2;
n1.ID = 5;
n1.time = 5.1;
n2.ID = 6;
n2.time = 6.2;
l.push_back(&n1);

By taking the address of a local variable you are setting yourself up for
trouble. When the function ends the local variables are destroyed and the
pointers are pointing at nothing.

[snip]

Let's try again:

// using namespace std; -- why pollute the global namespace?

class node // let's do this in C++
{
private:
int m_ID;
double m_time;
public:
node(int i_ID, double i_time) : m_ID(i_ID), m_time(i_time) {}
int ID() const {return m_ID;}
double time() const {return m_time;}
};

std::list<node> mylist() // not std::list<node*>
{
std::list<node> answer;
answer.push_back(node(5, 5.1));
answer.push_back(node(6, 6.2));
return answer;
}


Warning: untested code.
 
M

Markus Dehmann

class node{//simple class, just for test purpose.:) public:
int ID;
double time;
};

The only difference between a struct and a class is that members are
automatically public in a struct, but private in a class. So, if you want
public member variables in a class, you must declare them as public.
int main()
{
std::vector<node*> v1;
myvector(v1);

node *n3, *n4;

cout<<"v1:"<<v1[0]<<endl;
cout<<"ID1:"<< (v1.front())->ID <<" time1:"<<v1[0]->time <<endl;
cout<<"ID2:"<< v1[1]->ID <<" time2:"<<v1[1]->time <<endl;

v1.clear();//Can it deallocate the memory allocated in myvector()?

n3 = v1[0];
n4 = v1[1];

delete n3;//Are there other methods to deallocate the memory?
delete n4;

}

It's not nice: You erase the elements of v1 (v1.clear()) and then still
access them. clear does not deallocate the memory. You have to do it
yourself with delete. There is no other way than delete -- except from
using std::auto_ptr. (Unfortunately, auto_ptr's can't be stored in a
vector (http://www.research.att.com/~bs/bs_faq2.html#auto_ptr))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top