views:

206

answers:

4

Or do you just have to do it and check errno and/or the pointer to see if you succeeded?

+1  A: 

There is no way to do that in C. Any solution would be OS-specific, and I don't know if it would be useful enough to implement.

David Thornley
What is the best practice in C to monitor your memory use? Is it as Paul suggested - malloc away until you fail?
arrocharGeek
That's pretty much what you have to do. Most programs will assume they've got enough memory, and fail when they can't get it; even if you can free up some memory, a process that maxes out is likely to keep mallocing any reasonable amount of memory.
David Thornley
You may want to keep a reserve for a soft failure. You can do that by mallocing a chunk of memory at the beginning, to release when you run out, so that you'll have some heap space to use while shutting down.
David Thornley
While I agree with the answer, I'm not sure about the rationale. The implementation of malloc is OS-specific, so having an available function, if one existed, OS-specific in its implementation doesn't seem like a problem.
Uri
David Thornley
+13  A: 

Are you asking if you can find out before you call malloc how many bytes are available for you to use?

If so, the answer is no. Even if you could, the answer may not be very valuable to you. The reason why is you can never know how much memory is available, you can only know how much memory was available. This is a very important distinction because it has an affect on the way you can properly utilize an API.

For instance, lets assume there was such a function called available_memory. This code looks correct but is horribly broken

size_t size = GetSizeToAllocate();
if ( size < available_memory() ) {
  // No need to check the answer because there is enough memory
  SomeType* p = malloc(size);
}

The reason why is that some other thread could come in between the if statement and the malloc statement and do it's own allocation. That could reduce the overal memory in the system and hence change available_memory's return.

Even worse is the available amount of memory in the system does not determine how much memory can be allocated with malloc. What's important for malloc (and any other memory alloc operation) is how much contiguous memory is available.

JaredPar
+6  A: 

Trying won't necessarily help because some OSes (like Linux) use an optimistic memory allocation strategy - even if you get a non-NULL result from malloc() it doesn't mean you have the requested chuck of memory at your service.

More here

Seems there is no way to do it.

qrdl
Last time I saw that discussed, I believe the experts concluded that that wasn't standard-conforming; according to the standards, either you have usable memory or a null pointer. I suppose you could then zero out the memory, but that could be a real performance hit.
David Thornley
+1  A: 

If you know at the start of a program how much memory you will need and the program must not fail while running it's common to just alloc all the memory you need at the start as one big block and then manage it yourself.

Martin Beckett