Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PSTR() on __FUNCTION__

Using avr-gcc it is possible to store data on Program Memory in order to save RAM. This is acomplished using PROGMEM attribute. AVR-libc also provides a macro, PSTR(), which can be used to with literal strings.

Now I'm trying to use PSTR() with __func__, __FUNCTION__ or __PRETTY_FUNCTION__.

The following code works as expected,

display.message(__func__, 2);
display.message(__FUNCTION__, 2);
display.message(__PRETTY_FUNCTION__, 2);

, while the compilation of any of these lines fails,

display.messageP(PSTR(__func__), 2);
display.messageP(PSTR(__FUNCTION__), 2);
display.messageP(PSTR(__PRETTY_FUNCTION__), 2);

, with the following error:

initializer fails to determine size of '__c'

The definition of PSTR, from WinAVR/avr/include/avr, explains the variable referenced on the error message:

# define PSTR(s) (__extension__({static char __c[] PROGMEM = (s); &__c[0];}))

This is not something general to macros, as __FILE__ compile and work fine:

display.messageP(PSTR(__FILE__), 2);

Any ideas on what is causing this error, and if it possible to use PSTR() to insert function name on program memory?

like image 323
mMontu Avatar asked Jul 31 '13 16:07

mMontu


People also ask

What is __ function __ in C++?

(C++11) The predefined identifier __func__ is implicitly defined as a string that contains the unqualified and unadorned name of the enclosing function. __func__ is mandated by the C++ standard and is not a Microsoft extension.

What is Pstr in C?

program memory string constant. a macro which puts a string into flash memory.


1 Answers

__func__, __FUNCTION__ and __PRETTY_FUNCTION__ are not string literals, like __FILE__, but are created like static local char array variables to the function you are using them from. So, the PSTR() macro will fail since you cannot use an array variable to initialize another array variable like that.

__func__ is described in C11, §6.4.2.2 ¶1:

The identifier __func__ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration

static const char __func__[] = "function-name";
appeared, where function-name is the name of the lexically-enclosing function.

According to the GCC manual, __FUNCTION__ and __PRETTY_FUNCTION__ are just synonyms for __func__ in C (__PRETTY_FUNCTION__ is more verbose for C++).

like image 69
jxh Avatar answered Sep 23 '22 00:09

jxh