When is the string literal "hello" allocated and deallocated during the lifetime of the program in this example?
init(char **s)
{
*s = "hello";
}
int f()
{
char *s = 0;
init(&s);
printf("%s\n", s);
return 0;
}
When is the string literal "hello" allocated and deallocated during the lifetime of the program in this example?
init(char **s)
{
*s = "hello";
}
int f()
{
char *s = 0;
init(&s);
printf("%s\n", s);
return 0;
}
They are not allocated but instead stored in the DATA segment of the executable.
The string literal is initialised into read-only memory segment by the compiler. There is no initialisation or removal done at run-time.
Assuming there is an operating system, the memory containing the string literal is allocated when the OS loads the executable and deallocated when the OS unloads the executable. Exactly when this happens depends on the type of executable (program, shared library, etc.) and the OS.