Is it wrong to use the malloc loop to guarantee the result from malloc?

Is it wrong to allocate memory as follows:

FOO *foo;
while (!(foo = malloc(sizeof(FOO)))) ;
+2
source share
2 answers

I do not know about bad practice, but this is unusual. malloc()crashes usually indicate serious system problems that your program is unlikely to recover from. If your system is different, your example can be very practical.

NB - this answer assumes that it sizeof(FOO)is "reasonable" and that yours are malloc()not just refusing because you are requesting too much memory.

+4
source

"" malloc(). malloc NULL, , (, ). .

, Linux:

Linux . , malloc() -NULL, , .

, , malloc - -NULL, - Linux -NULL , -, .

, .

+1

All Articles