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;
}
The string literal is initialised into read-only memory segment by the compiler. There is no initialisation or removal done at run-time.
They are not allocated but instead stored in the DATA segment of the executable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With