c - Byte-addressed memory (Data alignment) -
i don't want homework. want tips on how can learn myself.
given byte-addressed memory writes lowest highest addresses. have c program has following declarations:
long int = 1; char c = 'x'; short int n = 10; short in m = 11; float f = 0.0;
in ia32 int 4 bytes, char 1 byte, short 2 byte , float 4 byte. how these declaration saved in memory likely? fill out following in hex digits.
how begin here? can give me tips? what's first thing have do?
edit: prof told me system ia32.
i assume variables declared , defined local (not static/global) variables, example:
int main() { long int = 1; char c = 'x'; short int n = 10; short in m = 11; float f = 0.0; }
if so, allocated on stack.
the principles of allocating local variables on stack same systems:
- stack grows high addresses low addresses
- the order of declaration of variables in program corresponds growth of stack
- each type has alignment - address of variable must divisible size (1
char
, 2short
, etc) - try waste little space possible
there conflict between principles (2) , (4) - if rearranging variables saves space, compiler it? guess can assume no (this "most likely" part in question).
the real situation more complicated (sizeof
not prescribed above numbers; alignment requirement not equal sizeof
; compiler may reuse space 2 different variables; etc) - have simplify if want solve homework in reasonable amount of time.
Comments
Post a Comment