What about linked lists? . . .
EDIT: Or chars, ints, and doubles?
What about linked lists? . . .
EDIT: Or chars, ints, and doubles?
> 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.
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.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
I think its an example
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:
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.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); }
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.*
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
Yeah this works for this situation, but my actual implementation would be something more like.
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.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;
Im sure this type of serial data packing happens all the time. What is typically done?
You will have even bigger issue using struct here - due to endian problem...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.
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