Go Down

Topic: Will F() recognize duplicates? (Read 924 times) previous topic - next topic

__Tango

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!

MarkT

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.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

cmiyc

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)))
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

PeterH

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.

Coding Badly

...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

__Tango


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.

Go Up