Hi All, when I write: const char mysting[] ={"my string"}; data are placed to DATA segment, not code sement. I know if it would be written like this: code char mysting[] ={"my string"}; data will be placed in code segment. What I have to do for automatic placing const data to code segment? I did it by #define const code But may be other ways are exist. Thanks, Vladimir
const char mysting[] ={"my string"};
code char mysting[] ={"my string"};
const char code myString[] = "Hello";
Thank you Mark for your answer. But if compiler knows that data read-only, why it's difficult to place it in ROM segment? I've seen a program compiled by IAR, and there were no statments "code" for "const" data. Vladimir
It's not a difficulty. The idea is to give the user the flexibility to locate the strings in any memory space. The default is data space. While you could make an argument that as a const, it should default to code space, someone will then ask why it is not in data space.
'const' is similar read-only, but it is not a memory_storage_qualifier. Look at the example:
struct CCC { const int x; int y; } ccc = { 1, 2 }; void func(const char *str) { char * ptr; ptr = str; // MUST produce WARNING *ptr = 'B'; // LEGAL *str = 'A'; // ILLEGAL ccc.y = 5; // LEGAL ccc.x = 2; // ILLEGAL }
The compiler should not place the data in ROM because you may want to place a memory mapped read-only register in XDATA space. Thus if you were force into code space you could not read the register. By allowing const to do exactly what it does in ISO/ANSI C, Keil has allowed us to enforce compile-time read-only attributes while having the freedom of placing the read-only variable in any data space. Another example, in the assembler startup routine you calculate a "first available memory" location for malloc and write it to an address for malloc to use. Malloc() declares that external variable as const but the variable must be in a writable memory space for the C startup module to be able to set it. In the end, tell C51 exactly where you want everything to go. I put memory space qualifiers on every single variable I define. I am never surprised when I look in the map file. - Mark
Hello Sergey, many thanks for your example, I didn't consider this situation. Vladimir
Thank you Tom, may be you are right. At least it's supported by other compilers too. But I just wish to have an option at IDE for allocating cont in ROM area. Such options are exist for some compilers. Regards, Vladimir
View all questions in Keil forum