我怎么能知道哪里的内存段是全零 [英] How can I know where the segment of memory is all Zero

查看:119
本文介绍了我怎么能知道哪里的内存段是全零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是,我的malloc存储器段,也许1K也许20字节..
假设指针 PMEM
我怎么能知道内容 PMEM 所指向都是 \\ 0
。我知道 memcmp ,但第二个参数应该另一内存地​​址...
感谢名单

I mean, I malloc a segment of memory, maybe 1k maybe 20bytes.. assume the pointer is pMem How can I know the content pMem refered is all Zero or \0 . I know memcmp but the second parameter should another memory address... thanx

推荐答案

正如其他人已经建议你可能想的 memset的释放calloc

As others have already suggested you probably want memset or calloc.

但如果你实际上要检查的内存区域都是零,你可以与自己进行比较,但移动一位。

But if you actually do want to check if a memory area is all zero, you can compare it with itself but shifted by one.

bool allZero = pMem[0] == '\0' && !memcmp(pMem, pMem + 1, length - 1);

,其中长度为你想成为零字节数。

where length is the number of bytes you want to be zero.

这篇关于我怎么能知道哪里的内存段是全零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆