4.3 -- C-Style Character Strings
When we allocate memory, we must eventually free it. Otherwise, memory is gradually used up and may be exhausted. When we no longer need the array, we mustexplicitly return its memory to the free store. We do so by applying thedelete [] expression to a pointer that addresses the array we want to release:
delete [] pia;
deallocates the array pointed to by pia, returning the associated memory to the free store. The empty bracket pair between thedelete keyword and the pointer is necessary:It indicates to the compiler that the pointer addresses an array of elements on the free store and not simply a single object.
Beware:If the empty bracket pair is omitted, it is an error, but an error that the compiler is unlikely to catch; the program may fail at run time.
本文讲解了C风格字符数组在分配内存之后如何通过delete[]操作符显式地释放内存回到自由存储区。文章强调了使用delete[]时方括号的重要性,并指出如果省略方括号可能会导致编译器无法检测到的错误,最终可能引发运行时故障。

被折叠的 条评论
为什么被折叠?



