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.

c programming memmory allocation

Status
Not open for further replies.

wild roze

Member level 2
Joined
Jul 8, 2011
Messages
42
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,573
hi,
im learning the c programming language .
i didn't understand the portion memmory allocation (malloc ,calloc ).
please tell me how do we allocate the memmory using the malloc and calloc ?
 

It should also be mentioned when allocating memory with malloc(), calloc() or realloc(), it should also be deallocated when no longer utilized, using free(). These memory allocations are persistent until explicitly freed by free() or program termination.

Failing to deallocate memory can result in what is referred to as a "memory leak." Garbage collection is not automatic in either C or C++.
 

this functions allocates the required memory space in the heap specified by the argument

e.g: int *array = (int*)malloc(sizeof(int)*10);
the argument to malloc() is the amount of memory. it is advisable to specify using sizeof() operator so that the code is ported to other architectures
array is pointer to series of memory locations, with a step increment of size of integer data type.
the data type returned by malloc() is void* so we've to parse it according to our requirement.

dont forget to deallocate the memoty allocated by malloc() or calloc() using free(); else u'll end up in memory leak.
 
Last edited by a moderator:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top