Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Zeblar Nagrim

std::vector

This topic is 6115 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!