Programatically accessing stack and heap pointers?

With a set of libraries I've been working on I ran into some issues which appeared to be caused by memory corruption, and upon further investigation (mainly various forums and http://www.nongnu.org/avr-libc/user-manual/malloc.html) and printing out variable addresses I suspect this was caused by the heap and stack colliding.

I'm looking to go through my libraries to minimize the memory usage there, however I'm not finding any good pointers to tools for analyzing the memory utilization (I see some posts regarding avr-size utility on Linux but I need something for MacOS). The simplest way I can think of would be to directly point out the stack and heap pointers at various points and perhaps add some #def'd instrumentation to track utilization. However I'm not finding a means of accessing the stack or heap pointers, are these accessible from a arduino program?

This may be what you're looking for.

int freeRam () {
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}

http://playground.arduino.cc/Code/AvailableMemory

random_vamp:
(I see some posts regarding avr-size utility on Linux but I need something for MacOS).

I use a Mac and have avr-size:

/usr/local/CrossPack-AVR/bin/avr-size

Not quite sure where I got it though.

However since all the other things (like avr-gcc) work on a Mac, it isn’t that surprising.

Not that I think it will help in this case.

joshuabardwell: int freeRam () {   extern int __heap_start, *__brkval;   int v;   return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); }

Yeah, this is probably exactly what I was looking for. Thanks!