Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can technically objects occupy non-contiguous bytes of storage?

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]

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

  1. [...] 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.

like image 811
bolov Avatar asked Sep 30 '16 21:09

bolov


People also ask

Are arrays contiguous in physical memory?

So array elements are contiguous in memory and therefore do not require any metadata.

Are arrays contiguous in memory C++?

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.

What is contiguous memory allocation in c++?

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.

Why is an array contiguous?

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.


1 Answers

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.

like image 102
1201ProgramAlarm Avatar answered Sep 23 '22 07:09

1201ProgramAlarm