Question

I want to clear some virtual memory because I need to load segments from an elf file.

(I'm writing a program that loads and executes an ELF file).

As I understand I have to use munmap

unsigned int blok = sectionHeads[i].adr - (sectionHeads[i].adr % getpagesize());

if (munmap((void *)blok,getpagesize()) == -1) 
{ 
  printf("fail\n");
} 
else 
{ 
  printf("succ\n");
}

blok is a multiple of the address from which I want to free the memory. (or so I think, English is not my mother tongue so it's very possible I misunderstood the meaning of "multiple of").

I want to free the memory because later on I need that space.

When I run the program it doesn't go beyond munmap();

What am I doing wrong?

The only constraint that I found in the munmap documentation is that the address parameter has to be a multiple of the system pagesize.

Is there an easier way to free this memory?

Note: The project requirement is that I free the memory if it is needed but it doesn't say how.

Was it helpful?

Solution

You might be unmapping the memory that your code is running in. You can check this in a debugger by puttimg a breakpoint at the if statement and comparing the value of blok to the current program counter.

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