tags:

views:

118

answers:

3

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