Why const is not const?

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

Why const is not const?

Jump to solution
1,360 Views
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

Labels (1)
0 Kudos
Reply
1 Solution
1,185 Views
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

View solution in original post

0 Kudos
Reply
3 Replies
1,186 Views
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 Kudos
Reply
1,185 Views
Teckna
Contributor V

Many thanks, Erich,

Teckna

0 Kudos
Reply
1,185 Views
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 Kudos
Reply