Reputation: 32912
void foo(const char* s) { }
foo("bar");
bar
deallocated?Upvotes: 1
Views: 238
Reputation: 139
"bar" is defined in the data section and the address will replace it in all the places you have it. The foo function will be called with a pointer to that address.
Upvotes: 2
Reputation: 154027
In your example, the argument is a string literal, which has static lifetime, and is never deleted.
Upvotes: 4
Reputation: 3024
The memory for bar is allocated at compile time. Thus, it never has to be deallocated.
There are different sections in a c++ binary. A few examples are text (where the code is stored), the stack, and the heap. There's also a section of read-only static memory. I believe that is where the strings would be stored.
Since the string is not on the heap, it does not need to be freed.
Upvotes: 1