Question

I am writing unit tests for a library I am developing. The library implementation cannot be mocked. The unit test is about returning a special value when the program runs out of memory.

I came up with this naive solution:

// Fillup memory
int *p = NULL
do {
    p = malloc(sizeof(int));
    // store p somewhere to release it later
} while(p);
// Got out of memory :)

Is there anyway to fill up the memory faster than this solution without increasing the memory block size allocated with malloc ?

PS: Using a third party memory allocator is forbidden :)

Was it helpful?

Solution

Obviously - but depending on the C++ implementation - you could do that in multiple threads. Besides that - no.

OTHER TIPS

Try allocating bigger memory chunks rather than sizeof(int). e.g. 1KB and when allocation fails keep on reducing the size argument in the loop. Do this till allocation fails for sufficiently small size - May be 1 B.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top