C语言 xmalloc 和 malloc 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7590254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between xmalloc and malloc?
提问by vaichidrewar
What is the difference between xmalloc()and malloc()for memory allocation?
Is there any pro of using xmalloc()?
xmalloc()和malloc()for 内存分配有什么区别?
有没有专业人士使用xmalloc()?
回答by orlp
xmalloc()is a non-standard function that has the motto succeed or die. If it fails to allocate memory, it will terminate your program and print an error message to stderr.
xmalloc()是一个非标准函数,其座右铭是success or die。如果它无法分配内存,它将终止您的程序并将错误消息打印到stderr.
The allocation itself is no different; only the behaviour in the case that no memory could be allocated is different.
分配本身没有什么不同;只有在无法分配内存的情况下的行为不同。
Use malloc(), since it's more friendly and standard.
使用malloc(),因为它更友好和标准。
回答by R.. GitHub STOP HELPING ICE
xmallocis not part of the standard library. It's usually the name of a very harmful function for lazy programmers that's common in lots of GNU software, which calls abortif mallocfails. Depending on the program/library, it might also convert malloc(0)into malloc(1)to ensure that xmalloc(0)returns a unique pointer.
xmalloc不是标准库的一部分。对于懒惰的程序员来说,它通常是一个非常有害的函数的名称,这在许多 GNU 软件中很常见,abort如果malloc失败就会调用。根据程序/库,它也可能转换malloc(0)为malloc(1)以确保xmalloc(0)返回唯一的指针。
In any case, aborting on mallocfailure is very very bad behavior, especially for library code. One of the most infamous examples is GMP (the GNU multiprecision arithmetic library), which aborts the calling program whenever it runs out of memory for a computation.
在任何情况下,abort荷兰国际集团的malloc失败是非常非常恶劣的行为,特别是对库中的代码。最臭名昭著的例子之一是 GMP(GNU 多精度算术库),它在计算内存不足时中止调用程序。
Correct library-level code should always handle allocation failures by backing out whatever partially-completed operation it was in the middle of and returning an error code to the caller. The calling program can then decide what to do, which will likely involve saving critical data.
正确的库级代码应该总是通过回退它在中间的任何部分完成的操作并向调用者返回错误代码来处理分配失败。然后调用程序可以决定要做什么,这可能涉及保存关键数据。
回答by evadeflow
As others have mentioned, it's true that xmallocis very often implemented as a wrapper function that invokes the OS-supplied mallocand blindly calls abortor exitif it fails. However, manyprojects contain an xmallocfunction that tries to save application state before exiting (see, for example, neovim).
正如其他人所提到的,确实xmalloc经常作为包装函数实现,该函数调用操作系统提供的malloc盲目调用abort或exit失败。但是,许多项目包含一个xmalloc函数,该函数尝试在退出之前保存应用程序状态(例如,请参见neovim)。
Personally, I think of xmallocas a kind of project-specific extendedmallocrather than an exitingmalloc. Though I don't recall ever seeing a version that didn'twind up calling abortor exit, some of them do a lotmore than that.
就个人而言,我认为xmalloc是一种特定于项目的扩展malloc而不是现有的malloc. 虽然我不记得曾经看到一个版本不拉闸打电话abort或者exit,他们有的做了很多不止于此。
So the answer to the question "What's the difference between xmallocand mallocis: it depends. xmallocis a non-standard, project-specific function, so it could do anything at all. The only way to know for sure is to read the code.
因此,“xmalloc和之间有什么区别malloc:这取决于。xmalloc是一个非标准的、特定于项目的功能,因此它可以做任何事情。唯一确定的方法是阅读代码。
回答by spikeysnack
an primitive example of xmalloc.c in K&R C
K&R C 中 xmalloc.c 的原始示例
#include <stdio.h>
extern char *malloc ();
void *
xmalloc (size)
unsigned size;
{
void *new_mem = (void *) malloc (size);
if (new_mem == NULL)
{
fprintf (stderr, "fatal: memory exhausted (xmalloc of %u bytes).\n", size);
exit (-1);
}
return new_mem;
}
then in your code header (early) you put
然后在您的代码标题(早期)中放置
#define malloc(m) xmalloc(m)
#define malloc(m) xmalloc(m)
to silently rewrite the source before compilation. (you can see the rewritten code by invoking the C preprocessor directly and saving the output. )
在编译之前默默地重写源代码。(直接调用C预处理器保存输出就可以看到改写的代码了。)
if crashing your program is not what you want you can do something different
如果程序崩溃不是你想要的,你可以做一些不同的事情
- Use a garbage collector
- redesign your code to be less of a memory hog
- have error checking code in your program to handle an Out of Memory or other allocation error gracefully.
- 使用垃圾收集器
- 重新设计您的代码以减少内存占用
- 在你的程序中有错误检查代码来优雅地处理内存不足或其他分配错误。
Users don't enjoy losing their data to a built-in crash command in their program.
用户不喜欢因程序中的内置崩溃命令而丢失数据。
回答by Albert
I have seen xmalloc while working on IBM AIX. xmalloc is a kernel service provided by AIX.
我在 IBM AIX 上工作时见过 xmalloc。xmalloc 是 AIX 提供的内核服务。
Nothing can explain a function better than the function's man page in my opinion. So I am pasting the below details from the man page
在我看来,没有什么比函数的手册页更好地解释函数了。所以我从手册页粘贴以下详细信息
Purpose: Allocates memory.
目的:分配内存。
Syntax:
句法:
caddr_t xmalloc ( size, align, heap)
caddr_t xmalloc(大小、对齐、堆)
Parameters:
参数:
size: Specifies the number of bytes to allocate.
size:指定要分配的字节数。
align: Specifies the alignment characteristics for the allocated memory.
align:指定分配内存的对齐特性。
heap : Specifies the address of the heap from which the memory is to be allocated.
heap :指定要从中分配内存的堆的地址。
Description:
描述:
The xmalloc kernel service allocates an area of memory out of the heap specified by the heap parameter. This area is the number of bytes in length specified by the size parameter and is aligned on the byte boundary specified by the align parameter. The align parameter is actually the log base 2 of the desired address boundary. For example, an align value of 4 requests that the allocated area be aligned on a 2^4 (16) byte boundary.
xmalloc 内核服务从 heap 参数指定的堆中分配一块内存区域。该区域是 size 参数指定的长度字节数,并在 align 参数指定的字节边界上对齐。align 参数实际上是所需地址边界的对数基数 2。例如,对齐值 4 请求分配的区域在 2^4 (16) 字节边界上对齐。
There are multiple heaps provided by the kernel for use by kernel extensions. Two primary kernel heaps are kernel_heap and pinned_heap. Kernel extensions should use the kernel_heap value when allocating memory that is not pinned, and should use the pinned_heap value when allocating memory that should always be pinned or pinned for long periods of time. When allocating from the pinned_heap heap, the xmalloc kernel service will pin the memory before a successful return. The pin and unpin kernel services should be used to pin and unpin memory from the kernel_heap heap when the memory should only be pinned for a limited amount of time. Memory from the kernel_heap heap must be unpinned before freeing it. Memory from the pinned_heap heap should not be unpinned.
内核提供了多个堆供内核扩展使用。两个主要的内核堆是 kernel_heap 和 pinned_heap。内核扩展在分配未固定的内存时应使用 kernel_heap 值,在分配应始终固定或长期固定的内存时应使用 pinned_heap 值。当从 pinned_heap 堆分配时,xmalloc 内核服务会在成功返回之前 pin 内存。当内存只应固定有限的时间时,应使用 pin 和 unpin 内核服务从 kernel_heap 堆中固定和取消固定内存。kernel_heap 堆中的内存必须在释放之前解除锁定。pinned_heap 堆中的内存不应取消固定。
If one is interested in knowing more about this function can visit the following link: IBM AIX Support
如果有兴趣了解有关此功能的更多信息,可以访问以下链接: IBM AIX 支持

