1

I'm programming embedded system with C language and I define a structure with two constant member "val" and "ptr".

typedef struct{
const u8 val;
u8 *const ptr;
}TEST;

If I declare such type variable as a global, would the variable be placed in ROM or in RAM?

TEST var;

Thanks

7
  • Why should it be stored in ROM. Memory is allocated either in stack or heap!!! Please mention the language you are using as memory allocation varies from language to language.
    – Narendra
    Jun 25, 2013 at 4:00
  • I'm programming embedded system with c language, and I know all the variables will be placed either in RAM,if declare "u8 var", or in ROM, if declare "const u8 var". But I'm not sure whether a structure with constant members will place its member in the RAM or ROM, when I declare a global structure variable as I mentioned above. Thank you. Jun 25, 2013 at 5:49
  • Please add proper tag to the question so that proper people can guide you.
    – Narendra
    Jun 25, 2013 at 5:54
  • 2
    It will depend on your system — read the manual. You will not be able to modify TEST.ptr once the variable is initialized, so it will always be a null pointer (since you didn't provide any explicit initialization, it is all zeroes). This is probably not what you had in mind. The variable should not be in ROM; the TEST.val may be modified at runtime. Your compiler may have other ideas... Jun 25, 2013 at 6:17
  • Since you have to rely on the fact that a complete instance of your struct is accessible with a single pointer your const members at least have to be accessible in the same memory area as the const members. For this I assume the memory manager (if there is one) is not smart enough to ensure the right access addresses if one member is in ROM and another is in RAM.
    – urzeit
    Jun 25, 2013 at 6:54

1 Answer 1

2

The TEST instance needs to go into read/write memory - the TEST instance is non-const, even though all of it's members are const. GCC will emit the struct into .data which is read/write and the linker script should put this into the appropriate memory type.

'const' in C doesn't guarantee that the storage really is immutable - it only means that a program that performs strictly to the C standard won't have a 'const' item modified.

If you want data to go into ROM then you'll need an appropriate linker script that puts .rodata sections into ROM.

1
  • I would add two things to this decent answer. One is that within the source you will need to use compiler specific keywords (such as "attribute section" with gcc) to make the variables go in the appropriate sections defined in the linker script, the other is that maybe the role of "const" should be clarified a bit. For example typically you may pass pointers to const to functions which are supposed to not modify the contents, and the "const volatile" apparent oxymoron may also worth a mention.
    – Jubatian
    Jul 6, 2013 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.