Why const is not const?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Why const is not const?

ソリューションへジャンプ
1,370件の閲覧回数
Teckna
Contributor V

Hi everyone,

 

I'm working with CW 10.2 and Kinetis MK60DN512.

 

I created a bareboard project with a single test file:

 

#include <stdio.h>

#include "derivative.h" /* include peripheral declarations */

 

typedef struct
{
     const char *str;
} TEST;

 

const TEST Test0 =
{
     "STRING0"
};
const TEST Test1 =
{
     "STRING1"
};

const TEST *Test[ 2 ] =
{
     &Test0,
     &Test1
};

 

int main( void )
{
     int i;

 

     for( i = 0; i < 2; i++ )
          printf( Test[ i ]->str );

     while( 1 )
     {
          i++;

     }

 

     return( 0 );
}

 

In the map file the two const structs Test0 and Test1 are located in .rodata section (flash), but the const struct Test is located in the .app_data section (ram) even if it's declared as const.

Why is there such difference? I expected all the structs to be located in the .rodata section.

 

Many thanks

Teckna

ラベル(1)
0 件の賞賛
返信
1 解決策
1,195件の閲覧回数
BlackNight
NXP Employee
NXP Employee

Hello,

because Test[] is not const. Test is a non-const array of two pointers to const structs.

To make it const, you have to define it as

const TEST *const Test[] =


Erich

元の投稿で解決策を見る

0 件の賞賛
返信
3 返答(返信)
1,196件の閲覧回数
BlackNight
NXP Employee
NXP Employee

Hello,

because Test[] is not const. Test is a non-const array of two pointers to const structs.

To make it const, you have to define it as

const TEST *const Test[] =


Erich

0 件の賞賛
返信
1,195件の閲覧回数
Teckna
Contributor V

Many thanks, Erich,

Teckna

0 件の賞賛
返信
1,195件の閲覧回数
mjbcswitzerland
Specialist V

Hi

See the following for some ideas about forcing more things to Flash - it is for CW7 but may be the same for CW10

reducing ram footprint

Regards

Mark

0 件の賞賛
返信