Pages: [1]   Go Down
Author Topic: malloc() and free() how it works!?  (Read 9484 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys, I am trying to understand how malloc and free works in arduino, there is no OS (as long as I know) running to manage the memory, anyone can help me to understand that? Links and books would be appreciated =D. :exclamation  ;D
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This http://en.wikipedia.org/wiki/Malloc is a good starting point.
One thing, be sure you really understand this before using it in anger.
Try various test programmes etc.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah, sorry, I am used to dynamic memory allocation, but never used it in microcontrollers (In fact I only used the 8051, in college).

I understand the heap allocation when used with an OS, my question was: "How can we use it in Arduino since no entity is controlling the memory (as long as I know)?"

I found this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1282050278/5 it appears to have some info about what I am seeking,

Thanks,

Flavio
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not sure I follow all of that.
malloc and free are part of avr-libc. So, just use them!
Logged

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

Quote
How can we use [malloc/free] in Arduino since no entity is controlling the memory
There are not other entities trying to use the memory, either.  So when the program starts, it knows that it has full control all the memory in the chip.  Some may already be allocated to data or global variables, or the stack, but the rest can be assumed to be a chunk that malloc() can carve into smaller pieces are requested.
Logged

UK
Offline Offline
God Member
*****
Karma: 13
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

But, I would really look at your design carefully first.

I just think dynamic memory allocation is fine in Big software - essential in the Objective C for an iPhone development I do in the day job. But rarely needed in embedded systems.

I used to work for a defence contractor, for embedded systems to do with displays, and there was simply a blanket ban on dynamic memory allocation.
« Last Edit: September 23, 2010, 01:10:20 pm by simon.monk » Logged

--
My New Arduino Book: http://www.arduinobook.com

The Wired 2.0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
Destroy the core!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.nongnu.org/avr-libc/user-manual/malloc.html

Be very very careful when using this stuff, you can drain your memory really quick...
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well I used that in one modification in the WString, i made a function toCharArray, that follows the specification in the docs... It allocate and return a char[] to the user

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1283943849
Logged

Pages: [1]   Go Up
Jump to: