MALLOC use

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1)I'm just experimenting w/ malloc using winavr 20090303 and PN. I'm using the syntax i found on other posts, it compiles but it goes into err handler. Missing something ?

2) Is there ever a justification for using malloc on 8 bitters for app. code ?

3) If it's to be avoided, what do you do instead ( Size an array for biggest data group and overwrite throughout app. ) ?

thanks in advance

#include 
.
.
.

uint8_t *buffer;

// allocate space for a temp sector buffer

    buffer = ( uint8_t *)malloc( 512);
    if ( buffer == NULL)        // report an error  
    {   set_led;
        FError = FE_MALLOC_FAILED;
        free( D);
        return NULL;
    }

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

Last Edited: Wed. Apr 7, 2010 - 05:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which model of AVR is this? Does it really have 512 bytes of SRAM available after .data and .bss have been allocated? (malloc would appear to think not as it'll return a NULL pointer if there is not sufficient RAM to service the requested amount).

2) No

3) Yes

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes. This is what you normally do. Share a global storage area. Or allocate a private storage on the stack. When your function completes, the stack is restored.

Most embedded apps have a pretty good idea what they will need. If you really do want dynamic memory, by all means use malloc().

Remember that memory is normally uninitialised, so never assume that virgin memory contains any particular value.

A global array will start off with zeros. Memory allocated with calloc() will contain zeros. Stack or malloc() memory may contain anything.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Using malloc in a deeply embedded system makes me feel icky.

A good alternative is a memory pool: http://en.wikipedia.org/wiki/Mem...

I use it for CAN Messages, since I doon't exactly know in advance how many I need in advance and I often pass them around the system like they are dynamic memory (they get "created" when received and "released" when consumed).

-- Damien

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I see no problem in using malloc in an embedded system, if you know can overview the potential issues and IF YOU KNOW YOUR MEMORY NEEDS!
But, if you know those needs, you could just as well follow Damiens suggestion, and use memory pools.
You can add your own "malloc" and "free" on top, and it will work the same, except you have removed all fragmentation issues. There will be a small memory overhead because of the fixed size blocks, but having a number of various size pools, this is normally not a problem.

/Jesper
http://www.yampp.com
The quick black AVR jumped over the lazy PIC.
What boots up, must come down.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Clawson, it's an xmega16A4, I adjusted down other ram i was using and now malloc works, gracias !

Quote:
Or allocate a private storage on the stack.
I think i should know how to do this, but i don't. If i define a local array in a func() other than main.c it gets permanently alloc, right ? I RTFM and didn't see how to do it. David would you give me an example?

Memory pools are on my todo list.

thanks all !

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
If i define a local array in a func() other than main.c it gets permanently alloc, right ?

No, local variables in functions exist only when the function is called (unless they are declared static).

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I think i should know how to do this, but i don't

int main(void) {
 uint8_t private[200];

puts a fairly permanent 200 bytes on the stack for private usage. It'll remain there until main() exits but if that happens the loss of private data is the least of your worries.

As Steve says, this only exists while main() is in context but in an embedded program main() is unusual among all the functions that it never goes out of context.