Runtime Structure Creation

D

Dieter

Hi All,

I am trying to dynamically implement (declare/define) a structure within
a function. The reason being, is that this structure would contain a
variable number of members of various possible types depending on what's
passed to it, and then be returned via pointer to the dynamically
allocated memory.

I'm sure that there are various ways to do this (ie: linked lists, void
types...,etc) but I can't seem to put my finger on it explicitly or
pragmatically in my available resources (that tree in the forrest :).
I'm still fairly green in "the ways of C".

I hope I've expressed my question clearly,
Thank you,
Dieter
 
E

Emmanuel Delahaye

Dieter a écrit :
I am trying to dynamically implement (declare/define) a structure within
a function. The reason being, is that this structure would contain a
variable number of members of various possible types depending on what's
passed to it, and then be returned via pointer to the dynamically
allocated memory.

A linked list is probably the best choice. That said, it's more a
question about "data structure / algorithm" (design stage) than about
(any) language (implementation stage).
 
M

Michael Mair

Dieter said:
Hi All,

I am trying to dynamically implement (declare/define) a structure within
a function. The reason being, is that this structure would contain a
variable number of members of various possible types depending on what's
passed to it, and then be returned via pointer to the dynamically
allocated memory.

I'm sure that there are various ways to do this (ie: linked lists, void
types...,etc) but I can't seem to put my finger on it explicitly or
pragmatically in my available resources (that tree in the forrest :).
I'm still fairly green in "the ways of C".

Note that you cannot create types (in the C sense) at run time as
there are no C types at run time.
That said:
1) If you do not know at compile time the types that will be created,
you could either go with linked lists (most flexible) and/or
rearrange the information into a tag list as array, ... This is
more of a data structures question; comp.lang.c may be appropriate
when asking about specific advantages within the language ("no
garbage collection" may influence your choice), otherwise
comp.programming IMO is the place to go. BTW: Linked lists would
be my choice in this case.
2) If you have a finite and small set of structured types known
at compile time, you can also work with a common type embedded as
first member:
typedef struct TypeIdAndCommonData {
TypeIdentifierType typeid;
/* common data */
....
} BaseType;

struct Type1 {
struct TypeIdAndCommonData basetype;
/* type specific */
....
}

struct Type2 {
struct TypeIdAndCommonData basetype;
/* type specific */
....
}

....

BaseType *createType(....);

....
BaseType *instance;

instance = createType(....);
if (!instance) {
/* error handling */
}

switch (instance->typeid) {
case TYPE_ONE:
{
struct Type1 *t1_instance = (struct Type1 *) instance;
t1_instance->....
....
}
}


Cheers
Michael
 
T

tmp123

Michael said:
typedef struct TypeIdAndCommonData {
TypeIdentifierType typeid;
/* common data */
....
} BaseType;

struct Type1 {
struct TypeIdAndCommonData basetype;
/* type specific */
....
}

struct Type2 {
struct TypeIdAndCommonData basetype;
/* type specific */
....
}

Or a variation of the same music:

struct foo
{
enum { t1, t2, ... } isa;
union
{
struct { ... } data1;
struct { ... } data2;
...
}
};

Kind regards.

DISCLAIMER:
The ellipsis composed by 3 points means "something that the reader can
imagine". Ellipsis with another number of dots are a typing mistake. If
the dot is up of a comma, it means dot and comma.
 
R

Rod Pemberton

Dieter said:
Hi All,

I am trying to dynamically implement (declare/define) a structure within
a function. The reason being, is that this structure would contain a
variable number of members of various possible types depending on what's
passed to it, and then be returned via pointer to the dynamically
allocated memory.

I'm sure that there are various ways to do this (ie: linked lists, void
types...,etc) but I can't seem to put my finger on it explicitly or
pragmatically in my available resources (that tree in the forrest :).
I'm still fairly green in "the ways of C".

I hope I've expressed my question clearly,
Thank you,
Dieter


I think what you're refering to is the C90 'struct hack'. Or, perhaps what
is known as variable length arrays in C99. Also, GCC supports dynamic
allocation of arrays (non-standard), if my memory serves...

If you're not familiar with the 'struct hack' #28 here:
http://home.tiscalinet.ch/t_wolf/tw/c/c9x_changes.html


Rod Pemberton
 
M

Michael Mair

tmp123 said:
Or a variation of the same music:

struct foo
{
enum { t1, t2, ... } isa;
union
{
struct { ... } data1;
struct { ... } data2;
...
}
};

Kind regards.

DISCLAIMER:
The ellipsis composed by 3 points means "something that the reader can
imagine". Ellipsis with another number of dots are a typing mistake. If
the dot is up of a comma, it means dot and comma.

Actually, I would rather use
union AllTypes {
struct TypeIdAndCommonData askme;
struct data1;
struct data2;
....
};
and have all types start with either a TypeIdAndCommonData
or the same common sequence. This makes handling common data
easier. For largely different type sizes, though, the first
approach may save enough memory too be more useful.

Cheers
Michael
 
D

Dieter

Dieter said:
Hi All,

I am trying to dynamically implement (declare/define) a structure within
a function. The reason being, is that this structure would contain a
variable number of members of various possible types depending on what's
passed to it, and then be returned via pointer to the dynamically
allocated memory.

I'm sure that there are various ways to do this (ie: linked lists, void
types...,etc) but I can't seem to put my finger on it explicitly or
pragmatically in my available resources (that tree in the forrest :).
I'm still fairly green in "the ways of C".

I hope I've expressed my question clearly,
Thank you,
Dieter

Naturally I very much appreciate those that took the time to respond to
my question. Thank you. :)

This is what I was hoping to accomplish, Which seems feasable and
practical. If this warrants some un-foreseen caution or other comments,
please let me know. As always, Thanks.

Dieter

/*
* voidstruct.c
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct hmm_ {
void *a;
void *b;
void *c;
} Hmm_t;

int main(void)
{
Hmm_t hmm;
int d = 5;
float e = 4.3;
char f = 'g';

hmm.a = malloc(sizeof(d));
hmm.b = malloc(sizeof(e));
hmm.c = malloc(sizeof(f));

hmm.a = &d;
hmm.b = &e;
hmm.c = &f;

printf("\n");
printf("%d\n", *(int *)hmm.a);
printf("%.2f\n", *(float *)hmm.b);
printf("%c\n", *(char *)hmm.c);

return 0;
}
 
W

Walter Roberson

Dieter said:
int d = 5;
float e = 4.3;
char f = 'g';
hmm.a = malloc(sizeof(d));
hmm.b = malloc(sizeof(e));
hmm.c = malloc(sizeof(f));

Those statements assign values to hmm.a, hmm.b, and hmm.c . The values
assigned are the addresses of the dynamic storage that was allocated.
The dynamically allocated storage is uninitialized at this point.
You should, by the way, check that these values are not NULL,
in case memory could not be allocated.

hmm.a = &d;
hmm.b = &e;
hmm.c = &f;

And those three statements assign *new* values to hmm.a, hmm.b, and
hmm.c. The values assigned are the [current] addresses of the local
variables d, e, and f respectively. The values that were the pointers
get overwritten.

What you probably wanted at this point is something like,

memcpy( hmm.a, &d, sizeof(d) );
memcpy( hmm.b, &e, sizeof(e) );
memcpy( hmm.c, &f, sizeof(f) );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top