Calloc 提供一个在已分配的内存中开始的指针?
我遇到了某种指针冲突,
基本上,在我做的一个函数中,
a = calloc(1,28); // gives me 0x100100d10
然后很快在我做的一个子函数中,
b = calloc(1,16); // gives me 0x100100d20;
第一个地址+ 28是0x0..d2C,即扩展了第二个calloc中提供的指针..这
是怎么回事?
指针值来自 printf,而不是 gdb。
I'm getting some kind of pointer collision,
Basically, in one function I do,
a = calloc(1,28); // gives me 0x100100d10
Then pretty soon in a subfunction I do,
b = calloc(1,16); // gives me 0x100100d20;
first address + 28 is 0x0..d2C, ie, extends over the pointer provided in the second calloc...
Whats going on here?
Pointer values are from printf, not gdb.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这种情况确实如您所描述的那样发生,那么您已经通过在已分配块的边界之外进行写入(或者甚至可能通过使用未初始化的指针或指向已释放内存的指针)来损坏堆,从而调用未定义的行为。工具 valgrind 可能可以帮助您找到问题所在,或者如果您的程序不是太大,您可以简单地手动搜索无效的指针使用情况。
If this is really happening as you describe, then you have corrupted the heap by writing outside the bounds of an allocated block (or perhaps even by using an uninitialized pointer or pointer to already-freed memory), thus invoking undefined behavior. The tool valgrind can probably help you track the problem down, or if your program isn't too big, you can simply search by hand for invalid pointer usage.