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 ?
 

alexxx

Advanced Member level 4
Joined
Apr 17, 2011
Messages
1,013
Helped
273
Reputation
552
Reaction score
270
Trophy points
1,383
Location
Greece
Activity points
7,938

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,510
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++.
 

vanangamudi

Banned
Joined
Jun 25, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
0
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

Top