473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Runtime Structure Creation

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
Jan 22 '06 #1
9 3738
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).

--
A+

Emmanuel Delahaye
Jan 22 '06 #2
Dieter wrote:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jan 22 '06 #3
Michael Mair wrote:
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.

Jan 22 '06 #4
tmp123 wrote too fast:
it means dot and comma

that is semicolon.

Jan 22 '06 #5
Op 22 Jan 2006 10:07:10 -0800 schreef tmp123:
tmp123 wrote too fast:
it means dot and comma

that is semicolon.


<OT>
http://nl.wikipedia.org/wiki/Puntkomma
</OT>
--
Coos
Jan 22 '06 #6

"Dieter" <us**********@comcast.net> wrote in message
news:P9******************************@comcast.com. ..
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

Jan 22 '06 #7
tmp123 wrote:
Michael Mair wrote:
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.


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.
Jan 22 '06 #8
Dieter wrote:
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;
}

Jan 25 '06 #9
In article <sp******************************@comcast.com>,
Dieter <us**********@comcast.net> wrote:
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) );
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Jan 25 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Steven T. Hatton | last post by:
As I was reading through some of the stuff in the Standard, I started to wonder if there might be reason to define classes in response to runtime conditions, say, to match some kind of new data...
27
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this...
3
by: chellappa | last post by:
Hi EveryBody! i need a information about creatting structure at runtime.... like example ..... int main() { char *a,*b; printf("Enter the structure"); scanf("%s",a); printf ("Enter The...
11
by: tony.fountaine | last post by:
I don't think this is possible but, if anyone has comments on a work around that would be great. Let me explain the reason I would like to do this. Suppose you are given a file that contains a...
10
by: Lauren Wilson | last post by:
I have a desperate short term need for a way to install Access 2003 runtime on client computers. I have the proper license to do so but I cannot seem to find the files to do it like we did with...
2
by: juventusaurabh | last post by:
Thanks ppl for your help. I actually need to create a DTS package runtime, add tasks to it (to convert text file to sql table) and validate the data in tables. Can anyone plz help me with...
3
by: Sean C. | last post by:
Hey All, I'm having a little problem here. I have a project that I'm working on that involves a MySQL server database backend. I'm having no problem creating the database on the fly if it...
17
by: blufox | last post by:
Hi All, Can i change the execution path of methods in my process at runtime? e.g a()->b()->c()->d()->e() Now, i want execution to be altered at runtime as -
1
by: chriskent | last post by:
Hi, I have an unusual situation whereby we create a Window during initialisation. We have used this code for many years and have found one unusual case with a customer when the RegiterClass fails...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.