Chapter 4 Arrays and Pointers

本文讲解了C风格字符数组在分配内存之后如何通过delete[]操作符显式地释放内存回到自由存储区。文章强调了使用delete[]时方括号的重要性,并指出如果省略方括号可能会导致编译器无法检测到的错误,最终可能引发运行时故障。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值