Can anyone suggest, how can we find highest and lowest address of heap using C?
+1
A:
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.
Robie Basak
2010-10-17 21:42:57
A:
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).
JeremyP
2010-10-17 22:04:00
A:
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;
R..
2010-10-18 02:49:02