How can we find highest and lowest address of heap
Can anyone suggest, how can we find highest and lowest addres开发者_如何转开发s of heap using C?
On a Linux system, you can use sbrk() with a 0 argument to find one end. You might be able to find the other end by understanding your program loader's segment ordering and examining etext and edata - see the end(3) manpage.
All of this is non-standard and outside the scope of C itself.
You could wrap your calls to malloc
to keep track of the lowest and highest address seen so far at each call:
extern unsigned char *lowest, *highest;
unsigned char *tmp = malloc(size);
if (!tmp) return 0;
if (!lowest || tmp < lowest) lowest = tmp;
if (tmp+size > highest) highest = tmp;
return tmp;
The answer is that you can't in C. If you check the language standard, you'll notice that the concept is never mentioned.
The heap is an implementation detail used in some operating environments (OK almost all of them).
精彩评论