c++arraysmemorystructmemory-layout

Memory layout of C++ struct with static multi-dimensional arrays


I have a C++ struct containing integers and static arrays like so:

struct sMyStruct {
    unsigned int uiVal;
    int iVal1;
    int iVal2 = 0;
    float afVals[ NUM_VALUES ];
    unsigned char auiVals[ NUM_VALUES ];
    float aafVals[ NUM_VALUES ][ NUM_VALUES_2 ];
};

where NUM_VALUES and NUM_VALUES_2 are #defined at compile-time.

Is it safe for me to assume that, allowing for some padding between the members to suit alignment boundaries, each member in this structure, including the arrays, will be laid out consecutively (contiguously) in memory?

I am passing this structure to a LabVIEW application and only the three integers and the first array (afVals) are arriving safely. The last two arrays are getting lost and I'm not sure why.


Solution

  • Many thanks to all who posted constructive comments: I think I have my answer now! It appears that the general consensus is: yes, I am safe to assume that all members of the structure, including the arrays, will be laid out contiguously in memory (allowing for any padding inserted for alignment purposes).