Sie sind auf Seite 1von 4

Memory allocation in C

suruchi purwar

Memory Allocations in C

Types of memory allocations in C:


1. Compile-time or Static allocation (using arrays)

2. Run-time or Dynamic memory allocation (using pointers)

Difference Between Static & Dynamic memory allocation are :

 In static memory allocation, memory is allocated to the variables at the start of the
program while in dynamic memory allocation, memory is allocated at the run-
time.

 In static, memory allocated to the variable is fixed while in dynamic, memory


allocated to the variable is dynamic.

 Static memory allocation results in the inefficient use of memory while dynamic
memory allocation makes the efficient use of memory.

 In dynamic memory allocation, memory can be expanded & contract during run-
time as per requirement. This provides the flexibility to the programmer.

Dynamic memory allocation functions:

C provides 4 standard routines of dynamic memory allocation:

1. Malloc( )

2. Calloc( )

1
Memory allocation in C
suruchi purwar

3. Free( )

4. Realloc( )

1. Malloc( ) :

• This function allocates the block of memory in bytes.

• This function is like a request to the RAM of the system to allocate the memory,
if the request is granted, it returns a pointer to the first block of that block of
memory & the type of pointer it returns is void. And if the request is not granted
then it returns a NULL.

• This function is available in the header file <stdlib.h> or <alloc.h>

• Syntax of this function is :

malloc(number of elements*size of each element);

• Example :

int*ptr;

ptr=malloc(10*sizeof(int));

• Since type of ponter it returns is void, so type_casting is required to change the


return pointer type.

• Example of type_casting:

int*ptr;

ptr=(int*)malloc(10*sizeof(int));

2
Memory allocation in C
suruchi purwar

2. Calloc( ) :

• It works similar to the malloc() function except that it requires two argument.

• Syntax for this function :

ptr = calloc(number of elements, size of each element);

• Example :

int*ptr;

ptr = (int*)calloc(10,2);

• This function is available in the header file <stdlib.h> or <alloc.h>

• The memory allocated by the malloc() contains garbage values & the memory
allocated by calloc() contains all zeros.

3. Free( ) :

• This function is used to de-allocate the previously allocated memory using


malloc() or calloc() function.

• Free function is used to return the allocated memory block to the system
memory RAM.

3
Memory allocation in C
suruchi purwar

• Syntax for this function is:

free(ptr);

4. Realloc( ) :

• This function is used to resize the size of memory block, which is already
allocated.

• This function is available in the header file <stdlib.h> or <alloc.h>

• Note that before using the realloc() function, the memory block to be
resized should be allocated using the following statement:

ptr = malloc(size);

• Syntax of this function :

ptr = realloc(ptr, new_size);

Das könnte Ihnen auch gefallen