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. Dynamic Memory Allocation

Dynamic Memory Allocation

Scheduled Pinned Locked Moved C / C++ / MFC
sysadmindata-structuresperformancehelpquestion
4 Posts 4 Posters 0 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.
  • S Offline
    S Offline
    sroberts82
    wrote on last edited by
    #1

    Hi folks, just wondering if someone could give me a hand with this. In my header file I have a struct as follows typedef struct someStruct{ char stuff; anotherStructure *moreStuff; char [20] // Packet padding }someStruct; This is to simulate a packet message being sent in a Network Simulator and so I need it to be a certain number a bytes. Heres the catch though,the moreStuff is a pointer to the start of an array which can have any number of elements, say each being x bytes. Previously I had just being padding messages with an array of chars to get it up to the required size, but now I need the size to be: fixed size + (no element in array)*x, i.e. its variable. Any ideas how I might sort it that this structure would be of this variable size? The number of elements in the array is not found out until run time. Thanks a million in advance for any help.

    B M A 3 Replies Last reply
    0
    • S sroberts82

      Hi folks, just wondering if someone could give me a hand with this. In my header file I have a struct as follows typedef struct someStruct{ char stuff; anotherStructure *moreStuff; char [20] // Packet padding }someStruct; This is to simulate a packet message being sent in a Network Simulator and so I need it to be a certain number a bytes. Heres the catch though,the moreStuff is a pointer to the start of an array which can have any number of elements, say each being x bytes. Previously I had just being padding messages with an array of chars to get it up to the required size, but now I need the size to be: fixed size + (no element in array)*x, i.e. its variable. Any ideas how I might sort it that this structure would be of this variable size? The number of elements in the array is not found out until run time. Thanks a million in advance for any help.

      B Offline
      B Offline
      badal_akr
      wrote on last edited by
      #2

      If you could have one more element in the moreStuff structure as like struct morestuff { int[xxx]; int size; } and update the size at the run time and can access this size by the parent structure someStruct to copy the desired amount of data. Hope it may help you some way Regards Anil Kumar

      1 Reply Last reply
      0
      • S sroberts82

        Hi folks, just wondering if someone could give me a hand with this. In my header file I have a struct as follows typedef struct someStruct{ char stuff; anotherStructure *moreStuff; char [20] // Packet padding }someStruct; This is to simulate a packet message being sent in a Network Simulator and so I need it to be a certain number a bytes. Heres the catch though,the moreStuff is a pointer to the start of an array which can have any number of elements, say each being x bytes. Previously I had just being padding messages with an array of chars to get it up to the required size, but now I need the size to be: fixed size + (no element in array)*x, i.e. its variable. Any ideas how I might sort it that this structure would be of this variable size? The number of elements in the array is not found out until run time. Thanks a million in advance for any help.

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        I think you will need to add another member to your someStruct struct to hold the size of the struct; you will set that value dynamically as needed.


        Maximilien Lincourt Your Head A Splode - Strong Bad

        1 Reply Last reply
        0
        • S sroberts82

          Hi folks, just wondering if someone could give me a hand with this. In my header file I have a struct as follows typedef struct someStruct{ char stuff; anotherStructure *moreStuff; char [20] // Packet padding }someStruct; This is to simulate a packet message being sent in a Network Simulator and so I need it to be a certain number a bytes. Heres the catch though,the moreStuff is a pointer to the start of an array which can have any number of elements, say each being x bytes. Previously I had just being padding messages with an array of chars to get it up to the required size, but now I need the size to be: fixed size + (no element in array)*x, i.e. its variable. Any ideas how I might sort it that this structure would be of this variable size? The number of elements in the array is not found out until run time. Thanks a million in advance for any help.

          A Offline
          A Offline
          Alex_Y
          wrote on last edited by
          #4

          typedef struct tagRS { int nCount; char szData[1]; } RS, *LPRS; ++++++++++ int nSizeYouNeedForData; LPRS pMem = malloc(sizeof(RS) + sizeof(char)*nSizeYouNeedForData)); pMem->nCount = nSizeYouNeedForData; memcpy(pMem->szData, From..., nSizeYouNeedForData); // Keep in mind element in char szData[1]; // should be always last ! // Code below is WRONG and BAD //typedef struct tagRS //{ //char szData[1]; //int nCount; //} RS, *LPRS; // GOOD typedef struct tagRS { int nCount; char szData[1]; } RS, *LPRS; :)

          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