当我们调用malloc负放慢参数会发生什么? [英] what happens when we call Malloc with negative paramter?
问题描述
7.22.3.4 malloc函数malloc函数的一个对象,其大小由size指定,其值是分配空间
不确定的。
7.22.3.4 The malloc function The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.
函数原型:的void * malloc的(为size_t大小);
我试图传递一个负面的值作为参数:的malloc(负)
收益 NULL
I tried passing a negative value as a parameter: malloc(negative)
returns NULL
.
是不是因为[为size_t]阴性转换为无[一些大值]并且无法分配所需的空间或功能检查参数,并返回 NULL
?
Is it because the [size_t] negative converted to unsigned [some big value] and cannot allot required space or is the function checking parameter and returns NULL
?
如果它得到转化为大利好,再打电话时的malloc(INT_MIN + 2)
它仍然会返回 NULL
,但获分配的malloc为指针(0)
和 * p = someValue中
的作品。这个是什么?
If its getting converted to big positive, then when calling malloc(INT_MIN+2)
it still returns NULL
, but malloc(0)
alloted to pointer and *p = somevalue
works. What about this?
难道是实现定义的?
阅读此链接:的malloc(0)
推荐答案
A 为size_t
值始终是正的,即使你传递一个负值到的malloc
。负值被转换成键入为size_t
的无符号值导致了巨大的正值。
A size_t
value is always positive even if you pass a negative value to malloc
. The negative value is converted to an unsigned value of type size_t
which leads to a huge positive value.
例如:
char *p = malloc(-2);
相当于:
char *p = malloc(SIZE_MAX - 1); // SIZE_MAX is the maximum
// size_t value
这篇关于当我们调用malloc负放慢参数会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!