在C程序中设置堆的开始地址? [英] Set the heap start address in C program?
问题描述
有没有办法在linux中设置GCC编译C程序中的堆起始地址?在x86_64系统中,我的测试程序将堆地址设置为4字节的引用地址(小于FFFFFFFF)。我想将它设置为8字节的引用地址进行一些测试(> FFFFFFFF)。 GCC是否提供了设置堆起始地址的方法?
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?
推荐答案
您可以使用 sbrk()
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;
}
这个工作原理是在开始时分配0xFFFFFFFF字节,这样接下来的事情 malloc()
可以分配的是更高的地址。
This works by "allocating" 0xFFFFFFFF bytes at the very start, so that the next thing malloc()
can allocate is a higher address.
这篇关于在C程序中设置堆的开始地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!