Re: c++ const aggregate vs constructor

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux