gcc 4.9.2 bug in -Werror=sizeof-pointer-memaccess?

GCC 在使用 memset 时出现 `-Wsizeof-pointer-memaccess` 警告,原因是将 sizeof 运算应用于指针而非数组。这可能导致不正确的内存填充。解决方法是确保传递给 memset 的长度参数是目标缓冲区的实际大小,而不是指针本身的大小。对于动态分配的内存,需要使用实际分配的长度,而对于字符串,可以使用 strlen 函数来确定长度。
摘要由CSDN通过智能技术生成

You fell straight into the trap.

In C, C++, Objective-C, Objective-C++, a parameter with a declaration that looks like "array of T" actually has type T*.

Your parameter charArray has a declaration that looks like "array of 100 chars", but the declaration is in fact "pointer to char".

Therefore, your third parameter to strncpy has a value of (most likely) 4 or 8, and not the 100 that you seem to expecct.

BTW. strncpy is highly dangerous the way you use it.

link: https://stackoverflow.com/questions/29186056/gcc-4-9-2-bug-in-werror-sizeof-pointer-memaccess

下面的代码中警告是什么原因,怎么解决

memset(m_pBuffer,0,sizeof(m_pBuffer));

warning: argument to 'sizeof' in 'void* memset(void*, int, size_t)' call is the same expression as the destinati

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值