Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

If I define a struct, must have given a size? As in example is 200.
C++
struct	LibCont_dat LCdat[200];

What can I do if I wish to have an unlimited size of struct?

Thanks in advance.
Posted
Updated 6-Dec-11 20:33pm
v2

That doesn't define a size for the struct - the struct definition does that.
What that code does is declare an array of structs - 200 of them - in the same way that
C++
int ar[100];
Defines an array of 100 integers.

If what you mean is "can I define an array where I do not specify the number of elements and it can be any size I want?" then no. You must always say how many elements you need when you declare the array.

If you want a storage structure where the number of elements is not fixed and new ones can be added at any time, I suggest you have a look at using a linked list instead.
 
Share this answer
 
Comments
sally8998 7-Dec-11 2:55am    
Hi there. Yes, I need a storage where its size is growing to a big number. Is there any examples or sites for your suggestions? Thanks.
OriginalGriff 7-Dec-11 3:04am    
http://www.codeproject.com/KB/trace/linkedlist.aspx
JackDingler 7-Dec-11 10:30am    
There are ready made STL solutions too.

std::vector
std::deque
std::map
std::mmap

std::deque might be the best for your problem.
OriginalGriff 7-Dec-11 10:41am    
Yep - but the subject index gave MFC, so I gave a pure MFC solution.
JackDingler 7-Dec-11 11:04am    
Ahh...
Your code won't even compile.

You can do something like that. Here is one primitive method, pretty much popular is Windows API:

C++
struct VariableSizeStructure {
    int someData;
    //...
    int Array[1]; //it can only come at the end
};


To use this structure, allocate some heap memory more than the size of the structure. For example, if you need N elements, allocate the size sizeof(int) * (N - 1) + sizeof(VariableSizeStructure) and index Array by indices 0 to N-1.

[EDIT]

I no way recommend this kind if code design in new development. This technique is good to know just because some APIs use it, but the practice itself is rotten. Don't use it in your own code design!

I really appreciate the valuable comment by Stephan below. Apparently, originally I failed to mention the danger of this approach, not to late to do it now. :-)

—SA
 
Share this answer
 
v3
Comments
CPallini 7-Dec-11 4:29am    
And add a size member to the struct, I would say.
My 5.
Sergey Alexandrovich Kryukov 7-Dec-11 18:25pm    
Right, good point. Please see the comment by Stephan below and my update/comment...
Thank you,
--SA
Stefan_Lang 7-Dec-11 5:02am    
Ugh...

be careful to whom you suggest such hacks. Yes, hacks! That MS uses them is no excuse, they've done quite horrible things and don't even bother to hide it in the depths of their code.

I know these kind of techniques can overcome certain ... shortcomings of the Windows API, but I see no reason at all to use them in any other scenario.

And apart from that, I would never suggest anything like that to someone whose question indicates he's just learning the language. This is not how the language should be (ab)used. It may be useful to learn such techniques later, but only when you know exactly what you're doing.
OriginalGriff 7-Dec-11 10:43am    
Regrettably, I can't give you a five for a comment. If I could, it would be a ten.
Stefan_Lang 7-Dec-11 12:01pm    
Hehe. Thanks anyway :-)

The code you posted define an array with 200 elements of type LibCont_dat (it does nothing to the size of the struct).


If you want an expandable array try to use vector or list.

 
Share this answer
 
Comments
sally8998 7-Dec-11 3:03am    
Thanks for the lesson.
If you want unlimited (or not predefined) number of elements, link list like data structure is useful. In array you must be previously aware of number of members you want.. use link list..
By the way std::vector and std::list are also good options if you are OK with using STL.
 
Share this answer
 
 
Share this answer
 
Comments
sally8998 7-Dec-11 2:52am    
Ok. I'll browse them. Thanks.
fjdiewornncalwe 7-Dec-11 16:38pm    
C++/MFC. Check the tags.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900