c - gcc compiler is not allocating memory contigously -
i writing simple program, after declaring variables checking addresses of variables memory not allocated contiguously there gaps in between. here program. why leaving gaps not understanding
#include < stdio.h > #include < stdint.h > int main() { char char_one,char_two; int = 5,b = 7,*ptr,*ptr_one; static int *sum_ptr; printf("address of %u\n",&a); printf("address of variable b %u\n",&b); printf("address of ptr variable %u\n",&ptr); printf("address of ptr_one variable %u\n",&ptr_one); printf("address of char_one var %u\n",&char_one); printf("address of char_two var %u\n",&char_two); return 0; }
output: address of 2636128020
address of variable b 2636128024
address of ptr variable 2636128000
address of ptr_one variable 2636128008
address of char_one var 2636128030
address of char_two var 2636128031
the c standard not require memory variables allocated contiguously. in fact, memory might not allocated @ if compiler decides can optimize keeping value in register instead.
if declare struct, contents of struct ordered way declare them, still might need consider how data aligned within struct - example, ints aligned on 4-byte boundaries in many architectures. if have:
struct foo { char a; int b; }
a guaranteed come before b in memory, still padded bytes between them keep correct alignment (so takes 8 bytes store struct, though "really" needs 5).
here's resource how structure alignment works:
Comments
Post a Comment