push_back address outside function scope


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: push_back address outside function scope

  1. #1
    Join Date
    Jul 2007
    Posts
    3

    push_back address outside function scope

    I am trying to grow a vector inside of a function and store the address of each new element so that I can point to each one later on outside the function. I guess I have a basic misunderstanding of references and pointers because I thought the following code would do the trick but the address being stored seems to be a local address. I thought since I was passing by reference the address of the new vector elements would be the same outside the function.

    Code:
    void add_elements(vector<int>& y,vector<int*>& yadd) {
    
    cout << "address of y: " << &y << endl; for (int ii=0; ii<5; ii++) { y.push_back(ii); yadd.push_back(&y.at(ii)); cout << y.at(ii) << " at address " << &y.at(ii) << " and " << yadd.at(ii) << endl; }
    } void main () {
    vector<int> x; vector<int*> xadd; add_elements(x,xadd); cout << "address of x: " << &x << endl; for (int ii=0; ii<x.size(); ii++) cout << x.at(ii) << " at address " << &x.at(ii) << " and " << xadd.at(ii) << endl;
    }
    The output is

    address of y: 0012FF48
    0 at address 00355160 and 00355160
    1 at address 00355184 and 00355184
    2 at address 00355198 and 00355198
    3 at address 003551CC and 003551CC
    4 at address 003551E8 and 003551E8
    address of x: 0012FF48
    0 at address 003551D8 and 00355160
    1 at address 003551DC and 00355184
    2 at address 003551E0 and 00355198
    3 at address 003551E4 and 003551CC
    4 at address 003551E8 and 003551E8



    How do I get the address of those new elements inside the function and pass them back out?

    Thanks

  2. #2
    Join Date
    Jan 2007
    Posts
    145
    Where are you telling cout to print a pointer?

  3. #3
    Join Date
    May 2006
    Posts
    176
    When a new element is added to vector with push_back, the internal buffer may be reallocated to a new location, therefore addresses obtained previously with at are not more valid.

    You can first add all of the elements, then store addresses in a second loop:

    Code:
    for (int ii=0; ii<5; ii++) 
    {
        y.push_back(ii);
    }
    for (int ii=0; ii<5; ii++) 
    {
        yadd.push_back(&y.at(ii));
        cout << y.at(ii) << . . .
    }
    Now the stored addresses are valid until you perform other operations on vector.

    I think such addressing must be avoided. You should access elements by index.

    I hope this makes sense.

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    You are missing a crucial observation about vectors: the elements they contain are copies of what you pass in a push_back call, and to add to this, the elements of a vector are stored on the heap, which is a totally different address space. You're not creating any function local variables anyway, you're simply copying the addresses of the elements of one vector into another vector. In other words, your copying addresses on the heap to another chunk of heap memory...
    As Viorel noted, some of the addresses copied might be invalid due to reallocation. I'm not going to explain what reallocation is here, but if you want to learn more about it look for one of my tips on DevX with the search word "reallocation".
    BTW, what's the purpose of calling at() instead of using the subscript notation? You're not catching any exceptions anyway, so the subscript notation is a better choice both in terms of performance and readability:

    Code:
    cout<<yad[ii]=5;
    Last edited by Danny; 07-30-2007 at 12:05 PM.
    Danny Kalev

  5. #5
    Join Date
    Jul 2007
    Posts
    3
    thanks. i think that clears it up for me. i'll stick to indexing them.

Similar Threads

  1. please help me
    By idea2 in forum .NET
    Replies: 7
    Last Post: 02-22-2007, 08:40 PM
  2. Register a DLL in code and Call a function by address?
    By Sean Woods in forum VB Classic
    Replies: 0
    Last Post: 01-11-2002, 05:10 PM
  3. Getting a list of files into an array
    By Scott in forum VB Classic
    Replies: 12
    Last Post: 12-21-2001, 05:21 PM
  4. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 04:45 PM
  5. Calling a function using its address
    By Véronique in forum VB Classic
    Replies: 12
    Last Post: 04-10-2000, 05:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center