September 23rd, 2004, 08:47 AM
create a structure at runtime in C
Does anyone know, how to create a structure at runtime in C? I yes, can you provide me the way?
September 23rd, 2004, 09:05 AM
Structures are compile-time defined. You can create open-ended structures (such as unions) that will allow you to put anything in it you want, but the whole point of a strongly typed language is so you resolve as many problems at compile time as possible.
September 23rd, 2004, 09:06 AM
what do you mean by "create a structure"? structures have to be defined at compile time, but you can allocate them at runtime. For example:
struct mystruct* pstruct = (struct mystruct*)malloc(sizeof(struct mystruct)));
September 23rd, 2004, 09:41 AM
What I meant was, I don't know the member types and sizes of the structure (for example, mystruct above) during compile time.. I will get these information (mostly from user input) only during runtime.. with that I need to create a memory area, identical to the structure above.. which is expected by the other function...
Originally Posted by Ancient Dragon
September 23rd, 2004, 09:44 AM
I agree with you, but I am creating a scripting language that has such requirement..
Originally Posted by mitakeet
September 23rd, 2004, 09:52 AM
Why create yet another scripting language?
September 23rd, 2004, 09:55 AM
then use an array of unions, such as an array of VARIANT structures.
September 23rd, 2004, 10:04 AM
I believe that it was from the Zen of Programming that we learn the story of the novice who did not like his editor, so he set about creating his own. The Master Programmer asked him what he was do and, upon hearing his reply, whacked him on the side of the head. Picking himself up off the floor, the novice asked what was wrong. The Master Programmer replied, "I do not wish to learn yet another editor!" And the novice was enlightened.
Originally Posted by mitakeet
After three days without programming, life becomes meaningless.
September 23rd, 2004, 10:33 AM
Expected by what other function? How does it know what to expect? You can pack values into memory anyway you like, of course, or you can instantiate a predefined structure at run time. The term, 'structure', implies a particular thing in C/C++ (like 'reference'), but can be used inappropriately, if you wish, to describe an off-the-cuff format in which you organize some conglomeration of data.
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
January 31st, 2013, 12:10 PM
I have the same need and have been looking for an answer to this. It would be useful to create a structure at runtime based on file or user input. I currently get around this by using a header definition and a function to calculate the data position and return the result. This can get cumbersome with bit fields and different data types. It would be much better if I could just create the structure and cast a pointer to access the data elements directly. Is there no way to create a mix of data types into a structure at runtime?
January 31st, 2013, 06:46 PM
Exactly what you need to do it i snot possible to say since you have provided few details, but in general if you do not know the size of data a priori, then you would use dynamic memory allocation.
Originally Posted by rsankara
You have made the mistake of inventing a solution to a problem then asking about the solution you have invented. You would do better to ask about the original problem, because your solution mighnt not be efficient, practical or even possible.
C "struct" is user defined aggregate data type, but you cannot "define" one at runtime. However data structures in the general sense can be constructed dynamically. Such structures are arrays, vectors, linked-lists, associative maps, b-trees etc. You need to select the data structure appropriate to your application.
The C standard library does not provide high-level data structures, but C++ does. Using C++ is probably easier and more efficient that rolling your own in C.
For very data-centric applications, use of an embedded database library may be prefereable.