标题已经说明了一切。
我发现一个旧的问题基本上是相同的, 但是我需要更进一步的澄清。
在这个问题中,被接受的答案说:
在这个问题中,被接受的答案说:
这是否意味着指针被删除,但指针所指向的数据不会被删除? 如果我在函数中创建1,000,000个字符指针,在它们超出范围时,所有内存都会被释放吗?还是只释放制作指针所需的内存,留下实际的字符本身来占用所有内存?char* text = "Hello, world";
Here an automatic variable (a pointer) is created on the stack and set to point to a value in constant memory, which means:
- the string literal in "" exists through the whole program execution.
- you are not responsible for "allocating" or "freeing" it
- you may not change it. If you want to change it, then you have to allocate some "non-constant memory" and copy it there.