Problem with C++ vectors

Discussion in 'C++' started by vasim98@gmail.com, Apr 19, 2007.

  1. Guest

    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
    , Apr 19, 2007
    #1
    1. Advertising

  2. On 19 Apr, 14:38, wrote:
    > 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.

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Apr 19, 2007
    #2
    1. Advertising

  3. Lionel B Guest

    On Thu, 19 Apr 2007 05:38:24 -0700, vasim98 wrote:

    > 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,

    --
    Lionel B
    Lionel B, Apr 19, 2007
    #3
  4. James Kanze Guest

    wrote:
    > 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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 19, 2007
    #4
  5. James Kanze Guest

    wrote:
    > 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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 19, 2007
    #5
  6. Salt_Peter Guest

    On Apr 19, 8:38 am, wrote:
    > 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)?
    std::vector< std::string > vs(1000, "default string"); // what the
    hell?

    >
    > 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
    Salt_Peter, Apr 19, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Phelps
    Replies:
    3
    Views:
    384
    Wendy Smoak
    Jun 10, 2005
  2. ree
    Replies:
    9
    Views:
    333
    Ron Natalie
    Nov 9, 2003
  3. Adam Hartshorne
    Replies:
    6
    Views:
    388
    Chris Croughton
    Mar 24, 2005
  4. Replies:
    3
    Views:
    675
    Shadowman
    Mar 26, 2008
  5. Guest
    Replies:
    0
    Views:
    424
    Guest
    Sep 14, 2005
Loading...

Share This Page