Thread: Are structs sequential in memory

  1. #16
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    What about linked lists? . . .

    EDIT: Or chars, ints, and doubles?

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > What about linked lists?

    What about them? Linked lists don't have the same locality of references that arrays do, and to nothing to solve the padding issue that the op complained about. Although frankly I'm not sure if arrays solve the problem either.

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    8
    Quote Originally Posted by Perspective View Post
    "I have a bunch of elements of the same type that I want to store in contiguous memory..."

    I don't understand why this converstation is even about structs, use an array.
    Because with a struct you can name the elements of the array. Instead of having to figure out which byte offset corresponds to what value.

  4. #19
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Supadude View Post
    If I define a struct as such:

    Code:
    struct mystruct
    {
        char one;
        char two;
        char three;
        char four;
    };
    Is it guarenteed to be sequential in memory such that I can do an assignment like this:
    Code:
    mystruct s;
    char *p = (char *) &s;
    If you do something like that you'll still need indices to access characters at p. So what's the gain over using an array in the first place? In your hypothetical example the members are named after numbers. Are you sure that using numeric indices is not going to be easier?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #20
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I think its an example

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    8
    Well what I am really trying to figure out is what is an efficient and non error prone way for packing serial streams of data? I will show you how I am doing it now:

    Code:
    #define BIN_TO_NMEA_MSG_ID 0x81
    #define DISABLE_NMEA_DEBUG 1
    #define GGA_MSG_RATE 0 // Do not send messages - will be polled
    #define GGA_CHECKSUM 0 // No checksum
    #define GLL_MSG_RATE 0
    #define GLL_CHECKSUM 0
    #define GSA_MSG_RATE 0
    #define GSA_CHECKSUM 0
    #define GSV_MSG_RATE 0
    #define GSV_CHECKSUM 0
    #define RMC_MSG_RATE 0
    #define RMC_CHECKSUM 0
    #define VTG_MSG_RATE 0
    #define VTG_CHECKSUM 0
    #define MSS_MSG_RATE 0
    #define MSS_CHECKSUM 0
    #define ZDA_MSG_RATE 0
    #define ZDA_CHECKSUM 0
    #define NMEA_BAUDRATE 38400 // 38400, 19200, 9600, 4800, 2400 only
    
    
    void GPS_bin_to_NMEA(void)
    {
      byte tmp_msg[26]; //Should be 24 bytes + extra padding
      
      tmp_msg[0] = BIN_TO_NMEA_MSG_ID;
      tmp_msg[1] = DISABLE_NMEA_DEBUG;
      tmp_msg[2] = GGA_MSG_RATE; 
      tmp_msg[3] = GGA_CHECKSUM; 
      tmp_msg[4] = GLL_MSG_RATE;
      tmp_msg[5] = GLL_CHECKSUM;
      tmp_msg[6] = GSA_MSG_RATE;
      tmp_msg[7] = GSA_CHECKSUM;
      tmp_msg[8] = GSV_MSG_RATE;
      tmp_msg[9] = GSV_CHECKSUM;
      tmp_msg[10] = RMC_MSG_RATE;
      tmp_msg[11] = RMC_CHECKSUM;
      tmp_msg[12] = VTG_MSG_RATE;
      tmp_msg[13] = VTG_CHECKSUM;
      tmp_msg[14] = MSS_MSG_RATE;
      tmp_msg[15] = MSS_CHECKSUM;
      tmp_msg[16] = 0;  // Not used
      tmp_msg[17] = 0;  // Not used
      tmp_msg[18] = ZDA_MSG_RATE;
      tmp_msg[19] = ZDA_CHECKSUM;
      tmp_msg[20] = 0;  // Not used
      tmp_msg[21] = 0;  // Not used
      tmp_msg[22] = NMEA_BAUDRATE >> 8;
      tmp_msg[23] = NMEA_BAUDRATE && 0x00FF;
    
      GPS_send_bin_msg(tmp_msg, 24);
    }
    I am doing a lot of repitition in this and I have to specify every byte. But I cant think of a better way to do it. Given in the example most of the #defines are zero, but this could change in the future.

  7. #22
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Ever thought of using an initialized array? Perhaps one that is static and const within the function?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #23
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Supadude View Post
    Because with a struct you can name the elements of the array. Instead of having to figure out which byte offset corresponds to what value.
    With the array you actually can do the same:
    Code:
    typedef enum
    {
    one = 0,
    two,
    three,
    four} INDEXES;
    
    char values[4];
    
    values[one], etc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #24
    Registered User
    Join Date
    Jun 2005
    Posts
    8
    Quote Originally Posted by vart View Post
    With the array you actually can do the same:
    Code:
    typedef enum
    {
    one = 0,
    two,
    three,
    four} INDEXES;
    
    char values[4];
    
    values[one], etc
    Yeah this works for this situation, but my actual implementation would be something more like.

    Code:
    typedef enum
    { 
      BIN_TO_NMEA_MSG_ID = 0,
      DISABLE_NMEA_DEBUG,
      GGA_MSG_RATE,
      ...
    };
    
    char array[3];
    array[BIN_TO_NMEA_MSG_ID] = 1;
    Which works, but it seems awkward to me. And then the issue arises with what happens when I have a 16 bit or larger value that needs to be masked and shifted into multiple bytes.

    Im sure this type of serial data packing happens all the time. What is typically done?

  10. #25
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And then the issue arises with what happens when I have a 16 bit or larger value that needs to be masked and shifted into multiple bytes.
    You will have even bigger issue using struct here - due to endian problem...
    So you anyway need to use char array with masking and shifting

    PS. In my expirience - general approach is to have a struct for the most part of the code

    And 2 functions pack/unpack that will convert struct to/from byte-array using the correct endian order and removing paddings (also packing several values into one byte where possible - using 20 bits instead of 32 for example...) and this packed byte array is transferred over the network
    Last edited by vart; 05-16-2007 at 11:48 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #26
    Registered User
    Join Date
    Jun 2005
    Posts
    8
    Quote Originally Posted by vart View Post
    You will have even bigger issue using struct here - due to endian problem...
    So you anyway need to use char array with masking and shifting

    PS. In my expirience - general approach is to have a struct for the most part of the code

    And 2 functions pack/unpack that will convert struct to/from byte-array using the correct endian order and removing paddings (also packing several values into one byte where possible - using 20 bits instead of 32 for example...) and this packed byte array is transferred over the network
    This makes sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM
  5. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 03:53 PM