Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

where to use malloc and calloc in controller programming

Status
Not open for further replies.

embpic

Advanced Member level 3
Joined
May 29, 2013
Messages
742
Helped
80
Reputation
160
Reaction score
77
Trophy points
1,308
Location
india
Activity points
5,213
Hello master's
what is main use of malloc() and calloc() and what does it do in embedded system programming that is controller programming?
where i can use it?
 

Re: where to use malloc() and calloc in controller programming

1. calloc allocates a block of memory for an array of elements of a certain size. By default the block is initialized to 0. The total number of memory allocated will be (number of elements * size).
2. malloc allocates memory blocks and returns a void pointer to the allocated space, or NULL if there is insufficient memory available.
calloc allocates an array in memory with elements initialized to 0 and returns a pointer to the allocated space.
malloc takes in only a single argument which is the memory required in bytes. malloc allocated bytes of memory and not blocks of memory like calloc.
IT DOES same kind of work in micro controllers also
Problem :- Using dynamic memory on PICs(MCU) with their very small RAM space is fraught with peril. You can rapidly run out of contiguous space due to the lack of more advanced virtual memory managers that full blown OS's give you, leading to failed allocations and crashes. Worse, it may not be deterministic, and will likely be a pain to debug.
To use this :- **broken link removed**
 

Re: where to use malloc() and calloc in controller programming

malloc() and calloc() are the function which can be ued to allocate memory during run-time. Mainly both the functions do the same job i.e. reserving the requested memory bytes and returning a pointer to that reserved memory with calloc doing the extra job of clearing all the bytes that it has reserved. But usually we don't depend on that and should clear the memory by ourselves if it is required.

In embedded systems normally the requirement of memory will change at run time. So using a static allocation will create a problem. Also by using dynamic memory allocation we reduce the amount of memory wastage and hence efficiently utilize the memory that is available. Again there is an option of freeing the memory after its use is over, which is not possible when static allocation is used.
 
Re: where to use malloc() and calloc in controller programming

Dynamic memory allocation can only improve memory utilization, if different tasks are started and completed sequential in time. If the tasks need to keep their allocated memory inbetween, no memory will be ever freed and the allocation is effectively static.

So the answer depends very much on the nature of embedded application and general answers are inappropriate.
 

Re: where to use malloc() and calloc in controller programming

means by using this function we can allocate space in ROM in run time.
and by using malloc function we are giving single variable to store and by using calloc we are storing no of bytes that block that is array.
is it right?
 

Re: where to use malloc() and calloc in controller programming

Hi its Not in ROM Its RAM and particularly in heap memory area.......
 

Re: where to use malloc() and calloc in controller programming

ok then is it like
we can allocate block of data or byte on perticular address using calloc and malloc respectively.?
 

Re: where to use malloc() and calloc in controller programming

Hi Its not particular location, at a location where it is available or may be its unavailable......
 

Re: where to use malloc() and calloc in controller programming

what does it means
Code:
malloc is used to allocate memory from the heap.
so what is heap ?
and what it returns if location is success?
 

Re: where to use malloc() and calloc in controller programming

It is a sector in a RAM of a system where the dynamic memory allocated, the malloc function returns a valid pointer if the allocation is successful else it returns a NULL pointer so by checking it null or not we could take the decision about the process...
 

Re: where to use malloc() and calloc in controller programming

Code:
the malloc function returns a valid pointer if the allocation is successfu

on success malloc function will return pointer means it will return address where data is allocated. is it right?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top