puzzlecracker said:
Got Confused on the interview with memory alligment questions... PLEASE
HELP -- How much bytes of memory will structs below take on 32 bit
machine? What about 64 bit machine? Why is it different? (if it's
relevent, use standard size of datatypes)
[snip]
And you can't do this yourself?
Have you read the replies to a similar post?
a)
Code:
struct
{
short int a;
int b;
};
How big is a "short int"?
On 32-bit platforms, some have 16-bit shorts and some
have 32-bit short ints.
Let us assume that the processor only fetches at
32-bit address locations, that is every 4 bytes in
common notation. We'll also assume that a short int
is 16-bits (2 bytes).
To make life easier, we well declare a variable of
the above structure and allocate it at address 0x00.
Let us assign the value 0x1616 to the 'a' member and
0x32323232 to the 'b' member. We'll also use Big
Endian notation.
Address Value
------- -----
0100 1616 -- member 'a'
0102* 3232323232 -- member 'b'
0106* 0000 -- unknown value in memory.
Since this processor only fetches at 4 byte boundaries,
it will fetch at addresses 0x100 and 0x104. The address
of 0x102 cannot be accessed directly and is out of
alignment. The actual values for 'a' and 'b' might
be: a == 0x16163232, b == 0x32320000
The compiler is allowed to add padding between members
so that the values are placed at locations where the
processor can fetch them correctly:
0x100 1616 -- member 'a'
0x102 0000 -- padding from the compiler.
0x104 32323232 -- member ' b'
The above layout allows the processor to fetch the
value 0x1616 at location 0x100 and 0x32323232 at
location 0x104.
The exercise of a 64-bit machine is left to the
reader. Hint: a 64-bit machine likes to fetch
at every 8 bytes; and assume a short int is either
16 or 32 bits.
b)
Code:
struct{
short int a;
int b;
int somefunc();
virtual int func1();
};
Similar problem as above. However, this time use
pointers instead of the functions above. You could
assume that a 32-bit processor has a 32-bit pointer,
and a 64-bit processor has a 64-bit pointer.
c) Very Tricky (Hint: it is tricky with virtual function (or should I
say pointers to vtable...you should know something about vtables in
last but clever case))
Code:
struct{
short int a;
int b;
virtual int func_1();
virtual int func_2();
:
:
virtual int func_n();
};
VTables are not required per the standard. So this
question can be thrown out. But in the spirit of the
question, one could assume that the vtable is just an
array of pointers. See question 'b'.
For some extra practice, consider these structs:
struct A
{
char c;
short int si;
int j;
char d
int i;
};
struct B
{
int i;
short int si;
char c;
char d;
int j;
};
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library