Skip to main content
GameDev.net gamedev.net
🔒 Locked

std::vector

Started by Zeblar Nagrim Dec 27, 2001 at 2:20 AM 1 replies 300+ views
Original Post
Zeblar Nagrim
Zeblar Nagrim
Is this possible (I know stupid question...):
  
std::vector<MYSTRUCT>vecMyDyArray;

MYSTRUCT object;
... intialize here....
vecMyDyArray.pushbach(object);

MYSTRYCT *pointer_to_a_element = &vecMyDyArray.back();
  
Will pointer_to_a_element point to the last element in the array? And the second last element when I add another object to the array? Zeblar Nagrim, Lord of Chaos
null_pointer
null_pointer
std::vector::back() returns the last element in the array. std::vector retains the order of its elements unless you tell it to change (i.e., std::sort, std::remove_if, etc.). If you add another element then the previous value returned by std::vector::back() will indeed be the next-to-last element.

You must be careful when taking the addresses of elements stored in standard containers. The standard containers store by value (as opposed to storing by reference), which means that they can freely create a destroy objects in memory as long as the objects'' values remain the same. This will cause the addresses of the stored elements to change.

If you are using Microsoft Visual C++, then look in the VC++ docs under "vector" in the section "C++ Languages and Libraries" or something like that, and take note of which member functions may cause "reallocation" to occur.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.