Representation of memory of float and int
struct s
{
int a;
float b;
int c;
}
How is this structure members stored in memory location? My understanding is that when 开发者_StackOverflow中文版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.
Assuming sizeof(int) == sizeof(float) == 4
and that the compiler doesn't decide to put some padding in, your answer is correct. I don't know what "Integers and float will have different address spaces in the memory" means, so I'm not sure I can answer your final question.
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 1000 b will be 1004.will c be 1008.
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 use pragma 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:
- Endianess.
- If you're casting to a pointer of another type (ie type-punning or aliasing) beware that C99 won't support pointer aliasing. See the strict aliasing rule.
It's not really meaningful to say that the members of a struct are allocated at the same time as a struct. A struct is simply the collection of its members. There is no additional data to a struct that you could meaningfully say that "the struct was allocated but not the members".
Exactly how memory is allocated depends on the platform and the compiler. If, say, an int and a float are both 4 bytes -- which I think is typical -- then this struct would consist of 12 bytes, the three fields one after the other.
Compilers sometimes put all the fields in a struct on 4-byte boundaries, or some other boundary. So if you had a struct made up of a char, a float, and an int, you'd probably get 1 byte for the char, 3 filler bytes, then 4 for the float and 4 for the int.
Integers and floats DON'T have different address spaces in memory, at least not on any system I've ever used. They're freely mixed together. The processor may have separate int and float registers, but that's a whole different thing.
You can use offsetof macro to get structure member offset. Add this offset to structure address to get address of member.
typedef struct s {
int a;
float b;
int c;
} s_t;
s_t var;
printf("var.c addresss=%p\n",((const uint8_t*)&var)+offsetof(s_t,c));
精彩评论