Pages: [1]   Go Down
Author Topic: Programatically accessing stack and heap pointers?  (Read 171 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 522
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This may be what you're looking for.

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

USA
Offline Offline
Jr. Member
**
Karma: 3
Posts: 88
If you can't fix it with a hammer, it must be an electrical problem.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I use a Mac and have avr-size:

Code:
/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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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!
Logged

Pages: [1]   Go Up
Jump to: