Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically create and read structs in C?

Tags:

c

struct

How can I do something like that (just an example):

any_struct *my_struct = create_struct();
add_struct_member(my_struct, "a", int_member);
add_struct_member(my_struct, "b", float_member);

So that I could load and use a struct instance "from the outside" (at the address addressOfMyStruct) with the given structure here?

any_struct_instance *instance = instance(my_struct, addressOfMyStruct);
int a = instance_get_member(instance, "a");
float b = instance_get_member(instance, "b");

I would also like to be able to create struct instances dynamically this way.

I hope it's clear what I want to do. I know that C/Invoke is able to do it, but is there a separate library to do that?

like image 421
user94405 Avatar asked Apr 22 '09 15:04

user94405


People also ask

Is it possible to dynamically define a struct in C++?

It isn't possible to dynamically define a struct that is identical to a compile-time struct. It is possible, but difficult, to create dynamic structures that can contain the information equivalent to a struct. The access to the data is less convenient than what is available at compile-time.

How do you write to a struct in C?

Prerequisite: Structure in C For writing in file, it is easy to write string or int to file using fprintf and putc, but you might have faced difficulty when writing contents of struct. fwrite and fread make task easier when you want to write and read blocks of data. fwrite : Following is the declaration of fwrite function

How to create structures within structures in C programming?

You can create structures within a structure in C programming. For example, struct complex { int imag; float real; }; struct number { struct complex comp; int integers; } num1, num2; Suppose, you want to set imag of num2 variable to 11. Here's how you can do it: num2.comp.imag = 11;

Is it possible to create dynamic data structures?

However, the 'cannot do dynamic data structures' part limits things - you have to agree beforehand on what the data structures are, and how they will be interpreted. How do you do that? It is possible, but difficult, to create dynamic structures that can contain the information equivalent to a struct. — How do you do that?


2 Answers

Actually demonstrating the code to make this work in C is a bit too involved for an SO post. But explaining the basic concept is doable.

What you're really creating here is a templated property bag system. The one thing you'll need a lot of to keep this going is some assiociative structure like a hash table. I'd say go with std::map but you mentioned this was a C only solution. For the sake of discussion I'm just going to assume you have some sort of hashtable available.

The "create_struct" call will need to return a structure which contains a pointer to a hashtable which makes const char* to essentially a size_t. This map defines what you need in order to create a new instance of the struct.

The "insance" method will essentially create a new hashtable with equal number of members as the template hashtable. Lets throw lazy evualation out the window for a second and assume you create all members up front. The method will need to loop over the template hashtable adding a member for every entry and malloc'ing a memory chunk of the specified size.

The implementation of instance_get_member will simply do a lookup in the map by name. The signature though and usage pattern will need to change though. C does not support templates and must chose a common return type that can represent all data. In this case you'll need to chose void* since that's how the memory will need to be stored.

void* instance_get_member(any_struct_instance* inst, const char* name);

You can make this a bit better by adding an envil macro to simulate templates

#define instance_get_member2(inst, name, type) \
  *((type*)instance_get_member((inst),(name)))
...
int i = instance_get_member2(pInst,"a", int);
like image 156
JaredPar Avatar answered Sep 18 '22 17:09

JaredPar


You've gone so far defining the problem that all that's left is a bit of (slightly tricky in some parts) implementation. You just need to keep track of the information:

typedef struct {
    fieldType type;
    char      name[NAMEMAX];
    /* anything else */
} meta_struct_field;
typedef struct {
    unsigned          num_fields;
    meta_struct_field *fields;
    /* anything else */
} meta_struct;

Then create_struct() allocates memory for meta_struct and initialized it to 0, and add_struct_member() does an alloc()/realloc() on my_struct.fields and increments my_struct.num_fields. The rest follows in the same vein.

You'll also want a union in meta_struct_field to hold actual values in instances.

like image 43
dwc Avatar answered Sep 21 '22 17:09

dwc