[英]What is the limit on malloc parameter of type size_t in C? Docs say it has an upper limit of UINT_MAX but I can't go beyond INT_MAX
I want to allocate a 2.9GB char array with 我想分配一个2.9GB的char数组
database = (char*) malloc((2900 * 1000000 * sizeof(char)));
This gives an integer overflow warning and the malloc
returns NULL
. 这给出了整数溢出警告, malloc
返回NULL
。 The malloc
parameter is of type size_t
which according to documentation is of type unsigned int
. malloc
参数的类型为size_t
,根据文档的类型为unsigned int
。
So the max should be UINT_MAX
which is at least 2.9GB. 所以max应该是UINT_MAX
,至少是2.9GB。 However, if I try to allocate more than MAX_INT
the malloc
fails. 但是,如果我尝试分配超过MAX_INT
则malloc
失败。 Does this mean size_t
on my system is of type int? 这是否意味着我的系统上的size_t
是int类型? How do I check this? 我该如何检查? I looked through 我看了看
/usr/include/stdlib.h
and 和
./lib/gcc/x86_64-redhat-linux/4.1.1/include/stddef.h
but can't find the definition of size_t
. 但是找不到size_t
的定义。 Thanks very much 非常感谢
The parameter is of type size_t
and malloc
is required to accept any possible value of type size_t
. 该参数的类型为size_t
并且malloc
需要接受size_t
类型的任何可能值。 Note that "accept" does not meant it is required to allocate that much; 请注意,“接受”并不意味着需要分配那么多; all it means is that malloc
is not allowed to misinterpret a very large number you give it as a small/negative number due to overflow issues, thereby returning a buffer that's too small and creating a critical undetectable vulnerability your program cannot defend against. 所有这一切意味着,由于溢出问题,不允许malloc
误解你给它的一个很小的数字作为一个小/负数,从而返回一个太小的缓冲区,并创建一个程序无法防范的关键不可检测的漏洞。 There are many possible reasons malloc
could fail to allocate very large objects: malloc
可能无法分配非常大的对象有很多可能的原因:
In this case I suspect you might be seeing the third, arbitrary limits, though I would not consider them so arbitrary. 在这种情况下,我怀疑你可能会看到第三个任意限制,但我不认为它们是如此随意。 There's a very good reason to disallow allocations (and the existence of any objects) larger than SIZE_MAX/2
: taking the difference between pointers within such large objects will result in (extremely dangerous) integer overflow and undefined behavior when the result does not fit in the (signed) type ptrdiff_t
. 有一个很好的理由禁止大于SIZE_MAX/2
分配(以及任何对象的存在):当这些大对象中的指针之间的差异将导致(非常危险的)整数溢出和未定义的行为,当结果不适合(签名)类型ptrdiff_t
。 Thus, on a robust 32-bit system, while the virtual address space size is 4GB, the maximum size of any single object will be 2GB. 因此,在强大的32位系统上,当虚拟地址空间大小为4GB时,任何单个对象的最大大小将为2GB。
There are two issues here. 这里有两个问题。
First, the overflow warning: both 2900
and 1000000
are of type int
, so the result of multiplying them is also of type int
. 首先,溢出警告: 2900
和1000000
都是int
类型,因此乘以它们的结果也是int
类型。 The result cannot be represented by a 32-bit signed integer, so it overflows. 结果不能用32位有符号整数表示,因此它会溢出。 You need to cast one (or both) arguments to size_t
to use unsigned arithmetic. 您需要将一个(或两个)参数size_t
转换为size_t
以使用无符号算术。
(Or, you could move the sizeof(char)
to be one of the first two terms, since its type is size_t
, though you can also just remove the sizeof(char)
since it is always 1
.) (或者,您可以将sizeof(char)
移动为前两个术语之一,因为它的类型是size_t
,但您也可以删除sizeof(char)
因为它总是为1
)
Second, the maximum size that malloc
can allocate depends both on the platform on which you are running and on the current state of the program. 其次, malloc
可以分配的最大大小取决于您运行的平台和程序的当前状态。 If there is insufficient contiguous address space left to satisfy the request, obviously the malloc
will fail. 如果没有足够的连续地址空间来满足请求,很明显malloc
将会失败。
Further, the platform on which you are running may have an upper limit on how large an object it can dynamically allocate. 此外,您运行的平台可能有一个上限,它可以动态分配一个对象的大小。 You'll need to consult your platform's documentation to find out what that upper limit is. 您需要查阅平台的文档以了解该上限是什么。
size_t
is certainly not int
, because int
is always signed and size_t
is always unsigned. size_t
肯定不是int
,因为int
总是有符号的,而size_t
总是无符号的。
The maximum size that malloc can allocate depends both on the platform on which you are running and on the current state of the program. malloc可以分配的最大大小取决于您运行的平台和程序的当前状态。 If there is insufficient contiguous address space left to satisfy the request, the malloc will fail obviously. 如果没有足够的连续地址空间来满足请求,malloc将明显失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.