Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what happens when we call Malloc with negative parameter?

Tags:

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.

Prototype: void *malloc(size_t size);

I tried passing a negative value as a parameter: malloc(negative) returns 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?

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?

Is it implementation defined?

Read this link:malloc(0)

like image 480
Dineshkumar Avatar asked Jul 29 '13 13:07

Dineshkumar


People also ask

What happens if you call malloc twice?

When you call malloc a second time, it has no way of knowing you are doing anything with newPtr . It merely allocates new space and returns a pointer to it. Then that new pointer is assigned to newPtr , which erases the old value that was in newPtr . At that point, you have no way of knowing what the old value was.

What does malloc function return on failure?

malloc() returns NULL on failure. Even though OP exempted it with "to allocate non-zero memory block", malloc(0) can also return NULL .

When can malloc fail?

So the first case of malloc() failing is when a memory request can not be satisfied because (1) there is not a usable block of memory on the list or heap of the C runtime and (2) when the C runtime memory management requested more memory from the operating system, the request was refused.

What is the parameter for malloc?

The malloc() function takes a single parameter, which is the size of the requested memory area in bytes. It returns a pointer to the allocated memory. If the allocation fails, it returns NULL.


2 Answers

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.

Example:

char *p = malloc(-2);

is equivalent to:

char *p = malloc(SIZE_MAX - 1);  // SIZE_MAX is the maximum
                                 // size_t value 
like image 137
ouah Avatar answered Oct 20 '22 16:10

ouah


Since the argument to malloc is of type size_t which is unsigned but you are passing an integer which is signed, the integer value will be converted to size_t the rules for this are covered in the draft C99 standard section 6.3.1.3 Signed and unsigned integers which comes under Conversions and it says:

Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.49)

Let's look at an example of what this means, if you pass in -1 then max size_t value + 1 will be added:

-1 + MAX_SIZE_T + 1

which results in:

 MAX_SIZE_T

For -5 you would end up with:

MAX_SIZE_T - 4

This means for small negative values the resulting size_t value will be a very large positive number.

So why do you receive NULL back for malloc in these cases? If we go back to the the draft standard section 7.20.3 Memory management functions it says:

If the space cannot be allocated, a null pointer is returned

You are making a request that is too large and the space can not be allocated.

like image 43
Shafik Yaghmour Avatar answered Oct 20 '22 14:10

Shafik Yaghmour