Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Struct memory allocation question

Struct memory allocation question

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structuresperformance
7 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thespiff
    wrote on last edited by
    #1

    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

    M J N 3 Replies Last reply
    0
    • T thespiff

      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

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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:

      L 1 Reply Last reply
      0
      • M Mark Salsbery

        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:

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        It's like trying to herd a pack of fish!

        M 1 Reply Last reply
        0
        • L led mike

          It's like trying to herd a pack of fish!

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Maybe we need a purse seine[^] :)

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          1 Reply Last reply
          0
          • T thespiff

            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

            J Offline
            J Offline
            John R Shaw
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • T thespiff

              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

              N Offline
              N Offline
              Neo Andreson
              wrote on last edited by
              #6

              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

              M 1 Reply Last reply
              0
              • N Neo Andreson

                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

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                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:

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups