getting last pushed element pointer of vector.

C

clqrq

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<Data> vData;

(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 ;-)
 
M

Mark P

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<Data> vData;

(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();
 
M

Michiel.Salters

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<Data> vData;

(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
 
C

clqrq

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?
 
G

Gernot Frisch

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.
 

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,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top