Wrong. While the footnote (# 103 in C99) does say a compiler MAY place a const
object in read-only memory, it does not require it (and in fact technically,
footnotes are not consider part of the standard). However, a const field
within a structure is still embedded in that whole structure, and the whole
structure is not const.
> If the structure members we located exactly as written, then
> the following structure would not be possible:
>
> struct s {
> int a;
> int b;
> const char c[0x10];
> int d;
> } s = { 0x01, 0x02, "Hello World", 0x04 };
>
> It is quite correct to mix read-only data and writable data within
> a structure. A correctly operating compiler will put the read-only
> data with other read-only data and, if this area is protected either
> because it's in ROM, or the OS traps, not allow it to be written.
> This means that it must be some place else than where it's denoted
> in a visual representation of the structure. This is one of
> the main reasons why one cannot assume that the memory layout
> is represented by the logical layout of the structure.
Wrong again. The standard quite clearly requires that an object be continous.
> In short, a structure is a logical representation of an array
> of aggregate types, not a physical representation.
>
> With `gcc` the following compiles with a warning about 'const', but it
> still compiles and runs.
>
> #include <stdio.h>
> #include <string.h>
>
> struct s {
> int a;
> int b;
> const char c[0x10];
> int d;
> } s = { 0x01, 0x02, "Hello World", 0x04 };
>
> int main()
> {
> printf("a = %p\n", &s.a);
> printf("b = %p\n", &s.b);
> printf("c = %p\n", s.c);
> printf("d = %p\n", &s.d);
> strcpy(s.c, "So much for rules");
> puts(s.c);
> return 0;
> }
>
> This compiles and runs because gcc does not put the "const char"
> string in ".rodata" as it is supposed to do. It just throws it in
> with with ".data" so you get the behavior that many persons expect,
> while refusing to do what the code told it to do. This is an example
> of the Gnu-ism where the compiler thinks it "knows more than the
> programmer". Further, since gcc does this, many persons think that
> this is "correct" or "how it is supposed to be" or "complies with
> CX standard", etc.
>
> If you are writing execute-in-place code (NVRAM), with only a few
> kilobytes of RAM, you are in a world of hurt with such a compiler.
> That's why you don't use gcc for this. Instead, you use a compiler
> which produces code "as written" for such a project.
No, you properly use the standard, instead of reading your own
misinterpretations into it. In this case, it means keeping parallel data
structures if you feel some parts need to be in read-only memory and some not.
-- Michael Meissner, Red Hat, Inc. (GCC group) PMB 198, 174 Littleton Road #3, Westford, Massachusetts 01886, USA Work: meissner@redhat.com phone: +1 978-486-9304 Non-work: meissner@spectacle-pond.org fax: +1 978-692-4482 - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/