avr-libc-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-libc-dev] PSTR() and duplicate strings


From: Benoît Ryder
Subject: [avr-libc-dev] PSTR() and duplicate strings
Date: Mon, 4 Apr 2011 23:11:37 +0200

Hi,

I have a question about the use of PSTR(), optimizations and objects
located in program space.
With optimizations enabled the compiler detect several occurrences of
identical constant strings to store them only once in the binary.
However, when using PSTR(), these optimizations are not made and the
string is duplicated.

For instance, using the following code, the string "123" is stored
only once in the resulting binary:
  fprintf(stdout, "123");
  fprintf(stdout, "123");

With the following code, the string "123" is stored twice, resulting
in a 4-byte overhead.
  fprintf(stdout, PSTR("123"));
  fprintf(stdout, PSTR("123"));

This does not matter much for small strings but the overhead can
become pretty large with numerous large strings (e.g. verbose log
messages).

There are two definitions of PSTR(). A "fake" definition intended for Doxygen:
  #define PSTR(s) ((const PROGMEM char *)(s))
And the "real one":
  #define PSTR(s) (__extension__({static char __c[] PROGMEM = (s); &__c[0];}))

When using the fake version, duplicates are optimized and strings are
located in the flash ROM, as excepted. The real version prevent
optimizations to be made (which is an expected behavior, according to
the definition code).
So, my question is: is there an actual difference between the two
definitions of PSTR() (besides the fact that pointer addresses will
differ)? May I safely switch to the fake definition to benefit from
the optimizations?

Thanks!

Benoît Ryder



reply via email to

[Prev in Thread] Current Thread [Next in Thread]