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?
Heap Memory is used for Dynamic Memory Allocation of Java objects and JRE classes that are created during the execution of a Java program. Heap memory is allocated to objects at runtime and these objects have global access which implies they can be accessed from anywhere in the application.
In C, there is no such place. The place from which malloc takes its memory is unspecified, and as it turns out we all call it 'the heap'.
The heap is in the back of memory, and it's managed by the operating system. Using this is a bit more involved. You need to use the malloc function to tell the operating system how much memory you need.
Heap Allocation: The memory is allocated during the execution of instructions written by programmers. Note that the name heap has nothing to do with the heap data structure. It is called heap because it is a pile of memory space available to programmers to allocate and de-allocate.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With