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