using __pgm with GCC 4.7

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

forked from the 4.6.1 LTO thread

SprinterSB wrote:
#include 

#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: Mon. Oct 24, 2011 - 06:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Magister,

You surely are aware of the substantial difference of

char * s = "abc";

and

char s[] = "abc";

?

http://c-faq.com/decl/strlitinit...

I guess Johann was trying some syntactic magic with the PGM_STR macro but I don't quite like it.

JW

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

https://www.avrfreaks.net/index.p...

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:

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

avrfreaks does not support Opera. Profile inactive.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

avrfreaks does not support Opera. Profile inactive.