Take a function that creates a char*
and does something with it.
void print_hello_twice()
{
char* hello = "hello";
printf(hello);
printf(hello);
}
This is a silly pointles example, but I wanted to avoid anything extraneous to my question, which is this:
I've read that when you assign a string literal to char* variable:
The actual string literal "hello" is stored in the read only memory section. The variable "word" though, is just a pointer created in the stack that as a value, holds the address of the first element of the string.
Memory allocation when using struct to defined a user defined type
So, does this mean that this is a potential source of a memory leak? What happens to the piece of memory containing "hello" itself (not the pointer to it) once print_hello_twice
returns? Is it freed up in some way?