By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Problem inserting into Vector

P: n/a
Hi,I had a problem while I was doing the assignment .the code is like
below(not exact):
//======================================

class Buyer
{
private:
int _product;

public:
//Buyer(void){ _product=-1;);
int get_product(void);
Buyer( int n ){ _product = n;};
};

int Buyer::get_product(void){
return _product;
};

std::vector<Buyer *> buyer_queue;

int main( int argc, char *argv[] )
{
for(;;){
int i=int(1+rand()%10);
Buyer b=Buyer(i);

buyer_queue.push_back(&b);
printf("\nB[%d]",(*buyer_queue[buyer_queue.size()-1]).get_product());

for (int j=0;j<buyer_queue.size();j++){
printf("\t\n looped [%d]",(*buyer_queue[j]).get_product());

}
usleep(1000000);
}
}

//====================================
the problem is in the for loop,why it outputs the same value for
member"product" of each Buyer?
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
wu*******@hotmail.com wrote:
Hi,I had a problem while I was doing the assignment .the code is like
below(not exact):
//======================================

class Buyer
{
private:
int _product;

public:
//Buyer(void){ _product=-1;);
int get_product(void);
Buyer( int n ){ _product = n;};
};

int Buyer::get_product(void){
return _product;
};

std::vector<Buyer *> buyer_queue;

int main( int argc, char *argv[] )
{
for(;;){
int i=int(1+rand()%10);
Buyer b=Buyer(i);

buyer_queue.push_back(&b);
printf("\nB[%d]",(*buyer_queue[buyer_queue.size()-1]).get_product());

for (int j=0;j<buyer_queue.size();j++){
printf("\t\n looped [%d]",(*buyer_queue[j]).get_product());

}
usleep(1000000);
}
}

//====================================
the problem is in the for loop,why it outputs the same value for
member"product" of each Buyer?


Don't see any of the "Miranda" functions.

Provide a default constructor, copy constructor, and assignment operator.

Also, you want to call srand() before you call rand().
Jul 22 '05 #2

P: n/a
> Buyer b=Buyer(i);

buyer_queue.push_back(&b);


The address you are storing is the address of a local variable with automatic
storage duration. That variable is being destroyed at the end of the for loop
in which it is declared and thusly your vector contains a pointer to an object
that has been destroyed.

However, in my runtime environment every pass through the loop results in that
variable being reconstructed at the same memory location as the last pass
through the loop. As a result all the pointers stored in the vector point to
the same object and at the time those pointers are accessed they are infact
pointing to a valid object.

Is there some reason you just didn't use std::vector<Buyer>?


Jul 22 '05 #3

P: n/a
DaKoadMunky wrote:
Buyer b=Buyer(i);

buyer_queue.push_back(&b);

The address you are storing is the address of a local variable with automatic
storage duration. That variable is being destroyed at the end of the for loop
in which it is declared and thusly your vector contains a pointer to an object
that has been destroyed.

However, in my runtime environment every pass through the loop results in that
variable being reconstructed at the same memory location as the last pass
through the loop. As a result all the pointers stored in the vector point to
the same object and at the time those pointers are accessed they are infact
pointing to a valid object.

Is there some reason you just didn't use std::vector<Buyer>?



declare buyer as a gloabl variable would do ?if I want to insert many
buyers with random product.
Jul 22 '05 #4

P: n/a
>declare buyer as a gloabl variable would do ?if I want to insert many
buyers with random product.


The object passed to vector<T>::push_back will be copied using the T copy
constructor. It is the copy that becomes part of the controlled sequence.

vector<Buyer> buyers;

for(int i=0;i<10;++i)
{
Buyer buyer(i);
buyers.push_back(buyer);
}

At this point the vector contains 10 elements that are copies of the elements
that were arguments to vector<T>::push_back.

Jul 22 '05 #5

P: n/a
In message <OC*************@newssvr14.news.prodigy.com>, red floyd
<no*****@here.dude> writes
wu*******@hotmail.com wrote:
Hi,I had a problem while I was doing the assignment .the code is like
below(not exact):
//======================================
class Buyer
{
private:
int _product;
public:
//Buyer(void){ _product=-1;);
int get_product(void);
Buyer( int n ){ _product = n;};
};
int Buyer::get_product(void){
return _product;
};
std::vector<Buyer *> buyer_queue;
int main( int argc, char *argv[] )
{
for(;;){
int i=int(1+rand()%10);
Buyer b=Buyer(i);
buyer_queue.push_back(&b);
printf("\nB[%d]",(*buyer_queue[buyer_queue.size()-1]).get_product());
for (int j=0;j<buyer_queue.size();j++){
printf("\t\n looped [%d]",(*buyer_queue[j]).get_product());

}
usleep(1000000);
}
}
//====================================
the problem is in the for loop,why it outputs the same value for
member"product" of each Buyer?
Don't see any of the "Miranda" functions.

Provide a default constructor, copy constructor, and assignment operator.


Why? The class doesn't use pointers or manage resources, so the
compiler-generated copy and assignment will have the correct semantics.
Writing your own, unnecessarily, is just another opportunity to add
errors.
Also, you want to call srand() before you call rand().


The real problem is that every entry in the vector is a pointer to
(effectively) the *same* object, 'b'. So no matter which element of the
vector you look at, you see the last value assigned to b.

--
Richard Herring
Jul 22 '05 #6

P: n/a

<wu*******@hotmail.com> wrote in message
news:10***************@clint.its.waikato.ac.nz...
DaKoadMunky wrote:
Buyer b=Buyer(i);

buyer_queue.push_back(&b);

The address you are storing is the address of a local variable with automatic storage duration. That variable is being destroyed at the end of the for loop in which it is declared and thusly your vector contains a pointer to an object that has been destroyed.

However, in my runtime environment every pass through the loop results in that variable being reconstructed at the same memory location as the last pass through the loop. As a result all the pointers stored in the vector point to the same object and at the time those pointers are accessed they are infact pointing to a valid object.

Is there some reason you just didn't use std::vector<Buyer>?


declare buyer as a gloabl variable would do ?if I want to insert many
buyers with random product.


No. Simply store Buyer objects instead of pointers to them. Then each
object in the vector will be unique, because push_back will make a copy of
the object and store that. Then when you re-use the object, it will be
different.

Also, you should use

Buyer b(i);

instead of

Buyer b=Buyer(i);

There's no need for the assignment. (Perhaps you were thinking of "Buyer* b
= new Buyer(i);"?)

-Howard

Jul 22 '05 #7

P: n/a
Richard Herring wrote:
[ reply to my incredibly dumb comment redacted]


You're right... I missed the fact that he was storing *pointers*. I've
been bitten by bad or missing Miranda functions with vectors before, so
that's why I suggested that.
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.