Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the heap start address in C program?

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?

like image 327
kumar Avatar asked Jan 23 '15 06:01

kumar


People also ask

How heap is allocated for a program?

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.

Is there a heap in C?

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'.

Where is the heap C?

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.

How is heap memory allocated?

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.


1 Answers

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.

like image 185
John Zwinck Avatar answered Sep 30 '22 06:09

John Zwinck