Subject: Re: gcc question
To: Wojciech Puchar <wojtek@chylonia.3miasto.net>
From: Simon Burge <simonb@wasabisystems.com>
List: netbsd-users
Date: 04/18/2002 11:13:46
Wojciech Puchar wrote:

> why when compiling (gcc -c) such code
> 
> const char* x[5]={"aaa","bbb","cseqweqwe","eeeeeeeee","fwerjkwelrjwe"};
> 
> i get non-zero .data segment in .o file?
> 
> Sections:
> Idx Name          Size      VMA       LMA       File off  Algn
>   0 .text         00000000  00000000  00000000  00000034  2**2
>                   CONTENTS, ALLOC, LOAD, READONLY, CODE
>   1 .data         00000014  00000000  00000000  00000034  2**2
>                   CONTENTS, ALLOC, LOAD, RELOC, DATA
>   2 .bss          00000000  00000000  00000000  00000048  2**2
>                   ALLOC
>   3 .note         00000014  00000000  00000000  00000048  2**0
>                   CONTENTS, READONLY
>   4 .rodata       0000002a  00000000  00000000  0000005c  2**0
>                   CONTENTS, ALLOC, LOAD, READONLY, DATA
> 
> or simply - why "const" doesn't but data in .rodata?!

You're array of pointers is now constant, but the strings themselves
are still writable.  Through an extra "const" in there:

const char * const x[5]={"aaa","bbb","cseqweqwe","eeeeeeeee","fwerjkwelrjwe"};

Simon.
--
Simon Burge                            <simonb@wasabisystems.com>
NetBSD CDs, Support and Service:    http://www.wasabisystems.com/