The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
create a structure at runtime in C
Discuss create a structure at runtime in C in the C Programming forum on Dev Shed. create a structure at runtime in C C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 23rd, 2004, 08:47 AM
|
|
Registered User
|
|
Join Date: Sep 2004
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
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
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
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:
Code:
struct mystruct
{
char name[20];
char address[80];
int age;
};
int main()
{
struct mystruct* pstruct = (struct mystruct*)malloc(sizeof(struct mystruct)));
}
|

September 23rd, 2004, 09:41 AM
|
|
Registered User
|
|
Join Date: Sep 2004
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Ancient Dragon 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:
Code:
struct mystruct
{
char name[20];
char address[80];
int age;
};
int main()
{
struct mystruct* pstruct = (struct mystruct*)malloc(sizeof(struct mystruct)));
}
|
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...
|

September 23rd, 2004, 09:44 AM
|
|
Registered User
|
|
Join Date: Sep 2004
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by mitakeet 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. |
I agree with you, but I am creating a scripting language that has such requirement..
|

September 23rd, 2004, 09:52 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
Why create yet another scripting language?
|

September 23rd, 2004, 09:55 AM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
then use an array of unions, such as an array of VARIANT structures.
Code:
struct myobject
{
std::string member_name;
VARIANT data;
}
vector<myobject> mystruct;
|

September 23rd, 2004, 10:04 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | Originally Posted by mitakeet Why create yet another scripting language? |
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.
---------------------------
After three days without programming, life becomes meaningless.
|

September 23rd, 2004, 10:33 AM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
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.
DaWei on Pointers Politically Incorrect.
|

January 31st, 2013, 12:10 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Location: Phoenix
Posts: 1
Time spent in forums: 31 m 28 sec
Reputation Power: 0
|
|
|
Runtime structures
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
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by rsankara 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... |
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.
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|