Struct memory allocation question
-
I'm doing binary file input and I want to read 4 32-bit values in binary from a file with the read() command as elegantly as possible. Read gives me back a pointer to a char array of 16 chars (16 bytes of binary data representing the 4 32-bit values, called headerValPtr) So I have a simple struct: myStruct { int int float float } My data types in the struct are 32-bit unsigned integers and floating point vals. So I create a pointer to myStruct and allocate memory for it, call it sillyStruct. Will it work to then say sillyStruct = (myStruct*)headerValPtr? I just want to typecast the binary data into the 4 values in the struct. I don't have a good textbook handy to see how struct allocates memory. If the struct pointer simply points to a set of consecutive memory locations that hold all of the data, then I'm good. But I'm not 100% sure that is the case. If you want real code snippets I can throw something together, but this is more of a theoretical question so maybe you can live without? Thanks, --Seth
-
I'm doing binary file input and I want to read 4 32-bit values in binary from a file with the read() command as elegantly as possible. Read gives me back a pointer to a char array of 16 chars (16 bytes of binary data representing the 4 32-bit values, called headerValPtr) So I have a simple struct: myStruct { int int float float } My data types in the struct are 32-bit unsigned integers and floating point vals. So I create a pointer to myStruct and allocate memory for it, call it sillyStruct. Will it work to then say sillyStruct = (myStruct*)headerValPtr? I just want to typecast the binary data into the 4 values in the struct. I don't have a good textbook handy to see how struct allocates memory. If the struct pointer simply points to a set of consecutive memory locations that hold all of the data, then I'm good. But I'm not 100% sure that is the case. If you want real code snippets I can throw something together, but this is more of a theoretical question so maybe you can live without? Thanks, --Seth
While it's not an issue in your current structure, you may want to make sure you understand how structs are padded for packing/alignment internally and also how to override the default alignment/packing... Larry Osterman on Alignment, Part 1[^] Larry Osterman on Alignment, Part 2[^] align (C++)[^] #pragma pack[^] Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
While it's not an issue in your current structure, you may want to make sure you understand how structs are padded for packing/alignment internally and also how to override the default alignment/packing... Larry Osterman on Alignment, Part 1[^] Larry Osterman on Alignment, Part 2[^] align (C++)[^] #pragma pack[^] Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Maybe we need a purse seine[^] :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I'm doing binary file input and I want to read 4 32-bit values in binary from a file with the read() command as elegantly as possible. Read gives me back a pointer to a char array of 16 chars (16 bytes of binary data representing the 4 32-bit values, called headerValPtr) So I have a simple struct: myStruct { int int float float } My data types in the struct are 32-bit unsigned integers and floating point vals. So I create a pointer to myStruct and allocate memory for it, call it sillyStruct. Will it work to then say sillyStruct = (myStruct*)headerValPtr? I just want to typecast the binary data into the 4 values in the struct. I don't have a good textbook handy to see how struct allocates memory. If the struct pointer simply points to a set of consecutive memory locations that hold all of the data, then I'm good. But I'm not 100% sure that is the case. If you want real code snippets I can throw something together, but this is more of a theoretical question so maybe you can live without? Thanks, --Seth
No problem! A structure is basically a record of a specific size, and therefore you can save an exact copy in binary form and read it back (using sizeof(struct what_ever)). You must ensure that the program writing it and the program reading it is using the same byte alignment or the reader will be reading the wrong number of bytes. That is all there is to it, unless the structure contains a pointer to other data, in which case life get much more complicated. Note that the above applies mainly to the C language, because C++ can be bit trickery. If you are only using a structure in the manner of your example, then no problem.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
I'm doing binary file input and I want to read 4 32-bit values in binary from a file with the read() command as elegantly as possible. Read gives me back a pointer to a char array of 16 chars (16 bytes of binary data representing the 4 32-bit values, called headerValPtr) So I have a simple struct: myStruct { int int float float } My data types in the struct are 32-bit unsigned integers and floating point vals. So I create a pointer to myStruct and allocate memory for it, call it sillyStruct. Will it work to then say sillyStruct = (myStruct*)headerValPtr? I just want to typecast the binary data into the 4 values in the struct. I don't have a good textbook handy to see how struct allocates memory. If the struct pointer simply points to a set of consecutive memory locations that hold all of the data, then I'm good. But I'm not 100% sure that is the case. If you want real code snippets I can throw something together, but this is more of a theoretical question so maybe you can live without? Thanks, --Seth
search for structure packing, pack your structure for 1 byte. then any way you have all the data in an char array, so try this, struct MyStruct { char a;//1 byte int b;//4 byte int c;//4 byte }; int main() { MyStruct MyObj; MyStruct* l_Stemp = &MyObj; //You have an array of 9 bytes //let's say as l_Carr[9]; char* l_cPtr = reinterprete_cast(l_Stemp); //Here address of l_Stemp and l_cPtr is same so u can write values to l_cPtr using and read it from l_Stemp for(int i=0;i<9;i++) { *l_cPtr++=l_Carr[i]; } //Here u'd get ur struct back in the form of l_Stemp pointer, While using this u have to be very careful because, // pointer is poweful but power comes with responsibility return 0; } } // I did not compile this program if any compilation error is thr pls let me know
-
search for structure packing, pack your structure for 1 byte. then any way you have all the data in an char array, so try this, struct MyStruct { char a;//1 byte int b;//4 byte int c;//4 byte }; int main() { MyStruct MyObj; MyStruct* l_Stemp = &MyObj; //You have an array of 9 bytes //let's say as l_Carr[9]; char* l_cPtr = reinterprete_cast(l_Stemp); //Here address of l_Stemp and l_cPtr is same so u can write values to l_cPtr using and read it from l_Stemp for(int i=0;i<9;i++) { *l_cPtr++=l_Carr[i]; } //Here u'd get ur struct back in the form of l_Stemp pointer, While using this u have to be very careful because, // pointer is poweful but power comes with responsibility return 0; } } // I did not compile this program if any compilation error is thr pls let me know
abhijit bhopale wrote:
search for structure packing
You may want to do that yourself ;P By default.... struct MyStruct { char a;//1 byte int b;//4 byte int c;//4 byte }; ...is not a 9 byte structure. It's more like struct MyStruct { char a;//1 byte + 3 bytes padding int b;//4 byte int c;//4 byte }; Size is 12 bytes. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: