struct s
{
int a;
float b;
int c;
}
How is this structure members stored in memory location?
My understanding is that when a structure variable is allocated then memory for the
structure members will also be allocated. If 1000 is the starting address then a will be at
1000, b will be 1004, and c will be 1008.
Integers and float will have different address spaces in the memory. How is a float and
an integer declared inside the struct represented in memory? Please help me to
understand.
Since the size of int and even float is platform specific as is how structures are packed/stored/aligned/retrieved in memory, there is no platform neutral answer to your question.
If it matters to you how structs are stored in memory, you’ll have to look into some compiler-specific ways to ensure structs are properly packed and aligned. For gcc see here for how to use
__attribute__to control packing and alignment. For MSVC see here for how to usepragma pack.Of course HERE BE DRAGONS:
You’re venturing into the platform-specific way of how data is stored in memory, yarr there be many opportunities for bugs if you’re taking this hunk of memory and sending it off to another device. To prevent bugs (and there will be bugs) keep in mind: