| | |
Struct with variable length arrays of struct
Thread Solved |
•
•
Join Date: Jul 2007
Posts: 19
Reputation:
Solved Threads: 0
I need to create a data structure of data structures, but I can know "how many structs I need" only at runtime.
Currently, the compiler gives an
I have to pass the address of this
Thanks a lot for reading my post. I graciously await your response.
c Syntax (Toggle Plain Text)
typedef struct tagPredefinedStruct{ int iSize; int iCount; } PREDEFINEDSTRUCT; int x; x = GetNumberofStructs(); //x is known only at runtime. typedef struct APIStruct { int iNumber; PREDEFINEDSTRUCT PreDefStruct[x]; int iDate; } APISTRUCT;
Currently, the compiler gives an
error C2057: expected constant expression for PreDefStruct[x].I have to pass the address of this
APISTRUCT structure to an API so I need all the memory allocated together in a chunk and can't use linked lists.Thanks a lot for reading my post. I graciously await your response.
You can't set the size of an array at run time. You have to dynamically create the array from a pointer.
C Syntax (Toggle Plain Text)
typedef struct tagPredefinedStruct{ int iSize; int iCount; } PREDEFINEDSTRUCT; typedef struct APIStruct { int iNumber; PREDEFINEDSTRUCT *PreDefStruct; int iDate; } APISTRUCT; APISTRUCT api; int x; x = GetNumberofStructs(); //x is known only at runtime. api.PreDefStruct = malloc( x * sizeof( PREDEFINEDSTRUCT ) );
The truth does not change according to our ability to stomach it.
•
•
Join Date: Jul 2007
Posts: 19
Reputation:
Solved Threads: 0
Thanks for the help. It compiles fine now. However, it crashes when I try to assign a value to the data structure.
Please help. Thanks.
My friend said another way is to give it an initial size of the array then change that size later. I don't know how to implement it though.
c Syntax (Toggle Plain Text)
. . . x = GetNumberofStructs(); //x = 3 in this case api.PreDefStruct = malloc( x * sizeof( PREDEFINEDSTRUCT ) ); api.PreDefStruct[0].iSize = 4; //here it crashes
Please help. Thanks.
My friend said another way is to give it an initial size of the array then change that size later. I don't know how to implement it though.
•
•
•
•
However, it crashes when I try to assign a value to the data structure.
•
•
•
•
My friend said another way is to give it an initial size of the array then change that size later.
The truth does not change according to our ability to stomach it.
Does this work for you?
C Syntax (Toggle Plain Text)
#include <stdio.h> typedef struct tagPredefinedStruct{ int iSize; int iCount; } PREDEFINEDSTRUCT; typedef struct APIStruct { int iNumber; PREDEFINEDSTRUCT *PreDefStruct; int iDate; } APISTRUCT; int main( void ) { APISTRUCT api; api.PreDefStruct = malloc( 3 * sizeof( PREDEFINEDSTRUCT ) ); api.PreDefStruct[0].iSize = 0; api.PreDefStruct[1].iSize = 1; api.PreDefStruct[2].iSize = 2; printf( "%d\n", api.PreDefStruct[0].iSize ); printf( "%d\n", api.PreDefStruct[1].iSize ); printf( "%d\n", api.PreDefStruct[2].iSize ); free( api.PreDefStruct ); return 0; }
The truth does not change according to our ability to stomach it.
> It's assigned a memory location of 0x6e615c31
Which looks a lot like a string fragment of "na\1"
The \ is telling, does it look like part of a filename perhaps?
I'm betting that you're copying a string into some other allocated memory, and this is where you observe the trashing of memory as a crash.
Which looks a lot like a string fragment of "na\1"
The \ is telling, does it look like part of a filename perhaps?
I'm betting that you're copying a string into some other allocated memory, and this is where you observe the trashing of memory as a crash.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
--
If your code lacks code tags, you will be IGNORED
•
•
Join Date: Jul 2007
Posts: 19
Reputation:
Solved Threads: 0
Thanks for all your help, but I don't think my problem can be solved using a struct in this manner.
What I need is all of the data items of the struct allocated together in one chunk. What I'm seeing from my debugger is that the 1st data item is allocated at:
What I need is all of the data items of the struct allocated together in one chunk. What I'm seeing from my debugger is that the 1st data item is allocated at:
0x0426c814, the 2nd at: 0x6674725c & so on... I read about a trick that makes everything together. Move the PreDefStruct member to be last, make it an array of 1, and then make the APISTRUCT variable dynamic. If you allocate the size of APISTRUCT plus the size of x PREDEFINEDSTRUCTs I think you get what you need.
C Syntax (Toggle Plain Text)
#include <stdio.h> typedef struct tagPredefinedStruct{ int iSize; int iCount; } PREDEFINEDSTRUCT; typedef struct APIStruct { int iNumber; int iDate; PREDEFINEDSTRUCT PreDefStruct[1]; } APISTRUCT; int main( void ) { APISTRUCT *api = malloc( sizeof( APISTRUCT ) + 3 * sizeof( PREDEFINEDSTRUCT ) ); api->PreDefStruct[0].iSize = 0; api->PreDefStruct[1].iSize = 1; api->PreDefStruct[2].iSize = 2; printf( "%d\n", api->PreDefStruct[0].iSize ); printf( "%d\n", api->PreDefStruct[1].iSize ); printf( "%d\n", api->PreDefStruct[2].iSize ); free( api ); return 0; }
The truth does not change according to our ability to stomach it.
![]() |
Other Threads in the C Forum
- Previous Thread: How to use admin passwords with "C"
- Next Thread: Cpu, virtual and physical memory
Views: 3527 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for C
api array arrays binary binarysearch c++ char code codes coke command conversion convert database decimal directory dude duplication dynamic ebook error exec fflush fgets file fork function functions getlasterror givemetehcodez grade graphics hardware help.forensic homework i/o inches input insert int integer km lazy line linked linkedlist linux list logical_drives loop malloc matrix memory messagebox motherboard mysql newbie no-effort output path performance pointer pointers prime printf problem process program programming read recursion recursive recv reverse roman2decimal scanf segmentationfault shape socketprograming spoonfeeding sql static steganography string strings strtok structures student studio system systemcall turbo turbo-c unix user variable voidmain() wab why-not-to-use-turbo windows






