Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

assert ( malloc...

Name: Anonymous 2010-06-28 13:36

A question for you, Anon.

If you watch at malloc() manpage you can notice that the function may return NULL in some cases (i.e. errors or 0-sized allocation). How do you face this issue?

I noticed that many programmers check the return value and manage error routines for it, while other just assert() the return value not to be NULL.

Also in C++ by the way you use the new operator without taking care about the return value (I guess it's asserted to be not NULL under the hood, is it?)

What is your opinion?

Name: Anonymous 2010-06-29 16:46

On a desktop/home computers you practically never get NULL from malloc, unless your program has a severe memory leak, since the operating system will swap unused memory to disk. Also, memory fragmentation might cause malloc() to fail if you ask for a block too big even though there's theoretically free memory left.

When the memory has gotten fragmented, you'll notice that performance drops since malloc() has to do a lot of work per allocation.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List