disregard: sizeof struct returning unexpected results

Discussion in 'C Programming' started by Sean, Aug 20, 2004.

  1. Sean

    Sean Guest

    I perused the archives of comp.lang.c and found some answers related
    to addressability of struct elements (only in arrays?), and will
    probably proceed by using byte or char arrays filled with raw data and
    then converting to the expected types. Unless anyone has any helpful
    or enlightening comments, I will leave this issue closed for now.

    Thanks again for the time!
     
    Sean, Aug 20, 2004
    #1
    1. Advertising

  2. Sean

    Jack Klein Guest

    On 20 Aug 2004 09:54:33 -0700, (Sean) wrote in
    comp.lang.c:

    > I perused the archives of comp.lang.c and found some answers related
    > to addressability of struct elements (only in arrays?), and will
    > probably proceed by using byte or char arrays filled with raw data and
    > then converting to the expected types. Unless anyone has any helpful
    > or enlightening comments, I will leave this issue closed for now.
    >
    > Thanks again for the time!


    No, it has nothing at all to do with whether you define a single item
    of a type or an array of them. The distance between one member of an
    array of type T and the next is sizeof(type T), and the size of a
    free-standing object of type T is also sizeof(type T).

    Using an array of unsigned chars is a good idea, but be careful about
    "converting" to the "expected" types. On some platforms the alignment
    imposed by the compiler is required by the underlying processor
    hardware, and violating it can have serious consequences.

    Assuming sizeof(short) is 2 and sizeof(int) is 4, then you could use
    something like this to pass the data around:

    unsigned char data [6];

    But this is a bad idea:

    unsigned short us;
    unsigned int ui;
    unsigned char data [6];

    /* get data into 'data' */

    us = *(unsigned short *)data;
    ui = *(unsigned long *)(data + 2);

    ....on some processors this produces code that runs more slowly but
    correctly. On others, including newer RISC architectures, either you
    get the wrong value or a hardware trap that terminates the program.

    The safe way to transfer the data is:

    memcpy(&us, data, 2);
    memcpy(&ui, data + 2, 4);

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Aug 21, 2004
    #2
    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. Steve Richter

    Why printing disregard <table width="100%">?

    Steve Richter, May 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    461
    =?Utf-8?B?bGF0aGEgdmFsbGluYXlhZ2Ft?=
    May 5, 2005
  2. D R E
    Replies:
    0
    Views:
    295
    D R E
    Jul 13, 2004
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  4. Pallav

    disregard post on sine table lookup

    Pallav, Jun 30, 2004, in forum: C Programming
    Replies:
    0
    Views:
    297
    Pallav
    Jun 30, 2004
  5. Sean
    Replies:
    10
    Views:
    631
    Keith Thompson
    Aug 26, 2004
Loading...

Share This Page