EmbeddedRelated.com
Forums

c++ how to put a const variable into flash memory in a class

Started by htrada2 June 16, 2008
Hi,
Ive been using msp430 for several years and for some months ive been
experimenting in the use of c++ objects to see how the compiler deals
with them.
My question is: I have a class with some const variables inside which
are initialized by the constructor (for example the size of a buffer).
Is it posible to force the compiler to locate this variable in flash
memory instead of ram?. Im defining the variable in a global scope,
so theres no stack usage.

2nd question: is my 1st quesion clear enough?? haha.
Bye
Hernan

Beginning Microcontrollers with the MSP430

Hi Hernan!

> I've been using msp430 for several years and for some months i've been
> experimenting in the use of c++ objects to see how the compiler deals
> with them.
> My question is: I have a class with some const variables inside which
> are initialized by the constructor (for example the size of a buffer).
> Is it posible to force the compiler to locate this variable in flash
> memory instead of ram?. I'm defining the variable in a global scope,
> so theres no stack usage.

No, it's not possible.

When you have a constructor it counts as though you are doing run-time
initialization of the object.

On the other hand, if you don't have constructors you can use plain old
struct assigment. You can, however, still define your own function in
the class/struct.

Also, you might want to considering not declaring the members "const",
but instead define the object using "const". For example:

class Foo
{
public:
void sune();

int mOne;
int mTwo;
};

const Foo foo = {10, 20};

int test()
{
return foo.mOne + foo.mTwo;
}

> 2nd question: is my 1st quesion clear enough?? haha.

If I misunderstood your question, a "yes" here would not count as much,
would it?

-- Anders Lindgren, IAR Systems
--
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.