C++ Constants going to SRAM

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

C++ Constants going to SRAM

1,012 Views
manishsangram
Contributor IV

Hi,

 

We are using S12ZVMC128 and using Codewarior 10.6

 

We were running out of SRAM and when looking into the matter by referring to the MAP file we realized that even though we have marked constants as static and const, these are being treated as variables and are being put in SRAM.  This should not be the case because obviously a constant is a constant and should only reside in RO memory.

 

Example of how we have defined a constant is given below

 

CClassX.h

class X{

static const unsigned char ConstantVal;

};

 

CClassX.cpp

const unsigned char X::ConstantVal = 100;

 

Why is the compiler creating variables in SRAM for such constants? 

How can we turn them off.

 

We checked that similar static constants outside class definition (module level) are correctly put only in RO so it cannot be any form of speed optimization.

Original Attachment has been moved to: Demo.zip

0 Kudos
4 Replies

633 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi Manish

To work on your problem more efficiently,Could you please upload your demo project? I will check it directly from my side.

Thanks,

Jennie Zhang

0 Kudos

633 Views
manishsangram
Contributor IV

Hi Zhang Jennie,

Were you able to look at the attached code and do you have any solution?

0 Kudos

633 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

HI Manish,

Constant value must be assigned while it is declared.  Thus we can guarantee the constant value allocated in FLASH.

for example, the first time we define constant variable ConstantVal in *.h, we need also assigned constant value to it

static const unsigned char ConstantVal;

change to

static const unsigned char ConstantVal = 100;

thus ConstantVal  is 0xaa, it can not be assigned to other value anymore.

This is the same for C code.


Have a great day,
Jennie Zhang

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

633 Views
manishsangram
Contributor IV

I've attached Demo.zip and the class A::TestClass::CVal is a constant declared in the header and defined in the CPP file and used in Main. DVal is declared and defined in the header and does not have the same issue as CVal.

0 Kudos