Page 1 of 1

Duplication in Flash of Const Strings

Posted: Mon Jan 07, 2019 11:51 am
by dedvalson
Hi,

Not sure this is totally the best forum for this but I wanted to see if anyone had a suggestion.

I have an include file with a bunch of const strings in it. Here is a snippet of that include file:

Code: Select all

#pragma once

#include <string>

using namespace std;

const string TypeKey = "type";
const string SpeedKey = "speed";
const string MaxSpeedKey = "maxSpeed";
I include this numerous places. In looking at my memory map I observed that a separate copy of each string is being created for every file that includes it:

Code: Select all

B	S	3FFB5DA8	24	TypeKey	
B	S	3FFB69C8	24	TypeKey	
B	S	3FFB5AC0	24	TypeKey	
B	S	3FFB9B20	24	TypeKey	
B	S	3FFB60A8	24	TypeKey	
B	S	3FFB9330	24	TypeKey	
B	S	3FFB53CC	24	TypeKey	
B	S	3FFB6CCC	24	TypeKey	
B	S	3FFB72D8	24	TypeKey	
B	S	3FFB66C4	24	TypeKey	
B	S	3FFB96A4	24	TypeKey	
B	S	3FFB4D94	24	TypeKey	
B	S	3FFB50CC	24	TypeKey	
B	S	3FFB4788	24	TypeKey	
B	S	3FFB7668	24	TypeKey	
B	S	3FFB4A8C	24	TypeKey	
B	S	3FFB6FD8	24	TypeKey	
B	S	3FFB63C4	24	TypeKey	
I located this while trying to reduce the size of my .bin file. While this isn't a huge deal I was interested to hear any suggestions to prevent it.

Thanks,

Don

Re: Duplication in Flash of Const Strings

Posted: Tue Jan 08, 2019 10:23 am
by ESP_Sprite
That is because every C file that includes the header file has their own private copy of the string. Maybe C++ has a way to fix this in a nicer way, but in C, the 'proper' way would be to declare the strings in the header file (extern const char *mymsg;) and then define them in a C file (const char *mymsg="Hello world!";).