Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Complex static memory structures and content (http://www.velocityreviews.com/forums/t635432-complex-static-memory-structures-and-content.html)

Andreas Lundgren 09-15-2008 02:36 PM

Complex static memory structures and content
 
Hi!

I want to put some values in static memory, and then be able to
reference this data from my code in a decent way by referencing
AllData.Data[i]. (All code beneth is placed outside functions.)

___TYPE___
typedef struct Data_s
{
char* Name_p;
void* Class_p;
int Size
} Data_t;

___DATA___
{"foo", NULL, 0},
{"bar", NULL, 0},



I first tried the following, but when compiling for ARM compiler I've
got "incomplete type is not allowed"

typedef struct AllData_s
{
Data_t Data[];
} AllData_t;

static AllData_t AllData = {
{"foo", NULL, 0},
{"bar", NULL, 0}
};



I then tried the following, but got the same error.

typedef struct Data_t AllData_t[2];

static AllData_t AllData = {
{"foo", NULL, 0},
{"bar", NULL, 0},
};



Finally this worked:
typedef struct Data_t AllData_t[2];

static Data_t AllData[2] = {
{"foo", NULL, 0},
{"bar", NULL, 0},
};


Is is possible to build up more advanced static structures? When I've
got this to work, I directly use the AllData[i] and not the
AllData.Data[i] that I really desired. Would it be possible to do a
more complex data structure at compile time?

Best Regards,
Andreas L - Sweden

Ben Bacarisse 09-15-2008 03:10 PM

Re: Complex static memory structures and content
 
Andreas Lundgren <d99alu@efd.lth.se> writes:

> I want to put some values in static memory, and then be able to
> reference this data from my code in a decent way by referencing
> AllData.Data[i]. (All code beneth is placed outside functions.)
>
> typedef struct Data_s
> {
> char* Name_p;
> void* Class_p;
> int Size


(Missing ;)

> } Data_t;
>

<snip>
> Finally this worked:
> typedef struct Data_t AllData_t[2];
>
> static Data_t AllData[2] = {
> {"foo", NULL, 0},
> {"bar", NULL, 0},
> };


The typedef of AllData_t is not involved (that is why it works!).
This is the correct way to proceed, but you don't need the 2:

static Data_t AllData[] = {
{"foo", NULL, 0},
{"bar", NULL, 0},
};

is fine.

> Is is possible to build up more advanced static structures?


Yes. Just keep building it up from the simple piece to the more
complex ones. If you can use C99 you get even more options, but I
have not shown anything here that is not "old" C.

> When I've
> got this to work, I directly use the AllData[i] and not the
> AllData.Data[i] that I really desired. Would it be possible to do a
> more complex data structure at compile time?


If you now want that array of two Data_t objects to be part of bigger
struct, then use a pointer:

struct AllData_s {
Data_t *Data;
size_t n_data_objects;
/* other members ... */
} all_data = {
AllData,
sizeof AllData / sizeof AllData[0]
};

Now you can use all_data.Data[1] if that is what you need (for some
reason).

--
Ben.


All times are GMT. The time now is 02:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.