avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] problem with malloc() in avr-libc 1.6.1


From: David Brown
Subject: Re: [avr-gcc-list] problem with malloc() in avr-libc 1.6.1
Date: Tue, 29 Jan 2008 10:57:25 +0100
User-agent: Thunderbird 2.0.0.9 (Windows/20071031)

Javier Almansa Sobrino wrote:
Hi everybody. I'm developing a proyect with an atmega128 and I've got
some troubles with dinamic memory allocation.

I have a structure with an uint8_t pointer and tree uint16_t variables
inside. Initially I create a pointer for this structure and initialize
it with a call to malloc. Later I initialize the uint8_t pointer inside
the structure with another call to malloc. In this case, I initialize
with a lenght of 150 bytes.

Later, I made a lot of calls to a funcion (that is not a recursive
function). In this function I call malloc at the init to initialice a
uint8_t buffer. Later, when the function is end, I free this space.


I don't know what the problem is in your program, and whether or not it is a bug in your code or the library - dynamic memory is something most embedded programmers avoid whenever possible, so I haven't used the avrlibc malloc. But when have you considered alloca() instead of malloc() in this situation? It is much simpler, faster, and safer when you want to release the memory at the end of the scope in which it was allocated.

What are your allocation/free patterns like? It sounds like you might be getting memory fragmentation, so that you can no longer allocate contiguous blocks of free memory. But malloc() should return 0, rather than hanging, if that happens.

Other than that, can you post some minimal code that illustrates your problem?

mvh.,

David


My program runs ok for a time, later it crashes. I've debugged it with
avarice and ddd and I've noticed the function before commented runs ok
for 5 or 6 times, later, when I call the malloc function this call to
malloc never returns. Debugging the code, I've seen the program never
end the step two of the malloc function.
At the end of the code before any call to malloc, I initialize
__malloc_heap_start to 0x808000 and __malloc_heap_end to 0x80ffff. Of
course the external memory is activated in the early init as described
in avr-libc user manual and it works fine.

Someone knows what's the reason for this malfunction in my application?
It's some bug on avr-libc or is an error by me?

thanks.







reply via email to

[Prev in Thread] Current Thread [Next in Thread]