c/c++内存操作函数 是否初始化

本文详细介绍了C语言中malloc、calloc函数的使用方法及内存初始化的重要性,包括新操作未初始化内存的风险和解决办法。

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

C
void * malloc ( size_t size );

Allocate memory block
Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.

The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.

void * calloc ( size_t num, size_t size );

Allocate space for array in memory
Allocates a block of memory for an array of num elements, each of them size bytes long, and initializes all its bits to zero.

The effective result is the allocation of an zero-initialized memory block of (num * size) bytes.

c++

new

new操作不会初始化内存

总之 需要需要对内存初始化为0  安全第一

strcpy  后面跟了0
strncpy 后面没跟0


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值