Connecting Tech Pros Worldwide Help | Site Map

getting last pushed element pointer of vector.

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 5th, 2006, 05:45 AM
clqrq@yahoo.de
Guest
 
Posts: n/a
Default getting last pushed element pointer of vector.

i want to do the following....

1) fill a struct (lets call it Data)
2) push it to a vector of Data
3) get a pointer to pushed vector-element.

std::vector<DatavData;

(lets assume we now filled some elements into the vector, erased some,
and now do the following...)

Data x = /* something */ y;
vData.push_back(x);

my question is:

is there a more simplier way to get a pointer to the new vector-element
than

Data* pData = &vData[vData.size() - 1];

and IS it the correct pointer? does push always add to the end?

thx for any knowlege ;-)


  #2  
Old September 5th, 2006, 06:15 AM
Mark P
Guest
 
Posts: n/a
Default Re: getting last pushed element pointer of vector.

clqrq@yahoo.de wrote:
Quote:
i want to do the following....
>
1) fill a struct (lets call it Data)
2) push it to a vector of Data
3) get a pointer to pushed vector-element.
>
std::vector<DatavData;
>
(lets assume we now filled some elements into the vector, erased some,
and now do the following...)
>
Data x = /* something */ y;
vData.push_back(x);
>
my question is:
>
is there a more simplier way to get a pointer to the new vector-element
than
>
Data* pData = &vData[vData.size() - 1];
>
and IS it the correct pointer? does push always add to the end?
>
Not push, push_back, and yes as the name implies it pushes to the end.
A simpler way to get the pointer is:

Data* pData = &vData.back();
  #3  
Old September 5th, 2006, 10:45 AM
Michiel.Salters@tomtom.com
Guest
 
Posts: n/a
Default Re: getting last pushed element pointer of vector.

clqrq@yahoo.de wrote:
Quote:
i want to do the following....
>
1) fill a struct (lets call it Data)
2) push it to a vector of Data
3) get a pointer to pushed vector-element.
>
std::vector<DatavData;
>
(lets assume we now filled some elements into the vector, erased some,
and now do the following...)
>
Data x = /* something */ y;
vData.push_back(x);
>
my question is:
>
is there a more simplier way to get a pointer to the new vector-element
than
>
Data* pData = &vData[vData.size() - 1];
>
and IS it the correct pointer? does push always add to the end?
vData[vData.size() - 1] is also called vData.back(), so yes, this
works.
It is the correct pointer. However, the real question is, WILL it be
the correct
pointer? A vector will move the Data objects around when adding new
Data
(not for every Data, that would be too slow though). Can you ensure
pData
is not used after vData changes? If not, you should remember both
&vData
and vData.size(). That also allows you to deal with the situation where
vData shrinks.

HTH,
Michiel Salters

  #4  
Old September 5th, 2006, 12:45 PM
clqrq@yahoo.de
Guest
 
Posts: n/a
Default Re: getting last pushed element pointer of vector.

what about the following situation:
______________________
struct Data {
some x;
another y;
}

class MyClass {
/*...*/
Data* Adddata(some x, another y);
private:
std::vector<Data*m_vpData;
}

Data* MyClass::Adddata(some x, another y) {
Data d;
d.x =x;
d.y =y;
m_vpData.push_back(&d);
return m_vpData.back();
}
_______________________

d is defined only inside MyClass::Adddata, so what does happen with the
memory of d and therefore with the memory the last element of m_vpData
after return?

  #5  
Old September 5th, 2006, 01:45 PM
Gernot Frisch
Guest
 
Posts: n/a
Default Re: getting last pushed element pointer of vector.

Quote:
Data* MyClass::Adddata(some x, another y) {
Data d;
d.x =x;
d.y =y;
m_vpData.push_back(&d);
return m_vpData.back();
}
Yuck! You're storing a pointer to an auto-variable. After the function
quits, you've got a dangling pointer stored.
The correct(er) way is to:

Data* MyClass::Adddata(some x, another y) {
Data* d = new Data();
d.x =x;
d.y =y;
m_vpData.push_back(d);
return m_vpData.back();
}

but then you have to delete every item in m_vpData before "forgetting"
it. Also, the vector stored data on the heap. It's uncommon to store
pointers in a vector. There are situations where you need it, but if
you have the choice, try getting along with the leastmost pointers you
can.

-G.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.