| Author |
Message |
|
|
Posted: Oct 24, 2011 - 02:52 PM |
|

Joined: Aug 06, 2008
Posts: 141
Location: Montréal, QC
|
|
forked from the 4.6.1 LTO thread
SprinterSB wrote:
Code:
#include <avr/pgmspace.h>
#define PGM_STR(X) ((const __pgm char[]) { X })
char const __pgm *gstr = PGM_STR ("123");
char const __pgm text[] = "abc";
void foo (const char __pgm *str2)
{
void lcd_print_P (const char __pgm*);
const char __pgm *str = PSTR ("0123");
static const char __pgm stext[] = "abc";
str2 = PSTR ("abc");
lcd_print_P (str);
lcd_print_P (str2);
lcd_print_P (gstr);
lcd_print_P (text);
lcd_print_P (stext);
}
Thanks for the example, what is the diff between
char const __pgm *gstr = PGM_STR ("123");
and
const char __pgm *str = PSTR ("0123");
?
Also using
lcd_print_P(PSTR("abc"))
put the string in flash, but
lcd_print_P((const char __pgm*)"abc")
does not.
and this
lcd_print_P(PGM_STR("abc"))
gives an error compound literal qualified by address-space qualifier
If we use __pgm can we still use the PSTR macro?
Thanks in advance for all your help! I also noted you have a new GCC4.7 build, but w/o the pgm feature? |
Last edited by Magister on Oct 24, 2011 - 07:18 PM; edited 2 times in total
|
| |
|
|
|
|
|
Posted: Oct 24, 2011 - 03:05 PM |
|

Joined: Dec 16, 2005
Posts: 3086
Location: Bratislava, Slovakia
|
|
Magister,
You surely are aware of the substantial difference of
Code:
char * s = "abc";
and
Code:
char s[] = "abc";
?
http://c-faq.com/decl/strlitinit.html
I guess Johann was trying some syntactic magic with the PGM_STR macro but I don't quite like it.
JW |
|
|
| |
|
|
|
|
|
Posted: Oct 24, 2011 - 05:54 PM |
|


Joined: Dec 21, 2006
Posts: 1483
Location: Saar-Lor-Lux
|
|
If you don't like it, you need not use it.
The caveats of initializing pointers with string literals to be located in flash are outlined in
http://www.avrfreaks.net/index.php?name ... 511#883511
The problem is that PSTR does not work ouside of function and PGM_STR does not work inside of functions.
This is all because of dreaded standard (extension) because such case of initializing pointers have been overlooked, IMO.
Magister wrote:
Code:
(const char __pgm*) "abc"
This locates "abc" in RAM and casts the pointer to a pointer to flash.
That's what I mean with error prone. |
|
|
| |
|
|
|
|
|
Posted: Oct 24, 2011 - 06:01 PM |
|

Joined: Aug 06, 2008
Posts: 141
Location: Montréal, QC
|
|
I saw this, I kept PSTR inside functions. Everything works fine, my *_P functions do not use the pgm_read_byte() anymore, the compiler use LPM automatically.
Final size is the same between PROGMEM/pgm_read_byte() and using the new __pgm, good job! |
|
|
| |
|
|
|
|
|
Posted: Oct 24, 2011 - 06:24 PM |
|


Joined: Dec 21, 2006
Posts: 1483
Location: Saar-Lor-Lux
|
|
| I'd expect that the size with the new feature increases, in particular if you read structures with odd size like 3 bytes that you read byte-wise beforehand. |
_________________ avr-gcc News • ABI • Options • 4.8-Windows • Inline Asm
|
| |
|
|
|
|
|