Gap of two 0x00 between structs.....

Discussion in 'C Programming' started by Don, Oct 1, 2003.

  1. Don

    Don Guest

    Hi NG. I have the code below my question.
    I would like to copy the content of measuredata to the MyBuffer from place
    nr 20 (MyBuffer[19]). The problem with this code is that between the two
    structs I get two empty bytes :-(. Like measuredata.struct1.measure13 are
    placed at MyBuffer[19+786] and when I print this it displays oxBF fine, but
    when I print
    MyBuffer[19+787],MyBuffer[19+788],MyBuffer[19+789] and MyBuffer[19+790], it
    displays 0x00, 0x00, 0x12 and 0x34 :-(. And MyBuffer[19+791] and
    MyBuffer[19+792] holds the rest of the integer 0x56 and 0x78. So I get these
    two 0x00 and 0x00.....Can I workaround this or is this something to do with
    the struct type???

    Best Regards
    Don



    unsigned char MyBuffer[2000];

    struct mystruct1{
    unsigned char measure1[256];
    unsigned short measure2[256];
    unsigned char measure3;
    unsigned short measure4;
    unsigned short measure5;
    unsigned short measure6;
    unsigned short measure7;
    unsigned short measure8;
    unsigned short measure9;
    unsigned char measure10;
    unsigned char measure11;
    unsigned char measure12;
    unsigned char measure13;
    };

    struct mystruct2{
    unsigned int measure14;
    unsigned int measure15;
    unsigned short measure16;
    unsigned short measure17;
    unsigned short measure18;
    unsigned short measure19;
    unsigned short measure20;
    unsigned short measure21;

    unsigned int measure22;
    unsigned int measure23;
    unsigned int measure24;
    unsigned int measure25;
    unsigned int measure26;
    unsigned int measure27;

    unsigned int measure28;
    unsigned int measure29;
    unsigned int measure30;
    unsigned int measure31;
    };


    struct measurement_data{
    struct mystruct1 struct1;
    struct mystruct2 struct2;
    }measuredata;



    int main(){
    int i= 0;

    for(i=0;i<200;i++)
    measuredata.struct1.measure1 = 0x55;

    for(i=0;i<100;i++)
    measuredata.struct1.measure2 = 0xFF11;

    measuredata.struct1.measure9 = 0x1133;

    measuredata.struct2.measure16 = 0x12345678;
    measuredata.struct2.measure23 = 0xFFFFFFFF;
    measuredata.struct2.measure26 = 0x08080808;
    measuredata.struct2.measure30 = 0x00000001;
    measuredata.struct2.measure31 = 0xDEADBEEF;

    //The transition between struct1 and struct2
    measuredata.struct1.measure13 = 0xBF;
    measuredata.struct2.measure14 = 0x12345678;

    //Copy measuredata struct with variable values to the MyBuffer
    memcpy(&MyBuffer[19], (unsigned char *) &measuredata, sizeof(struct
    measurement_data));

    return 0;

    }
    Don, Oct 1, 2003
    #1
    1. Advertising

  2. Don wrote:
    > Hi NG. I have the code below my question.
    > I would like to copy the content of measuredata to the MyBuffer from place
    > nr 20 (MyBuffer[19]). The problem with this code is that between the two
    > structs I get two empty bytes :-(. Like measuredata.struct1.measure13 are
    > placed at MyBuffer[19+786] and when I print this it displays oxBF fine, but
    > when I print
    > MyBuffer[19+787],MyBuffer[19+788],MyBuffer[19+789] and MyBuffer[19+790], it
    > displays 0x00, 0x00, 0x12 and 0x34 :-(. And MyBuffer[19+791] and
    > MyBuffer[19+792] holds the rest of the integer 0x56 and 0x78. So I get these
    > two 0x00 and 0x00.....Can I workaround this or is this something to do with
    > the struct type???
    >
    > Best Regards
    > Don
    >
    >
    >
    > unsigned char MyBuffer[2000];
    >


    The compiler is allowed to add padding bytes between fields in a
    structure and also at the end of the structure.

    Some compilers offer a "packed" option which forces the compiler
    remove the padding (I've had problems with this option on
    different compilers). This option is NOT standard.

    Another option is to use a pointer to an unsigned character
    then cast to whatever type. In this manner, you can access
    memory at the finest detail without messing with any padding
    that _may_ be imposed by the compiler.

    Just remember that the size of a structure may not be equal
    to the size of its fields.

    --
    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.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Oct 1, 2003
    #2
    1. Advertising

  3. Don

    Jack Klein Guest

    On Wed, 1 Oct 2003 15:43:25 +0200, "Don" <> wrote in
    comp.lang.c:

    > Hi NG. I have the code below my question.
    > I would like to copy the content of measuredata to the MyBuffer from place
    > nr 20 (MyBuffer[19]). The problem with this code is that between the two
    > structs I get two empty bytes :-(. Like measuredata.struct1.measure13 are
    > placed at MyBuffer[19+786] and when I print this it displays oxBF fine, but
    > when I print
    > MyBuffer[19+787],MyBuffer[19+788],MyBuffer[19+789] and MyBuffer[19+790], it
    > displays 0x00, 0x00, 0x12 and 0x34 :-(. And MyBuffer[19+791] and
    > MyBuffer[19+792] holds the rest of the integer 0x56 and 0x78. So I get these
    > two 0x00 and 0x00.....Can I workaround this or is this something to do with
    > the struct type???
    >
    > Best Regards
    > Don


    [code snipped]

    http://www.eskimo.com/~scs/C-faq/q2.13.html

    --
    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++ ftp://snurse-l.org/pub/acllc-c++/faq
    Jack Klein, Oct 1, 2003
    #3
  4. Don

    Derk Gwen Guest

    # MyBuffer[19+787],MyBuffer[19+788],MyBuffer[19+789] and MyBuffer[19+790], it
    # displays 0x00, 0x00, 0x12 and 0x34 :-(. And MyBuffer[19+791] and
    # MyBuffer[19+792] holds the rest of the integer 0x56 and 0x78. So I get these
    # two 0x00 and 0x00.....Can I workaround this or is this something to do with
    # the struct type???

    C structs cannot be portably used to stencil a structure on a byte stream. To be
    portable you need to move bytes to and from a struct yourself. Nonportably your
    compiler may have pragmas or other means to force byte alignment; consult your
    compiler documentation if you want to go this route.

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    Who's leading this mob?
    Derk Gwen, Oct 1, 2003
    #4
    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. chlori

    Gap in IE, no gap in FF

    chlori, Jan 19, 2006, in forum: HTML
    Replies:
    1
    Views:
    436
    kchayka
    Jan 19, 2006
  2. Joseph Suprenant

    printing 0x00 in ascii

    Joseph Suprenant, Nov 20, 2003, in forum: C++
    Replies:
    3
    Views:
    763
    Noah Roberts
    Nov 20, 2003
  3. baumann@pan

    printf("%#04x\n", 0); print 0000 not 0x00

    baumann@pan, Jul 1, 2005, in forum: C Programming
    Replies:
    8
    Views:
    607
    Lawrence Kirby
    Jul 4, 2005
  4. Vallabha
    Replies:
    3
    Views:
    266
    Gordon Beaton
    Oct 7, 2008
  5. Earle Clubb
    Replies:
    2
    Views:
    86
    Robert Klemme
    Apr 24, 2007
Loading...

Share This Page