Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under what circumstances can malloc return NULL?

It has never happened to me, and I've programming for years now.

Can someone give me an example of a non-trivial program in which malloc will actually not work?

I'm not talking about memory exhaustion: I'm looking for the simple case when you are allocating just one memory block in a bound size given by the user, lets say an integer, causes malloc to fail.

like image 842
RanZilber Avatar asked Feb 01 '12 19:02

RanZilber


People also ask

What does it mean if malloc () returns a NULL?

If the malloc function is unable to allocate the memory buffer, it returns NULL. Any normal program should check the pointers which the malloc function returns and properly handle the situation when the memory allocation failed.

Does malloc return NULL if failed?

malloc() returns a null pointer when it fails to allocate the needed space. This can be due to: Out-of-memory in the machine (not enough bytes)

How do I handle malloc returning NULL?

For some programs, simply aborting is the right thing to do. For some applications, the right thing to do is to shrink caches and try the malloc again. For some multithreaded programs, just waiting (to give other threads a chance to free memory) and retrying will work.

Does malloc set to NULL?

DESCRIPTION top. The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().


2 Answers

You need to do some work in embedded systems, you'll frequently get NULL returned there :-)

It's much harder to run out of memory in modern massive-address-space-and-backing-store systems but still quite possible in applcations where you process large amounts of data, such as GIS or in-memory databases, or in places where your buggy code results in a memory leak.

But it really doesn't matter whether you've never experienced it before - the standard says it can happen so you should cater for it. I haven't been hit by a car in the last few decades either but that doesn't mean I wander across roads without looking first.

And re your edit:

I'm not talking about memory exhaustion, ...

the very definition of memory exhaustion is malloc not giving you the desired space. It's irrelevant whether that's caused by allocating all available memory, or heap fragmentation meaning you cannot get a contiguous block even though the aggregate of all free blocks in the memory arena is higher, or artificially limiting your address space usage such using the standards-compliant function:

void *malloc (size_t sz) { return NULL; } 

The C standard doesn't distinguish between modes of failure, only that it succeeds or fails.

like image 76
paxdiablo Avatar answered Sep 26 '22 00:09

paxdiablo


Yes.

Just try to malloc more memory than your system can provide (either by exhausting your address space, or virtual memory - whichever is smaller).

malloc(SIZE_MAX) 

will probably do it. If not, repeat a few times until you run out.

like image 37
Useless Avatar answered Sep 25 '22 00:09

Useless