Connecting Tech Pros Worldwide Forums | Help | Site Map

Runtime Structure Creation

Dieter
Guest
 
Posts: n/a
#1: Jan 22 '06
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

Emmanuel Delahaye
Guest
 
Posts: n/a
#2: Jan 22 '06

re: Runtime Structure Creation


Dieter a écrit :[color=blue]
> 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.[/color]

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).

--
A+

Emmanuel Delahaye
Michael Mair
Guest
 
Posts: n/a
#3: Jan 22 '06

re: Runtime Structure Creation


Dieter wrote:[color=blue]
> 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".[/color]

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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
tmp123
Guest
 
Posts: n/a
#4: Jan 22 '06

re: Runtime Structure Creation


Michael Mair wrote:[color=blue]
> typedef struct TypeIdAndCommonData {
> TypeIdentifierType typeid;
> /* common data */
> ....
> } BaseType;
>
> struct Type1 {
> struct TypeIdAndCommonData basetype;
> /* type specific */
> ....
> }
>
> struct Type2 {
> struct TypeIdAndCommonData basetype;
> /* type specific */
> ....
> }[/color]

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.

tmp123
Guest
 
Posts: n/a
#5: Jan 22 '06

re: Runtime Structure Creation


tmp123 wrote too fast:[color=blue]
> it means dot and comma[/color]
that is semicolon.

Coos Haak
Guest
 
Posts: n/a
#6: Jan 22 '06

re: Runtime Structure Creation


Op 22 Jan 2006 10:07:10 -0800 schreef tmp123:
[color=blue]
> tmp123 wrote too fast:[color=green]
>> it means dot and comma[/color]
> that is semicolon.[/color]

<OT>
http://nl.wikipedia.org/wiki/Puntkomma
</OT>
--
Coos
Rod Pemberton
Guest
 
Posts: n/a
#7: Jan 22 '06

re: Runtime Structure Creation



"Dieter" <usenet.stuff@comcast.net> wrote in message
news:P9-dnazePd-SqE7enZ2dnUVZ_sidnZ2d@comcast.com...[color=blue]
> 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[/color]


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



Michael Mair
Guest
 
Posts: n/a
#8: Jan 22 '06

re: Runtime Structure Creation


tmp123 wrote:[color=blue]
> Michael Mair wrote:
>[color=green]
>> typedef struct TypeIdAndCommonData {
>> TypeIdentifierType typeid;
>> /* common data */
>> ....
>> } BaseType;
>>
>> struct Type1 {
>> struct TypeIdAndCommonData basetype;
>> /* type specific */
>> ....
>> }
>>
>> struct Type2 {
>> struct TypeIdAndCommonData basetype;
>> /* type specific */
>> ....
>> }[/color]
>
>
> 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.[/color]

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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Dieter
Guest
 
Posts: n/a
#9: Jan 25 '06

re: Runtime Structure Creation


Dieter wrote:[color=blue]
> 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[/color]

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;
}

Walter Roberson
Guest
 
Posts: n/a
#10: Jan 25 '06

re: Runtime Structure Creation


In article <sp6dneLy8YZ5ZErenZ2dnUVZ_vudnZ2d@comcast.com>,
Dieter <usenet.stuff@comcast.net> wrote:
[color=blue]
> int d = 5;
> float e = 4.3;
> char f = 'g';[/color]
[color=blue]
> hmm.a = malloc(sizeof(d));
> hmm.b = malloc(sizeof(e));
> hmm.c = malloc(sizeof(f));[/color]

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.

[color=blue]
> hmm.a = &d;
> hmm.b = &e;
> hmm.c = &f;[/color]

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) );
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Closed Thread