c++ pointer scope

前端 未结 4 537
终归单人心
终归单人心 2020-12-25 13:47

What happens when you have the following code:

void makeItHappen()
{
    char* text = \"Hello, world\";
}

Does text go out of

4条回答
  •  有刺的猬
    2020-12-25 14:15

    In the first example the string literal is stored in data segment of your executable.
    In the second case you do not have to call delete (in your example program just terminates) since on program termination the heap is freed anyway for the process.
    Note though that there are OS (as I have read) that you have to explicitly release heap even if the program terminates since it will not be cleaned up at termination for you.
    Of course programmer is responsible for memory management in C++ and objects you create on heap should be deleteed once unneeded.

提交回复
热议问题