when does c++ allocate/deallocate string literals

后端 未结 3 619
长发绾君心
长发绾君心 2020-12-10 07:42

When is the string literal \"hello\" allocated and deallocated during the lifetime of the program in this example?

init(char **s)
{ 
  *s = \"hello\";
}
int          


        
相关标签:
3条回答
  • 2020-12-10 08:12

    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.

    0 讨论(0)
  • 2020-12-10 08:16

    The string literal is initialised into read-only memory segment by the compiler. There is no initialisation or removal done at run-time.

    0 讨论(0)
  • 2020-12-10 08:24

    They are not allocated but instead stored in the DATA segment of the executable.

    0 讨论(0)
提交回复
热议问题