size of memory in C -- allocation

Discussion in 'C Programming' started by puzzlecracker, Jan 18, 2005.

  1. 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)


    a)

    Code:
    struct
    {
    short int a;
    int b;
    };





    b)
    Code:
    struct{
    short int a;
    int b;
    int somefunc();
    virtual int func1();
    };






    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();
    };
    puzzlecracker, Jan 18, 2005
    #1
    1. Advertising

  2. puzzlecracker

    Mike Wahler Guest

    "puzzlecracker" <> wrote in message
    news:...
    > 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?


    This depends entirely upon the compiler. The language
    only requires that the memory is sufficient to store
    all the members.

    > Why is it different?


    Because machines are different (and thus compilers for
    them will do things differently).

    > (if it's
    > relevent, use standard size of datatypes)


    Data types (except for the character types) don't have
    a 'standard size', only a 'minimum size'. The character
    types all have a size of one byte, by definition.

    > a)
    >
    > Code:
    > struct
    > {
    > short int a;
    > int b;
    > };
    >
    > b)
    > Code:
    > struct{
    > short int a;
    > int b;
    > int somefunc();
    > virtual int func1();
    > };
    >
    > 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))


    C does not have virtual functions. C++ does.
    However, 'vtables' are not part of the C++ language,
    but an implementation detail, not required to be
    used at all.

    Are you aware that C and C++ are two separate, distinct
    languages, and that while they share similar syntax,
    there are significant differences? Does the interviewer
    know this?

    -Mike
    Mike Wahler, Jan 18, 2005
    #2
    1. Advertising

  3. puzzlecracker

    pete Guest

    puzzlecracker wrote:

    > virtual function


    Try news:comp.lang.c++

    --
    pete
    pete, Jan 18, 2005
    #3
  4. puzzlecracker wrote:
    > 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
    Thomas Matthews, Jan 18, 2005
    #4
  5. Let me know if i solved your examples right, on 32 and 64 bit machine.
    If not, please explain where I made a mistake.

    >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.


    since short int is 2 bytes and int is 4 bytes, it will occupy a block
    of 8 byte, so the total size is 8 byte.


    2)
    >struct A
    >{
    > char c;
    >short int si;
    > int j;
    > char d
    > int i;
    >};


    32 bit machine:
    -----------------
    1st block: s, si +padding = 4 bytes
    2nd block: j = 4 bytes
    3rd block: d+padding = 4 bytes
    4th block: i = 4 bytes
    -----------------------------------
    TOTAL: = 16 bytes


    64 bit Machine
    ----------------
    1st block: s,si j+d = 8byte
    2nd block i+padding = 8byte
    ------------------------------
    TOTAL: 16 bytes



    2)
    >struct B
    >{
    > int i;
    > short int si;
    > char c;
    > char d;
    > int j;
    >};


    32 bit machine
    --------------
    1st block: i = 4 bytes
    2rd block: s1, c d = 4 bytes
    4th block j = 4 bytes
    -----------------------------------
    TOTAL: 12 bytes

    64 bit Machine
    ----------------
    1st block: i, si c + d = 8 byte
    2nd block j = 8 byte
    ---------------------------------
    TOTAL: = 16 bytes

    =========================================================


    small question: virutal functions, regular fucntions and inline
    functions take the same space (ex: 4 bytes or 8 bytes 32 and 64 bits
    respectively)? According to alignement rules: compiler is tring to fit
    as many variable (data types) as possible in each block for as long as
    it fits entirely or else adds padding... is that the correct way of
    thinking?



    Thanks...

    ...Puzzlecracker!
    puzzlecracker, Jan 18, 2005
    #5
  6. On 18 Jan 2005 10:49:11 -0800, puzzlecracker
    <> wrote:

    > Got Confused on the interview with memory alligment questions... PLEASE
    > HELP -- How much bytes of memory will structs below take on 32 bit
    > machine?


    Some machine and compiler dependant number. Since you don't say what
    machine and compiler it is impossible to give an answer.

    > What about 64 bit machine? Why is it different? (if it's
    > relevent, use standard size of datatypes)


    > struct
    > {
    > short int a;
    > int b;
    > };


    No fewer than 32 bits, could be quite a lot more. The standard requires
    a short int to hold at least 16 bits and an int to hold at least 16
    bits. On a 32 bit machine an int may commonly be 32 bits, on a 64 bit
    machine it could be 32 or 64 bits commonly. Then you may have alignment
    requirements, some machines can only access 32 bit variables starting on
    a 32 bit boundary so padding will have to be added between the
    variables. Similarly for 64 bit variables. Then there may be a
    requirement that all structs are padded to (say) a 128 bit boundary.

    > struct{
    > short int a;
    > int b;
    > int somefunc();
    > virtual int func1();
    > };


    Zero bits, it has a compilation error ('virtual' is not a C keyword).

    > 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))


    What's a vtable in C? The word doesn't exist in ISO/IEC-9899 (the C
    specification). Nor does 'virtual', so the answer is that it takes no
    space again because it won't compile.

    Chris C
    Chris Croughton, Jan 18, 2005
    #6
  7. puzzlecracker

    CBFalconer Guest

    puzzlecracker wrote:
    >
    > 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)
    >
    > a)
    >
    > Code:
    > struct
    > {
    > short int a;
    > int b;
    > };


    They forgot to give the struct a label. The answer then is

    sizeof(struct x)

    which may very well be 2 * sizeof(int)

    and you cannot be any more accurate without seeing limits.h.
    Otherwise it is just a guess. Even with limits.h it will be a
    guess, but an educated one. sizeof is the only accurate means of
    measuring it.

    >
    > b)
    > Code:
    > struct{
    > short int a;
    > int b;
    > int somefunc();
    > virtual int func1();
    > };
    >
    > 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();
    > };


    The "virtual" is a syntax error. Again the struct is missing a
    label, and the answer to a applies.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Jan 18, 2005
    #7
  8. puzzlecracker Jan 18, 12:40 pm show options

    Newsgroups: comp.lang.c
    From: "puzzlecracker" <> - Find messages by this
    author
    Date: 18 Jan 2005 12:40:16 -0800
    Local: Tues, Jan 18 2005 12:40 pm
    Subject: Re: size of memory in C -- allocation
    Reply | Reply to Author | Forward | Print | Individual Message | Show
    original | Remove | Report Abuse

    Let me know if i solved your examples right, on 32 and 64 bit machine.
    If not, please explain where I made a mistake.



    >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.



    since short int is 2 bytes and int is 4 bytes, it will occupy a block
    of 8 byte, so the total size is 8 byte.

    2)



    >struct A
    >{
    > char c;
    >short int si;
    > int j;
    > char d
    > int i;
    >};



    32 bit machine:
    -----------------
    1st block: s, si +padding = 4 bytes
    2nd block: j = 4 bytes
    3rd block: d+padding = 4 bytes
    4th block: i = 4 bytes
    -----------------------------------
    TOTAL: = 16 bytes

    64 bit Machine
    ----------------
    1st block: s,si j+d = 8byte
    2nd block i+padding = 8byte
    ------------------------------
    TOTAL: 16 bytes


    2)



    >struct B
    >{
    > int i;
    > short int si;
    > char c;
    > char d;
    > int j;
    >};



    32 bit machine
    --------------
    1st block: i = 4 bytes
    2rd block: s1, c d = 4 bytes
    4th block j = 4 bytes
    -----------------------------------
    TOTAL: 12 bytes

    64 bit Machine
    ----------------
    1st block: i, si c + d = 8 byte
    2nd block j = 8 byte
    ---------------------------------
    TOTAL: = 16 bytes


    =========================================================


    small question: virutal functions, regular fucntions and inline
    functions take the same space (ex: 4 bytes or 8 bytes 32 and 64 bits
    respectively)? According to alignement rules: compiler is tring to fit
    as many variable (data types) as possible in each block for as long as
    it fits entirely or else adds padding... is that the correct way of
    thinking?


    Thanks...


    ...Puzzlecracker!
    puzzlecracker, Jan 18, 2005
    #8
  9. Please let me know if I solved these ones correctly. thx

    >struct A
    >{
    > char c;
    >short int si;
    > int j;
    > char d
    > int i;
    >};



    32 bit machine:
    -----------------
    1st block: s, si +padding = 4 bytes
    2nd block: j = 4 bytes
    3rd block: d+padding = 4 bytes
    4th block: i = 4 bytes
    -----------------------------------
    TOTAL: = 16 bytes

    64 bit Machine
    ----------------
    1st block: s,si j+d = 8byte
    2nd block i+padding = 8byte
    ------------------------------
    TOTAL: 16 bytes


    2)



    >struct B
    >{
    > int i;
    > short int si;
    > char c;
    > char d;
    > int j;
    >};



    32 bit machine
    --------------
    1st block: i = 4 bytes
    2rd block: s1, c d = 4 bytes
    4th block j = 4 bytes
    -----------------------------------
    TOTAL: 12 bytes

    64 bit Machine
    ----------------
    1st block: i, si c + d = 8 byte
    2nd block j = 8 byte
    ---------------------------------
    TOTAL: = 16 bytes


    =========================================================


    small question: virutal functions, regular fucntions and inline
    functions take the same space (ex: 4 bytes or 8 bytes 32 and 64 bits
    respectively)? According to alignement rules: compiler is tring to fit
    as many variable (data types) as possible in each block for as long as
    it fits entirely or else adds padding... is that the correct way of
    thinking?


    Thanks...


    ...Puzzlecracker!
    puzzlecracker, Jan 18, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gianni Mariani
    Replies:
    0
    Views:
    476
    Gianni Mariani
    Aug 20, 2003
  2. baobaoba
    Replies:
    1
    Views:
    866
    tom_usenet
    Aug 21, 2003
  3. Jinming Xu
    Replies:
    2
    Views:
    2,997
    Craig Ringer
    Jan 21, 2005
  4. Ken
    Replies:
    24
    Views:
    3,842
    Ben Bacarisse
    Nov 30, 2006
  5. chris
    Replies:
    6
    Views:
    974
    chris
    Oct 28, 2005
Loading...

Share This Page