是否指针传递给free()必须指向内存块的开始,也可以指向内部? [英] Does the pointer passed to free() have to point to beginning of the memory block, or can it point to the interior?

查看:161
本文介绍了是否指针传递给free()必须指向内存块的开始,也可以指向内部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是在标题...我搜查,但没有找到任何东西。

The question is in the title... I searched but couldn't find anything.

编辑:

我真的没有看到任何需要解释这个,而是因为人们认为我在说什么是没有意义的(以及我问错了问题),这里有一个问题:

I don't really see any need to explain this, but because people think that what I'm saying makes no sense (and that I'm asking the wrong questions), here's the problem:

由于人们似乎的非常的兴趣在所有的问题,而不是问实际问题的根的原因(因为这显然有助于事情得到更好地解决,让我们看看它)这里有一个问题:

Since people seem to be very interested in the "root" cause of all the problem rather than the actual question asked (since that apparently helps things get solved better, let's see if it does), here's the problem:

我试图使基于NTDLL.DLL A D运行时库,这样我就可以使用该库比Win32子系统等子系统。所以这迫使我只能用ntdll.dll中的链接。

I'm trying to make a D runtime library based on NTDLL.dll, so that I can use that library for subsystems other than the Win32 subsystem. So that forces me to only link with NTDLL.dll.

是的,我知道该功能是无证,并可以在任何时间(即使我敢打赌一百元的 wcstombs 仍然会做改变同样的确切的东西20年后,如果仍存在)。是的,我知道人们(尤其是微软)不喜欢​​开发商链接到该库,那我可能会得到批评的就在这里。是的,这两点上面的意思是,像CHKDSK程序和磁盘碎片整理程序运行的的Win32子系统都甚至不应该在第一时间被创建,因为它根本不可能像KERNEL32任何链接.dll文件或MSVCRT.DLL,仍然有NT-本地可执行文件,所以我们开发商应该只是pretend那些阶段,目的是要永远我们达到的。

Yes, I'm aware that the functions are "undocumented" and could change at any time (even though I'd bet a hundred dollars that wcstombs will still do the same exact thing 20 years from now, if it still exists). Yes, I know people (especially Microsoft) don't like developers linking to that library, and that I'll probably get criticized for the right here. And yes, those two points above mean that programs like chkdsk and defragmenters that run before the Win32 subsystem aren't even supposed to be created in the first place, because it's literally impossible to link with anything like kernel32.dll or msvcrt.dll and still have NT-native executables, so we developers should just pretend that those stages are meant to be forever out of our reaches.

不过的没有的,我怀疑有人在这里想我贴几千行code,并帮我看一下通过他们,并尝试找出原因的内存分配是不没有正在由源$ C ​​$ C我修改拒绝。所以这就是为什么我问了一下比根的原因不同的问题,尽管这是理应知道是由社区的最佳实践。

But no, I doubt that anyone here would like me to paste a few thousand lines of code and help me look through them and try to figure out why memory allocations that aren't failing are being rejected by the source code I'm modifying. So that's why I asked about a different problem than the "root" cause, even though that's supposedly known to be the best practice by the community.

如果事情还没有意义,随意下面发表评论! :)

If things still don't make sense, feel free to post comments below! :)

编辑2:

围绕〜8小时调试之后,我终于发现了问题:

After around ~8 hours of debugging, I finally found the problem:

原来, RtlReAllocateHeap()做的的自动工作,就像 RtlAllocateHeap()如果给它的指针 NULL

It turns out that RtlReAllocateHeap() does not automatically work like RtlAllocateHeap() if the pointer given to it is NULL.

推荐答案

它指向块的开始。它是安全的一个空指针传递给免费(),但传球)不是分配的任何指针的malloc(或一个其亲属将导致不确定的行为。有些系统会给你一个运行时错误 - 这是沿

It has to point to the beginning of the block. It's safe to pass a null pointer to free(), but passing any pointer not allocated by malloc() or one of its relatives will cause undefined behaviour. Some systems will give you a runtime error - something along the lines of "Deallocation of a pointer not malloced".

编辑:

我写了一个测试程序,以获得错误信息。在我的机器,这个程序:

I wrote a test program to get the error message. On my machine, this program:

#include <stdlib.h>

int main(int argc, char **argv)
{
  int *x = malloc(12);
  x++;

  free(x);

  return 0;
}

此消息崩溃:

app(31550) malloc: *** error for object 0x100100084: pointer being freed was not allocated

这篇关于是否指针传递给free()必须指向内存块的开始,也可以指向内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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