问题
Is there a way to set the heap start address in GCC compiled C program in linux? In x86_64 system,my test program sets the heap address to 4 byte referenced address ( less than FFFFFFFF). I want to set this to 8 byte referenced address for some testing ( > FFFFFFFF). Does GCC provide any way to set the heap start address?
回答1:
You can do this a bit indirectly using sbrk()
:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
sbrk(0xFFFFFFFF);
printf("%p\n", malloc(1));
return 0;
}
This works by "allocating" 0xFFFFFFFF bytes at the very start, so that the next thing malloc()
can allocate is a higher address.
来源:https://stackoverflow.com/questions/28103943/set-the-heap-start-address-in-c-program