Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22016

Can I store pointer to __PRETTY_FUNCTION__ for later use or do I need to copy the string immediately?

$
0
0

What is the lifetime of the string __PRETTY_FUNCTION__ produces?

I know __func__ generates equivalent of static const char __func__[] = "function-name";, Thanks to that static, if I want to create a record of something that happened in the function somewhere for referencing later, I can just store the value of __func__ in a const char* field in the record and retrieve it whenever I want, long after I left the scope of the function.

Does __PRETTY_FUNCTION__ behave the same way, or does the string it generates behave as a local, temporary value? Say, will this work reliably, or can it fail, fname pointing to a location where the function name used to be, and I need to do make a buffer and do strncpy() or similar instead?

#include <stdio.h>const char* fname = NULL;void foo(){    fname = __PRETTY_FUNCTION__;}int main(){    foo();    printf(fname);    return 0;}

Viewing all articles
Browse latest Browse all 22016

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>