风起云涌的时间 2019-07-24 23:06 采纳率: 100%
浏览 1303
已采纳

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

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 destination; did you mean to provide an explicit length? [-Wsizeof-pointer-memaccess]
memset(m_pBuffer,0,sizeof(m_pBuffer));
^

  • 写回答

4条回答 默认 最新

  • 觉行无量 2019-07-25 12:41
    关注

    首先,m_pBuffer应该是个成员指针变量,不是数组名吧。sizeof 运算符对“指针变量”运算的结果是指针变量的字节大小(32位4字节,64位8字节),可不是其指向的数据的大小哦。
    对数组名运算是为数组分配的空间的字节大小。
    其次,这个警告:**-Wsizeof-pointer-memaccess**

    Warn for suspicious length parameters to certain string and memory built-in functions if the argument uses sizeof.
    This warning warns e.g. about memset (ptr, 0, sizeof (ptr)); if ptr is not an array, but a pointer, and suggests a possible fix, or about memcpy (&foo, ptr, sizeof (&foo));. This warning is enabled by -Wall.
    gcc编译器检测到可疑的使用sizeof对指针做了运算而不是数组,所以给了个温馨提示哦。

    怎么解决:
    对指针指向的数据做sizeof,如果是new分配的还要乘上数量了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大