Pages: [1]   Go Down
Author Topic: Is stack space fixed size or dynamic  (Read 471 times)
0 Members and 1 Guest are viewing this topic.
Norfolk UK
Offline Offline
Faraday Member
**
Karma: 67
Posts: 2514
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use this library to determine the amount of free RAM my project has as I want to define a circular buffer (at compile time) as large as possible.
Question is, does the stack have a fixed & reserved size not included in the free memory calculation or is it's size dynamic, extending into the free memory pool as I call functions etc.
Also does the Serial library allocate all it memory in the begin class or can/will it allocate more memory as needed later.
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8501
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
or is it's size dynamic, extending into the free memory pool as I call functions etc.
Yes, the stack varies with every function call and allocation of local (automatic) variables in those functions.

Don't know exactly about the Serial lib but I would say it either uses static arrays or mallocs() for them in the .begin() funct. AFAIK the 64-byte buffer is static.

______
Rob
« Last Edit: March 28, 2013, 04:33:12 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

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

The stack and dynamically allocated memory use the same space. One from the top, one from the bottom.

Quote
I want to define a circular buffer (at compile time) as large as possible

Why?
Logged


Norfolk UK
Offline Offline
Faraday Member
**
Karma: 67
Posts: 2514
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The stack and dynamically allocated memory use the same space. One from the top, one from the bottom.

Quote
I want to define a circular buffer (at compile time) as large as possible

Why?
For this. I know there is the arduino SUMP project but I'm no fan of Java (security implications) though it's used in Arduino IDE & Processing but not the full console SUMP needs. Also this would be event driven (pin change) instead of time driven so could maybe capture finer detail for longer than time driven.
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8501
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So by the time you get to the end of setup() all memory allocation should have been done, you determine how much RAM is left, subtract maybe 100 bytes to allow for the stack to grow, then malloc() the rest. That's your buffer.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

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

The project (an Arduino logic analyzer) sounds exciting. I agree with Graynomad that you could basically malloc as much memory as you can, less a hundred or so bytes for the stack (used when you call functions).

However 2000 or so bytes won't capture many data points, especially if you are recording changes and need to save the time along with each change (say, 4 bytes each change).

You can get external SPI-based RAM chips, that could give you a fair bit more.

Or another approach might be a CPLD (logic array) which might be able to simultaneously capture changes and write them to external RAM.
Logged


Norfolk UK
Offline Offline
Faraday Member
**
Karma: 67
Posts: 2514
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The project (an Arduino logic analyzer) sounds exciting. I agree with Graynomad that you could basically malloc as much memory as you can, less a hundred or so bytes for the stack (used when you call functions).

However 2000 or so bytes won't capture many data points, especially if you are recording changes and need to save the time along with each change (say, 4 bytes each change).

You can get external SPI-based RAM chips, that could give you a fair bit more.

Or another approach might be a CPLD (logic array) which might be able to simultaneously capture changes and write them to external RAM.

I already have a hardware logic analyser but wanted to do a simple 1 (or maybe 2) channel software only version so peoples on here wanting help decoding signals could maybe be instructed to download and run the analyser sketch and then post the results.
As most stuff to decode comes in small packets I'm hoping an event driven approach of only logging data when something changes instead of every x micro/milli seconds will allow me to capture and serial send at least 425+ changes without buffer overflow.
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 132
Posts: 6746
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The stack starts at the top of memory and will grow as-needed without anything to stop it.  If it overruns the heap (which starts at the bottom of memory, right after static data, and grows upward), bad things will happen, but there won't be any indication that this has happened.
Logged

Pages: [1]   Go Up
Jump to: