Problem with C++ vectors

V

vasim98

I am surprised to see the output of the following C++ program using
vectors. The program is so simple. An element is pushed in a vector
"v" and the pointer to this element is obtained as "ptr_v". Then an
other element is pushed in the vector and after that the pointer
"ptr_v" to the earlier element is lost!!!

Whereas in case of array, the pointer to already stored element is
preserved even if you insert elements to that array. You can try
running this simple program and see the output.

The "ptr_v" addressing the 1st element of vector loses it's value when
an other element is inserted in the vector!

Why does this happen??? Any expert of C++ data structures can explain?

//
*****************************************************************************************
#include <iostream>
#include <vector>
using namespace std;

int main(int)
{

vector<int> v;

v.push_back(40);
int *ptr_v;
ptr_v = &v.at(0);
cout<<"Value = "<<*ptr_v<<endl;
cout<<"Adress of ptr is "<<ptr_v<<endl;
v.push_back(98);

cout<<"Adress of ptr is still "<<ptr_v<<endl;
cout<<"Value = "<<*ptr_v<<endl;

//*************************************
cout<<"......Now for array......"<<endl;

int array[10];
array[0] = 40;
int *ptr_array;
ptr_array = &array[0];
cout<<"Value = "<<*ptr_array<<endl;
cout<<"Adress of ptr is "<<ptr_array<<endl;
array[1] = 98;
cout<<"Adress of ptr is still "<<ptr_array<<endl;
cout<<"Value = "<<*ptr_array<<endl;


return 0;
}
//
********************************************************************************************


Thanks
Waseem
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

I am surprised to see the output of the following C++ program using
vectors. The program is so simple. An element is pushed in a vector
"v" and the pointer to this element is obtained as "ptr_v". Then an
other element is pushed in the vector and after that the pointer
"ptr_v" to the earlier element is lost!!!

Whereas in case of array, the pointer to already stored element is
preserved even if you insert elements to that array. You can try
running this simple program and see the output.

The "ptr_v" addressing the 1st element of vector loses it's value when
an other element is inserted in the vector!

Why does this happen??? Any expert of C++ data structures can explain?

It's not at all surprising the vector is in charge of managing the
memory used to store its elements. When you push the second element
onto the vector it will need to resize the memory-area used to store
the elements and will thus reallocate its elements.

There are a number of operations that might cause a vector to
invalidate iterators, and all of them will also invalidate any
pointers to members.
 
L

Lionel B

I am surprised to see the output of the following C++ program using
vectors. The program is so simple. An element is pushed in a vector "v"
and the pointer to this element is obtained as "ptr_v". Then an other
element is pushed in the vector and after that the pointer "ptr_v" to
the earlier element is lost!!!

Whereas in case of array, the pointer to already stored element is
preserved even if you insert elements to that array. You can try running
this simple program and see the output.

Why should this behaviour surprise you? A vector is not an array and
nothing in the standard says that elements of a vector may not be moved
about in memory.
The "ptr_v" addressing the 1st element of vector loses it's value when
an other element is inserted in the vector!

With standard containers it is generally disastrous to access elements
via raw pointers; rather, iterators should be used - and there are clear
rules as to when an iterator is "invalidated" (in the sense that the
pointer in your example apparently is) by some operation.

Any good book on the Standard Library ought to clarify this further.

[...]

Cheers,
 
J

James Kanze

I am surprised to see the output of the following C++ program using
vectors. The program is so simple. An element is pushed in a vector
"v" and the pointer to this element is obtained as "ptr_v". Then an
other element is pushed in the vector and after that the pointer
"ptr_v" to the earlier element is lost!!!

Invalidated. Any use of the pointer is undefined behavior. If
you need to maintain pointers in such cases, you'll either need
a different type of container, or to avoid increasing the size
of the vector.
Whereas in case of array, the pointer to already stored element is
preserved even if you insert elements to that array.

Whereas in the case of a C-style array, you cannot insert
elements, so the question doesn't come up. If you use a vector
like you would a C-style array, declaring its size up front, you
won't have any problems either.
You can try
running this simple program and see the output.
The "ptr_v" addressing the 1st element of vector loses it's value when
an other element is inserted in the vector!
Why does this happen??? Any expert of C++ data structures can explain?

Try implementing a simple vector class yourself. Or try doing
the equivalent using new int[], and you'll probably get the same
effect. The standard says that iterators, pointers and
references to elements in the vector may be invalidated anytime
the capacity of the vector is increased for the simple reason
that you can't implement anything reasonable otherwise.
 
J

James Kanze

I am surprised to see the output of the following C++ program using
vectors. The program is so simple. An element is pushed in a vector
"v" and the pointer to this element is obtained as "ptr_v". Then an
other element is pushed in the vector and after that the pointer
"ptr_v" to the earlier element is lost!!!

Invalidated. Any use of the pointer is undefined behavior. If
you need to maintain pointers in such cases, you'll either need
a different type of container, or to avoid increasing the size
of the vector.
Whereas in case of array, the pointer to already stored element is
preserved even if you insert elements to that array.

Whereas in the case of a C-style array, you cannot insert
elements, so the question doesn't come up. If you use a vector
like you would a C-style array, declaring its size up front, you
won't have any problems either.
You can try
running this simple program and see the output.
The "ptr_v" addressing the 1st element of vector loses it's value when
an other element is inserted in the vector!
Why does this happen??? Any expert of C++ data structures can explain?

Try implementing a simple vector class yourself. Or try doing
the equivalent using new int[], and you'll probably get the same
effect. The standard says that iterators, pointers and
references to elements in the vector may be invalidated anytime
the capacity of the vector is increased for the simple reason
that you can't implement anything reasonable otherwise.
 
S

Salt_Peter

I am surprised to see the output of the following C++ program using
vectors. The program is so simple. An element is pushed in a vector
"v" and the pointer to this element is obtained as "ptr_v". Then an
other element is pushed in the vector and after that the pointer
"ptr_v" to the earlier element is lost!!!

As expected. Although its not lost. Your code lost it - yes.
The vector has member functions begin(), front(), back() and end()
that will return corresponding iterators.
Whereas in case of array, the pointer to already stored element is
preserved even if you insert elements to that array. You can try
running this simple program and see the output.

The "ptr_v" addressing the 1st element of vector loses it's value when
an other element is inserted in the vector!

Why does that surprise you? Is std::vector not dynamic?

Did you not know that:
std::vector< int > v(40);
generates a container of 40 default initialized elements?
yet its still a dynamic container?
std::vector< int > v(40, 99);
generates the same with whatever initialized integer value you choose
(other than 99)?
Why does this happen??? Any expert of C++ data structures can explain?

//
*****************************************************************************************
#include <iostream>
#include <vector>
using namespace std;

int main(int)
{

vector<int> v;

v.push_back(40);
int *ptr_v;
ptr_v = &v.at(0);
cout<<"Value = "<<*ptr_v<<endl;
cout<<"Adress of ptr is "<<ptr_v<<endl;
v.push_back(98);

cout<<"Adress of ptr is still "<<ptr_v<<endl;
cout<<"Value = "<<*ptr_v<<endl;

//*************************************
cout<<"......Now for array......"<<endl;

int array[10];
array[0] = 40;
int *ptr_array;
ptr_array = &array[0];
cout<<"Value = "<<*ptr_array<<endl;
cout<<"Adress of ptr is "<<ptr_array<<endl;
array[1] = 98;
cout<<"Adress of ptr is still "<<ptr_array<<endl;
cout<<"Value = "<<*ptr_array<<endl;

return 0;}

//
********************************************************************************************

Thanks
Waseem
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top