This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: c++ const aggregate vs constructor
- From: Daniel Lohmann <daniel dot lohmann at informatik dot uni-erlangen dot de>
- To: Kevin Yohe <kevin at ssc-nh dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Tue, 26 Jun 2007 14:19:10 +0200
- Subject: Re: c++ const aggregate vs constructor
- References: <001801c7b774$0d1b22c0$8132a8c0@sscnh.com>
Kevin Yohe wrote:
> Hi,
>
> I am trying to define a constant table that can be initialized at
> compile-time and linked into ROM. I had envisioned two ways of defining
> this; either
>
> struct sAggregate
> {
> int m0;
> int m1;
> int m2;
> int m3;
> };
>
> const sAggregate table1[] =
> { { 0, 0, 0, 0 },
> { 1, 1, 1, 1 },
> { 2, 2, 2, 2 },
> { 3, 3, 3, 3 }
> };
This is initialized at compile-time.
> or
>
> struct sConstruct
> {
> int m0;
> int m1;
> int m2;
> int m3;
>
> sConstruct( a0, a1, a2, a3 )
> : m0(a0), m1(a1), m2(a2), m3(a3)
> {};
> };
>
> const sConstruct table2[] =
> { sConstruct( 0, 0, 0, 0 ),
> sConstruct( 1, 1, 1, 1 ),
> sConstruct( 2, 2, 2, 2 ),
> sConstruct( 3, 3, 3, 3 )
> };
AFAIK this is initialized at runtime. (Given that you have a runtime
library that invokes constructors of globals. Otherwise it is undefined.
> I am wondering if these two definitions are logically equivalent.
>
> Is the gnu c++ compiler smart enough to initialize these tables at
> compile-time or are they actually initialized after the data is copied into
> ram?
>
> Background: I am writing some boot code that has the task of configuring
> and initializing some SDRAM. The boot code has a limited amount of SRAM to
> play with. Since these tables are constants I would like to store them in
> ROM. How do I force the linker to put them in ROM? How do I ensure that
> the table is initialized?
The compiler should put const data alread in a ".rodata" section - you
might want to check this with objdump or nm. If not, you can explicitly
assign a section by the gcc attribute syntax (__attribute__(("section",
".rodata"))__, take a look in the manual.)
If the linker really puts .rodata into the ROM area depends on the
linker file you use.
Daniel