Struct with variable length arrays of struct

Thread Solved

Join Date: Jul 2007
Posts: 19
Reputation: mohammadalipak is an unknown quantity at this point 
Solved Threads: 0
mohammadalipak mohammadalipak is offline Offline
Newbie Poster

Struct with variable length arrays of struct

 
0
  #1
Jul 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Struct with variable length arrays of struct

 
0
  #2
Jul 17th, 2007
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 ) );
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 19
Reputation: mohammadalipak is an unknown quantity at this point 
Solved Threads: 0
mohammadalipak mohammadalipak is offline Offline
Newbie Poster

Re: Struct with variable length arrays of struct

 
0
  #3
Jul 17th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Struct with variable length arrays of struct

 
0
  #4
Jul 17th, 2007
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.

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.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 19
Reputation: mohammadalipak is an unknown quantity at this point 
Solved Threads: 0
mohammadalipak mohammadalipak is offline Offline
Newbie Poster

Re: Struct with variable length arrays of struct

 
0
  #5
Jul 17th, 2007
The pointer is not zero. It's assigned a memory location of 0x6e615c31.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Struct with variable length arrays of struct

 
0
  #6
Jul 17th, 2007
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. }
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 19
Reputation: mohammadalipak is an unknown quantity at this point 
Solved Threads: 0
mohammadalipak mohammadalipak is offline Offline
Newbie Poster

Re: Struct with variable length arrays of struct

 
0
  #7
Jul 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6,530
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 836
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Struct with variable length arrays of struct

 
1
  #8
Jul 17th, 2007
> 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.
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 19
Reputation: mohammadalipak is an unknown quantity at this point 
Solved Threads: 0
mohammadalipak mohammadalipak is offline Offline
Newbie Poster

Re: Struct with variable length arrays of struct

 
0
  #9
Jul 18th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Struct with variable length arrays of struct

 
0
  #10
Jul 18th, 2007
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. }
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum


Views: 3527 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC