Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the memory allocated for struct members continguous? What if a struct member is an array?

Tags:

c++

arrays

c

struct

In C/C++ suppose I define a simple struct named test as follows.

struct test {    double height;    int    age;    char   gender; } 

For a specific instance of this struct say test A are A.height, A.age, A.gender contiguous in memory?

More generally, how do the layouts in memory for a Structure of Arrays and an Array of structures look like? A picture would be really helpful.

like image 360
smilingbuddha Avatar asked Dec 04 '11 18:12

smilingbuddha


People also ask

How is memory allocated for structure members?

If we create an object of some structure, then the compiler allocates contiguous memory for the data members of the structure. The size of allocated memory is at least the sum of sizes of all data members. The compiler can use padding and in that case there will be unused space created between two data members.

How memory is allocated for structure and array?

The memory can be allocated using the malloc() function for an array of struct . This is called dynamic memory allocation. The malloc() (memory allocation) function is used to dynamically allocate a single block of memory with the specified size. This function returns a pointer of type void .

Can an array be a member of a struct?

A structure may contain elements of different data types – int, char, float, double, etc. It may also contain an array as its member. Such an array is called an array within a structure. An array within a structure is a member of the structure and can be accessed just as we access other elements of the structure.

Does struct allocate memory?

This tells the compiler how big our struct is and how the different data items (“members”) are laid out in memory. But it does not allocate any memory. To allocate memory for a struct, we declare a variable using our new data type.


1 Answers

They will not necessarily be contiguous in memory. This is due to struct padding.

However, in your particular case, it may very well be contiguous. But if you changed the order to something like this:

struct test {     char   gender;     int    age;     double height; } 

then they most likely will not be. However, in your particular case, you will still likely get padding after gender, to realign the struct to 8 bytes.


The difference between SoA (Struct of Arrays) and AoS (Array of Structs) would be like this:

SoA:

----------------------------------------------------------------------------------- | double | double | double | *pad* | int | int | int | *pad* | char | char | char | ----------------------------------------------------------------------------------- 

AoS:

----------------------------------------------------------------------------------- | double | int | char | *pad* | double | int | char | *pad* | double | int | char | ----------------------------------------------------------------------------------- 

Note that AoS pads within each struct. While SoA pads between the arrays.

These have the following trade-offs:

  1. AoS tends to be more readable to the programmer as each "object" is kept together.
  2. AoS may have better cache locality if all the members of the struct are accessed together.
  3. SoA could potentially be more efficient since grouping same datatypes together sometimes exposes vectorization.
  4. In many cases SoA uses less memory because padding is only between arrays rather than between every struct.
like image 112
Mysticial Avatar answered Sep 19 '22 03:09

Mysticial