While answering this question I was asked to provide standard quotes. I was shocked to find in the C++14 draft:
§ 3.9 Types [basic.types]
- The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N equals sizeof(T)
Hmm.. it doesn't say that the "unsigned char objects" must be contiguous in memory. Maybe it is implied by "sequence". Then I found a specific mention of "contiguous bytes of storage", but...
§ 1.8 The C++ object model [intro.object]
- [...] An object of trivially copyable or standard-layout type (3.9) shall occupy contiguous bytes of storage.
What? Only trivially copyable and standard-layout types are required to occupy contiguous bytes of storage? The rest of the types can have "holes" in the storage they occupy? I searched the rest of the standard but could not find any other relevance to "contiguous storage". Granted I am not that familiar with the standard.
If that is true (for me it would be the greatest shock about the standard) how does that go with sizeof
and pointer arithmetics? Are (were) there really any architectures/compilers that use (used) non-contiguous bytes of storage for types?
I really hope I am misinterpreting and/or missing something.
edit: I thought that maybe it is related to padding, but this interpretation would make sense only if trivially copyable or standard-layout types could not have padding. Then you say these types occupy contiguous bytes storage, and for the rest of types who can have padding you don't say that. But that is clearly not the case as any struct type can have padding.
So array elements are contiguous in memory and therefore do not require any metadata.
An array in C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They are used to store similar type of elements as in the data type must be the same for all elements.
1. Contiguous Memory Allocation : Contiguous memory allocation is basically a method in which a single contiguous section/part of memory is allocated to a process or file needing it.
A contiguous array is just an array stored in an unbroken block of memory: to access the next value in the array, we just move to the next memory address. This means arr is a C contiguous array because the rows are stored as contiguous blocks of memory. The next memory address holds the next row value on that row.
A class with a virtual base class may not be in contiguous bytes of memory (as some of the bytes between the virtual base and the rest of the class can be occupied by another class that also derives from the virtual base.
class A {
int a;
};
class B: virtual A {
int b;
};
class C: virtual A {
int c;
};
class D: public B, C {
int D;
}
The memory for an object of class D can be organized something like this:
-------
| a |
-------
| b |
-------
| c |
-------
| d |
-------
in increasing memory order. Object "C", which consists of ints "a" and "c", does not occupy consecutive memory locations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With