Jump to content
  • Advertisement


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

Zeblar Nagrim


This topic is 6195 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...):

MYSTRUCT object;
... intialize here....

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!