windows vista - memory allocation in C -
i have question regarding memory allocation order. in following code allocate in loop 4 strings. when print addresses don't seem allocated 1 after other... doing wrong or sort of defense mechanism implemented os prevent possible buffer overflows? (i use windows vista).
thank you.
char **stringarr; int size=4, i; stringarr=(char**)malloc(size*sizeof(char*)); (i=0; i<size; i++) stringarr[i]=(char*)malloc(10*sizeof(char)); strcpy(stringarr[0], "abcdefgh"); strcpy(stringarr[1], "good-luck"); strcpy(stringarr[2], "mully"); strcpy(stringarr[3], "stam"); (i=0; i<size; i++) { printf("%s\n", stringarr[i]); printf("%d %u\n\n", &(stringarr[i]), stringarr[i]); }
output:
abcdefgh 9650064 9650128
good-luck 9650068 9638624
mully 9650072 9638680
stam 9650076 9638736
typically when request memory through malloc()
, c runtime library round size of request minimum allocation size. makes sure that:
- the runtime library has room bookkeeping information
- it's more efficient runtime library manage allocated blocks multiples of size (such 16 bytes)
however, these implementation details , can't rely on particular behaviour of malloc()
.
Comments
Post a Comment