473,505 Members | 14,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Sep 15 '08 #1
2 1954
Andreas Lundgren wrote:
[... initializing linked data structures at compile time ...]

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?
You can often achieve the desired effect by initializing the
"data pieces" separately and giving them names, and then using
those names to initialize the "linking pieces." For example,
this won't work:

static struct {
int count;
int data[];
} Data[] = {
{ 3, {1, 2, 3} },
{ 2, {10, 11} },
{ 5, {4, 3, 2, 1, 0} },
};

.... but you can do this instead:

static int data0[] = { 1, 2, 3 };
static int data1[] = { 10, 11 };
static int data2[] = { 4, 3, 2, 1, 0 };

static struct {
int count;
int *data;
} Data[] = {
{ 3, data0 },
{ 2, data1 },
{ 5, data2 },
};

Linked lists, trees, and so on can be built the same way,
although at some point the explosion of "helper" names may get
out of hand. If that's your situation, it may be time to write
a program to that builds the data structure dynamically and then
generates C source like the above to initialize it statically.

--
Er*********@sun.com
Sep 15 '08 #2
Andreas Lundgren <d9****@efd.lth.sewrites:
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.
Sep 15 '08 #3

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

Similar topics

1
4092
by: Dennis Gavrilov | last post by:
Hi, All! I have two questions: strategic and technical. Technical one first: I need to share an array of objects (implemented as hashes, having references to other objects and hashes, sharing...
8
3665
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
12
2545
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
5
2209
by: Arno | last post by:
Hello, here is my problem: I have a complex data type, like a std::vector< std::string > and need some global constant values of this type. The type is also used non-constant throughout the...
3
1533
by: arne.muller | last post by:
Hello, I've read in the FAQ that modern versions of malloc/free can cache memory, i.e. free does not return memory to the OS so that it may be re-used by the next malloc call. I was thinking...
24
19035
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
53
26329
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
4
4690
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
0
944
by: oleop | last post by:
I am trying to call dll from the managed code.eveyrting works OK until i'm trying to use fucntions that use complex (containing arrays of structures) structures as a parameters. Let's say ...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
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...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.