CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Porting issue (initializing struct)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
theteaman



Joined: 04 Aug 2006
Posts: 98

View user's profile Send private message

Porting issue (initializing struct)
PostPosted: Thu Oct 18, 2012 6:42 am     Reply with quote

Hi

I am porting a program from x86 GCC to PIC CCS C. It has been a lot of work, for example, replacing all the recursive functions and ensuring the code is CCS compatible.

Now I am at a point where I could really use some help. The issue is that there is a very large data structure. It is a struct, with more structs inside.

On the x86 version of the program, the data structure is created at init time through a set of function calls that populate the data structure with constants. On the PIC, there is simply not enough RAM to do this.

Luckily, all the data going in to the structure is constant and known at compile time, so theoretically I should be able to store this data in program memory.

I've looked at the examples of storing data in program memory but it's really messy. I guess what I'm after is the ability to do something like this (global variables):

Code:

struct b
{
    int a;
};

struct a
{
    int x;
    struct b z;
};

const struct a data[100];
data[0].x = 1;      //breaks here
data[0].z.a = 2;   //breaks here

void main() {
...


However, it seems it's only possible to initialize the memory in one huge blob of code, like so:

Code:

const struct a data[100] = {1, {2}}...


This would make the code difficult to read, write and maintain. Is it possible to initialize the struct as above?

Any help appreciated, thanks.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Oct 18, 2012 7:47 am     Reply with quote

A const data items can only be initialised, and read, it cannot be assigned to. As they never changed, they can be placed in read-only memory, which in CCS C means program memory.

Your preferred initialisation is assigning values at run time: you cannot do this with const data. The "huge blob" is the correct, and only way of initialising const data.

This is basic C: const is const and cannot be changed. End of.

Yo uare suffering one of the consequences of porting code from a processor with vast memory resources to a very limited one. You cannot expect to simply copy the code over, you have to put in quite a lot of porting effor, which you seem to be able and willing to do, which is good.

RF Developer
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Thu Oct 18, 2012 7:58 am     Reply with quote

and remember a 'huge blob', can be split up in your source. If (for instance), you are intialising structures, then put each structure initialisation on a separate line with the '\' at the end of each line. You can even have comments after the '\' so the initialisation needn't be 'seen' as one big blob.....

Best Wishes
theteaman



Joined: 04 Aug 2006
Posts: 98

View user's profile Send private message

PostPosted: Thu Oct 18, 2012 8:02 am     Reply with quote

Thanks both for your help. I guess the issue is the struct has structs (with more structs) inside it. It just gets too messy. But I think I can use macros to make things look neater.. I will try it.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group