Hi,

I am getting the following error :

test.o(11488) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
test.o(11488) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
test.o(11488) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
test.o(11488) malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
terminate called recursively
Abort trap

I am using finite recursion to build the nodes of binary tree. So i believe the C++ program should take care of the storage of nodes. I also think that it will store the nodes of the binary tree in a heap.

Please help

Thanks

Dani AI

Generated

The failure message shows repeated mmap attempts for a 2,097,152‑byte block that return error 12 (ENOMEM) — the allocator could not obtain that memory. Common causes are: the program is exhausting available heap (leaks or many allocations), a recursion bug causing unbounded allocation, per‑process resource limits or 32‑bit address‑space fragmentation that prevents a contiguous 2MB block. was right that nominal heap size isn’t the whole story; fragmentation or runaway allocation can still make modest requests fail. was also correct to ask for the code — you need to locate the allocation site.

Concrete, practical steps to track it down

  • Run under a debugger and break where malloc fails. On macOS/Linux with lldb/gdb:

    lldb ./test
    (lldb) breakpoint set -n malloc_error_break
    (lldb) run
    (lldb) bt            # inspect the call stack when it breaks

    The backtrace will show which function requested that 2MB chunk.

  • Use runtime leak/sanitizers. Build with AddressSanitizer (-fsanitize=address -g) or run under Valgrind (Linux) to detect leaks and invalid frees. On macOS use the leaks tool or Instruments with MallocStackLogging enabled to see allocation sites.

  • Instrument the code to find runaway recursion/allocations: add a simple depth counter and a global allocation counter (increment on each new/malloc, decrement on free) and log when numbers grow unexpectedly. Example to detect runaway recursion:

    static int depth = 0;
    int f1(int out_g) {
        ++depth;
        if (depth > 100000) { fprintf(stderr, "recursion depth=%d\n", depth); abort(); }
        ...
        --depth;
    }

Logic and quick fixes to check

  • Inspect any function called inside recursion (your solve/backtrace) for heap allocations that aren’t freed each branch.
  • Fix obvious control-flow bugs (there’s an unreachable fprintf after a return in the posted function — remove or move it).
  • Check for accidental use of bitwise & instead of logical &&, or integer overflow when computing allocation sizes.
  • Consider switching to an arena/pool or reserving a node vector to avoid many small heap allocations during deep backtracking.

Finally, isolate a minimal reproducer: once you can break at the failing allocation, the stack trace will point to the culprit allocation path and make the fix straightforward.

Recommended Answers

All 3 Replies

Umm, your code?

Umm, your code?

Exactly my thoughts.

From what I understand, malloc can fail if you've run out of heap-space your program is using for dynamic allocation or if you are attempting to allocate memory to a location that is in use by another process. size=2097152 Your heap should be (by default) 512 MB, which is (depending on how "Mega" and "Kilo" are interpreted) 1024 * 512 Kilobytes and 1024 * 1024 * 512 bytes.

So you should have enough space (by default, not saying this isn't expandable), but the question is what object (or hopefully struct) are you trying to allocate memory for using a C-style mem allocator? I believe some structures have a limit on the amount of memory they can hold, but a professional would have to confirm that.

Hi,

I am pasting part of the code ... recursion part. It is big code so i am not sure whether teh code will help or not. 


int circuit::f1(int out_g)
{
	/* Initially the output of the gate is don't care. This is 
	only true for the first time when the function is called */
	
	int outg = out_g;
	
	/* If output is excited to '1' return true */
	if(output[outg-1][1] == 1)
	{
		status1 = true;
		return status1;
	}
	
	/* If Conflict occurs return false */ 
	if((output[outg-1][1] != 1) && (output[outg-1][1] != 2)) 
	{
		number_backtrack++;
		status1 = false;
		return status1;
	}
		
	int kgg = 0;
	number_backtrace++;
	
	kgg = backtrace(outgate,1);
	
	solve(input_column[kgg][0],input_column[kgg][1]);
	if(f1(outg) == true)
	{
	    status1 = true;
	    return status1;
	}
	
	if(input_column[kgg][1] == 1)
		input_column[kgg][1] = 0;
	else if(input_column[kgg][1] == 0)
		input_column[kgg][1] = 1;
	solve(input_column[kgg][0],input_column[kgg][1]);
	
	if(f1(outg) == true)
	{
		status1 = true;
		return status1;
	}
	
	input_column[kgg][1] = 2;
	solve(input_column[kgg][0],input_column[kgg][1]);
	
	
	status1 = false;
	return status1;
	
	fprintf(stderr,"\n I am here A");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.