Now I am assuming that you don't use temp = realloc(temp,
11*sizeof(char));
because if there is not enough memory you get a
NULL ptr returned, thus leaving no way to access the original memory
block.
Correct. This would cause a memory leak, if temp
was your only reference to the memory block.
So would the below be a good way to handle a NULL pointer being returned?
No. That loop might loop forever. It's unlikely in this instance where you are simply trying to expand to 11 bytes. As H2CO3 said in the comments, if this fails you have serious issues.
If your system has run out of memory, it will likely become unstable. If only your process has fragmented the memory or run out of address space, no amount of waiting will help, unless other parts of your program are busy rectifying the problem.
When memory allocation fails, you either need a complex recovery strategy, or a clean exit strategy. Chances are high that trying to be clever about allocation will make your program extremely difficult to follow.
Unless you are writing something that will be part of a space probe or some other mission-critical device (which is unlikely to rely on dynamic memory anyway), your best strategy is to issue a helpful error message and cleanly exit.
Now, I just wrote all this and then saw your clarification in the comments about what you're actually doing. If you are happy that the sleep-loop solves your problems, then go for it, but put some code in there to detect outlandish waits and report them so you don't go crazy when debugging.
A richer solution would be to wrap this stuff in your own memory manager and use semaphore-driven queues or some other mechanism tailored to your program. Using sleeps is a little naive.
Also, I would not recommend allocating small blocks of data. If you do a lot of that, you may fragment your memory. If you need that, then you may need to do memory pooling in each thread and have them request larger blocks.