malloc

harimagic's Avatar, Join Date: Oct 2007
Go4Expert Member
Hi,

I have one doubt may be so many guys answer this.
We know that malloc use to allocate dynamic memory. But while writing programs we use to check with NULL.

Char *p;
p=(char *)malloc(100);
If(p==NULL)printf(“mem alloction failed”);// <-----(When dynamic allocation fail)

1. When will memory allocation fail?
2. I have a scenario that it should not be fail at any cost for that what I should do?
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
> 1. When will memory allocation fail?
Typically when you run out of memory, or the pool has become too fragmented that no large blocks remain.

> 2. I have a scenario that it should not be fail at any cost for that what I should do?
So you're writing a safety critical system in C?