Will the pointer to __FILE__ remain valid after leaving the scope?

If I want to pass to an __FILE__object, do I need to make a copy of the string, or can I just save its pointer as const char*?

I guess I need to make a copy, as I think she will be free when she goes out of scope.

Does the pointer indicate __FILE__any warranty?

+4
source share
3 answers

__FILE__expands to a regular string literal. Therefore, while you are only reading this, you can simply use const char*as

const char *filename = __FILE__;

Since string literals have a static storage duration, the pointer will remain valid throughout the program.

- , .

, , " " .

@Keith Thompson , __FILE__ , :

const char *f1 = __FILE__;
const char *f2 = __FILE__;
assert(f1 == f2);              // May or may not fire

, , , .

+8

__FILE__ - , . , "filename.c" . , - , .

+3

, , , , .

, . .

__FILE__ - ?

. , const char[], , .

+1

All Articles