943,876 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 7235
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 17th, 2007
0

Struct with variable length arrays of struct

Expand Post »
I need to create a data structure of data structures, but I can know "how many structs I need" only at runtime.

  1. typedef struct tagPredefinedStruct{
  2. int iSize;
  3. int iCount;
  4. } PREDEFINEDSTRUCT;
  5.  
  6. int x;
  7. x = GetNumberofStructs(); //x is known only at runtime.
  8.  
  9. typedef struct APIStruct {
  10. int iNumber;
  11. PREDEFINEDSTRUCT PreDefStruct[x];
  12. int iDate;
  13. } 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mohammadalipak is offline Offline
19 posts
since Jul 2007
Jul 17th, 2007
0

Re: Struct with variable length arrays of struct

You can't set the size of an array at run time. You have to dynamically create the array from a pointer.
  1. typedef struct tagPredefinedStruct{
  2. int iSize;
  3. int iCount;
  4. } PREDEFINEDSTRUCT;
  5.  
  6. typedef struct APIStruct {
  7. int iNumber;
  8. PREDEFINEDSTRUCT *PreDefStruct;
  9. int iDate;
  10. } APISTRUCT;
  11.  
  12. APISTRUCT api;
  13. int x;
  14.  
  15. x = GetNumberofStructs(); //x is known only at runtime.
  16. api.PreDefStruct = malloc( x * sizeof( PREDEFINEDSTRUCT ) );
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Jul 17th, 2007
0

Re: Struct with variable length arrays of struct

Thanks for the help. It compiles fine now. However, it crashes when I try to assign a value to the data structure.
  1. .
  2. .
  3. .
  4. x = GetNumberofStructs(); //x = 3 in this case
  5. api.PreDefStruct = malloc( x * sizeof( PREDEFINEDSTRUCT ) );
  6. 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.

Ali
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mohammadalipak is offline Offline
19 posts
since Jul 2007
Jul 17th, 2007
0

Re: Struct with variable length arrays of struct

Quote ...
However, it crashes when I try to assign a value to the data structure.
Check the pointer to see if it's 0. If it is then malloc failed to allocate the memory.

Quote ...
My friend said another way is to give it an initial size of the array then change that size later.
You can't. Array sizes have to be constant and they can't be changed. A variable sized array is what I showed you.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Jul 17th, 2007
0

Re: Struct with variable length arrays of struct

The pointer is not zero. It's assigned a memory location of 0x6e615c31.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mohammadalipak is offline Offline
19 posts
since Jul 2007
Jul 17th, 2007
0

Re: Struct with variable length arrays of struct

Does this work for you?
  1. #include <stdio.h>
  2.  
  3. typedef struct tagPredefinedStruct{
  4. int iSize;
  5. int iCount;
  6. } PREDEFINEDSTRUCT;
  7.  
  8. typedef struct APIStruct {
  9. int iNumber;
  10. PREDEFINEDSTRUCT *PreDefStruct;
  11. int iDate;
  12. } APISTRUCT;
  13.  
  14. int main( void ) {
  15. APISTRUCT api;
  16.  
  17. api.PreDefStruct = malloc( 3 * sizeof( PREDEFINEDSTRUCT ) );
  18.  
  19. api.PreDefStruct[0].iSize = 0;
  20. api.PreDefStruct[1].iSize = 1;
  21. api.PreDefStruct[2].iSize = 2;
  22.  
  23. printf( "%d\n", api.PreDefStruct[0].iSize );
  24. printf( "%d\n", api.PreDefStruct[1].iSize );
  25. printf( "%d\n", api.PreDefStruct[2].iSize );
  26.  
  27. free( api.PreDefStruct );
  28.  
  29. return 0;
  30. }
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Jul 17th, 2007
0

Re: Struct with variable length arrays of struct

No, it's not working. It crashes on line 19 of your proposed code: api.PreDefStruct[0].iSize = 0;. Thanks for continuing to help. And so promptly.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mohammadalipak is offline Offline
19 posts
since Jul 2007
Jul 17th, 2007
1

Re: Struct with variable length arrays of struct

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 18th, 2007
0

Re: Struct with variable length arrays of struct

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: 0x0426c814, the 2nd at: 0x6674725c & so on...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mohammadalipak is offline Offline
19 posts
since Jul 2007
Jul 18th, 2007
0

Re: Struct with variable length arrays of struct

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.
  1. #include <stdio.h>
  2.  
  3. typedef struct tagPredefinedStruct{
  4. int iSize;
  5. int iCount;
  6. } PREDEFINEDSTRUCT;
  7.  
  8. typedef struct APIStruct {
  9. int iNumber;
  10. int iDate;
  11. PREDEFINEDSTRUCT PreDefStruct[1];
  12. } APISTRUCT;
  13.  
  14. int main( void ) {
  15. APISTRUCT *api = malloc( sizeof( APISTRUCT ) + 3 * sizeof( PREDEFINEDSTRUCT ) );
  16.  
  17. api->PreDefStruct[0].iSize = 0;
  18. api->PreDefStruct[1].iSize = 1;
  19. api->PreDefStruct[2].iSize = 2;
  20.  
  21. printf( "%d\n", api->PreDefStruct[0].iSize );
  22. printf( "%d\n", api->PreDefStruct[1].iSize );
  23. printf( "%d\n", api->PreDefStruct[2].iSize );
  24.  
  25. free( api );
  26.  
  27. return 0;
  28. }
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to use admin passwords with "C"
Next Thread in C Forum Timeline: Cpu, virtual and physical memory





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC