The following line of code const char __attribute__((progmem)) var = "Hallo"[0]; compileds with avr-g++ progmem.c -S -Os compiles code that tries to initialize var at run time: .section .text.startup,"ax",@progbits .type _GLOBAL__sub_I_progmem.c, @function _GLOBAL__sub_I_progmem.c: ldi r24,lo8(72) sts _ZL3var,r24 ret .size _GLOBAL__sub_I_progmem.c, .-_GLOBAL__sub_I_progmem.c .global __do_global_ctors .section .ctors,"a",@progbits .word gs(_GLOBAL__sub_I_progmem.c) .local _ZL3var .comm _ZL3var,1,1 As var is located in flash and thus cannot be initialized at runtime, there should be an error message like "progmem variable var cannot be initialized at load time".
Confirmed with 4.6.2 The generated code is a bit different. var is put into the correct section .progmem but there is no error and the constructor will write to RAM: .section .text.startup,"ax",@progbits .type _GLOBAL__sub_I_progmem.c, @function _GLOBAL__sub_I_progmem.c: ldi r24,lo8(72) sts _ZL3var,r24 ret .size _GLOBAL__sub_I_progmem.c, .-_GLOBAL__sub_I_progmem.c .global __do_global_ctors .section .ctors,"a",@progbits .word gs(_GLOBAL__sub_I_progmem.c) .section .progmem.data,"a",@progbits .type _ZL3var, @object .size _ZL3var, 1 _ZL3var: .skip 1,0
GCC 4.6.2 is being released.
GCC 4.6.3 is being released.
Is this reproduceable with 4.7.3, 4.8.0 or the trunk?
(In reply to Jakub Jelinek from comment #4) > Is this reproduceable with 4.7.3, 4.8.0 or the trunk? With the test case from above and 4.8.0, no code is generated at all. With an extended test case as follows, the constructor still writes to RAM in insn 6: const char __attribute__((progmem)) var = "Hallo"[0]; const char *foo (void) { return &var; } _GLOBAL__sub_I__Z3foov: ldi r24,lo8(72) ; 5 movqi_insn/2 [length = 1] sts _ZL3var,r24 ; 6 movqi_insn/3 [length = 2] ret ; 13 return [length = 1] And there is a warning from the back end: foo.c: In function 'const char* foo()': foo.c:1:37: warning: uninitialized variable 'var' put into program memory area [-Wuninitialized] const char __attribute__((progmem)) var = "Hallo"[0];
Works in 4.9.