Thread: structure into string?

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    structure into string?

    I have a small question about the code's output.
    Code:
    main()
     {
        struct data {
           unsigned char first;
           unsigned short int second;
        } *info;
        char *msg;
        int i;
    
        msg = (char *) malloc(sizeof(struct data));
        info = (struct data *) msg;
    
        info->first = 0x4a;
        info->second = (unsigned short int)0x4d5e;
    
        for (i=0; i<sizeof(struct data); ++i)
           printf("%x\n", msg[i]);
     }
    the output i get is:

    4a
    0
    5e
    4d


    why do i have a 0 after 4a?




    thank you.
    Last edited by Devil Panther; 07-24-2004 at 01:39 AM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Because structures can have internal padding to preserve the alignment restrictions of the types
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    but what if i want to use this structure as part of an ip header (for example)... that 0 will corrupt the header structure, because the protocol does not have any padding in it.
    what can i do to remove the padding?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well there are no good ways - in fact if you want to be portable, you shouldn't use a structure at all, but compose the external structure one byte at a time

    Say
    Code:
    void make_ip_header ( unsigned char *ip_header_buff );
    But for quick hacks, try whatever compiler specific extension applies for packing structures
    VC++ would be #pragma pack(1)
    GCC would be __attribute__((packed))
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    compose the external structure one byte at a time
    you mean to read the structure byte by byte and create a string?

    But for quick hacks, try whatever compiler specific extension applies for packing structures
    VC++ would be #pragma pack(1)
    GCC would be __attribute__((packed))
    how does it work? where do i apply it?
    can you please give me an example for both platforms?


    thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Devil Panther
    how does it work? where do i apply it?
    can you please give me an example for both platforms?


    thank you.
    I sure do wonder why noone knows how to do any searching for answers themselves.
    Really, how hard is it to visit Google and click a few links? *shakes fist*

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >I sure do wonder why noone knows how to do any searching for answers themselves.
    Why do actual work when eventually someone will do it for you?

  8. #8
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    fair enough... just one question.

    once I change the way the structures are packed to byte by byte ( #pragma pack(1) ), could it effect something else?

    If so, how can I restore the old settings?
    Last edited by Devil Panther; 07-27-2004 at 12:07 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > could it effect something else?
    Everything else which follows it.

    Which if you'd read the rest of the manual, you would have seen
    #pragma pack() // restore compiler default
    #pragma push() // save current setting
    #pragma pop() // restore previously saved setting
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    is this code right?
    Code:
    #pragma pack( push, backup_id, 1 )    // saves the old value, and sets a new one
    
    . . .    // some code
    
    #pragma pack( pop, backup_id )          // sets back the old value
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  4. Replies: 12
    Last Post: 10-14-2003, 10:17 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM