Arduino Forum

Using Arduino => Programming Questions => Topic started by: __Tango on Dec 31, 2013, 02:25 am

Title: Will F() recognize duplicates?
Post by: __Tango on Dec 31, 2013, 02:25 am
When using the F() syntax for strings stored in flash/progmem, what will happen if i do something like this:

loop() {
  Serial.println(F("This string will be stored in flash memory"));
  Serial.println(F("This string will be stored in flash memory"));
  Serial.println(F("This string will be stored in flash memory"));
  Serial.println(F("This string will be stored in flash memory"));
  Serial.println(F("This string will be stored in flash memory"));
}

Obviously this is a bit of a contrived example, but what i'd like to know is if 5 copies of "This string will be stored in flash memory" will be stored in flash memory.  When manually setting up PROGMEM, since you usually reference strings by array index, it's easy to avoid duplication.  With this handy-dandy new syntax, i'm wondering how to do the same.

Thanks!
Title: Re: Will F() recognize duplicates?
Post by: MarkT on Dec 31, 2013, 02:31 am
Try it?  Look at the source to F()?  That's what we'd do to answer the question. Normally you would avoid
duplicates when programming for a small microcontroller.
Title: Re: Will F() recognize duplicates?
Post by: cmiyc on Dec 31, 2013, 02:31 am
As written:
Binary sketch size: 1,842 bytes (of a 32,256 byte maximum)

With only one:
Binary sketch size: 1,620 bytes (of a 32,256 byte maximum)

With all but one string replaced with "" (so its the same number of Serial.print() calls):
Binary sketch size: 1,674 bytes (of a 32,256 byte maximum)

So no, the simple F() macro doesn't find duplicates.
F():
WString.h:#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
Title: Re: Will F() recognize duplicates?
Post by: PeterH on Dec 31, 2013, 02:34 am
I suppose the compiler documentation would probably tell you if you dug far enough, but a quick test using your example and just changing one character in each string to make them unique didn't change the program storage space used, so I guess that duplicate literal values are not being merged.
Title: Re: Will F() recognize duplicates?
Post by: Coding Badly on Dec 31, 2013, 02:39 am
...I guess that duplicate literal values are not being merged.


Half correct ( ;))...
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=539904#539904
Title: Re: Will F() recognize duplicates?
Post by: __Tango on Dec 31, 2013, 06:53 am

As written:
Binary sketch size: 1,842 bytes (of a 32,256 byte maximum)

With only one:
Binary sketch size: 1,620 bytes (of a 32,256 byte maximum)

With all but one string replaced with "" (so its the same number of Serial.print() calls):
Binary sketch size: 1,674 bytes (of a 32,256 byte maximum)

So no, the simple F() macro doesn't find duplicates.
F():
WString.h:#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))


Great. This is the info i was looking for.