Malloc

How do i use the 'malloc' function.

Can someone give me an example..

Thanks in advance...

Parents
  • I agree with Andrew. Allocating memory dynamically in an embedded application is not a great idea. You will eventually end up with too many fragments, and have to put in a routine to clean up the memory pool.

    Unless you are seriously short of memory, you are better off declaring variables. Remember that the compiler will try and use internal registers for any variables declared within functions, and the linker will overlay wherever it can. Try using the OVERLAY directive in the linker and see how much impact it has. We have used the OVERLAY with the '51 toolset and it worked fine. You may have to play with the overlay options some to get it correct, but it's worth the effort I'd say.

    With the cost of RAM these days, try using a bigger chip. With 'C' code, the days of small RAM segments is fast disappearing.

Reply
  • I agree with Andrew. Allocating memory dynamically in an embedded application is not a great idea. You will eventually end up with too many fragments, and have to put in a routine to clean up the memory pool.

    Unless you are seriously short of memory, you are better off declaring variables. Remember that the compiler will try and use internal registers for any variables declared within functions, and the linker will overlay wherever it can. Try using the OVERLAY directive in the linker and see how much impact it has. We have used the OVERLAY with the '51 toolset and it worked fine. You may have to play with the overlay options some to get it correct, but it's worth the effort I'd say.

    With the cost of RAM these days, try using a bigger chip. With 'C' code, the days of small RAM segments is fast disappearing.

Children
More questions in this forum