#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    3
    Rep 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?
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    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.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2004
    Location
    near St. Louis Illinois
    Posts
    3,288
    Rep Power
    24
    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)));
    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    3
    Rep Power
    0
    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...
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    3
    Rep Power
    0
    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..
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Why create yet another scripting language?

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  12. #7
  13. Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2004
    Location
    near St. Louis Illinois
    Posts
    3,288
    Rep Power
    24
    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;
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,183
    Rep Power
    2222
    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.
  16. #9
  17. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    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.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Phoenix
    Posts
    1
    Rep 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?
  20. #11
  21. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,114
    Rep Power
    1803
    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.

IMN logo majestic logo threadwatch logo seochat tools logo