Set the heap start address in C program?

微笑、不失礼 提交于 2020-06-12 04:44:28

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!