Jump to content
  • Advertisement
Sign in to follow this  
XenoPhyre

(C++) Is there a guarantee of static array element data contiguity in RAM?

This topic is 2975 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

When an array with several elements is created, are the individual elements of the array guaranteed to be contiguous in memory?

Meaning, for example, with an array of 5 ints called 'intArray', represented in RAM:

| intArray[0] 0x0001ca00 | intArray[1] 0x0001ca04 | intArray[2] 0x0001ca08 | intArray[3] 0x0001ca0c | intArray[4] 0x0001ca10

With no breaks in between the elements. Is this contiguity always present whenever an array is instantiated (resizable arrays excluded)?

[Edited by - XenoPhyre on October 23, 2010 11:13:37 PM]

Share this post


Link to post
Share on other sites
Advertisement
I've read somewhere that this isn't true when using resizable arrays (vectors, etc.).... Is there any truth to that?

Share this post


Link to post
Share on other sites
I believe that indeed does not hold true with resizable arrays, perhaps because of all the copying and shuffling-around that goes on when arrays are resized, elements removed or added, etc.

Share this post


Link to post
Share on other sites
On the contrary, std::vector is guaranteed to provide contiguous storage for its elements.

This does not apply to the other container classes AFAIK.

Share this post


Link to post
Share on other sites
Quote:
Original post by ApochPiQ
On the contrary, std::vector is guaranteed to provide contiguous storage for its elements.


Hmm, I had no idea std::vectors featured contiguity. Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by LessBread
Does the page size have any bearing on this? Just wondering...


Any bearing on what in particular? A vector is guaranteed continuous by the standard (23.2.4 §1), as are arrays. I did notice a misconception in the EASTL that someone thinks vectors aren't guaranteed contiguous... it was one of the "differences" they listed between their version and the standard library version.

While physical RAM pages may not be contiguous (in a virtual memory system), that actually will have no effect on your access speed (for caching or other issues), as page boundaries are always cache aligned.

Share this post


Link to post
Share on other sites
In the first version of the C++ standard, released in 1998, storage for std::vector wasn't guaranteed to be contiguous. However when technical corrigenda 1 was released in 2003, a requirement for contiguous storage was added.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!