0% found this document useful (0 votes)
98 views

Dynamic Memory Allocation in C

The document discusses dynamic memory allocation in C using functions like malloc(), calloc(), realloc() and free(). It explains that dynamic allocation allows memory to be allocated at runtime unlike static allocation. This allows the size to be increased or decreased as needed while the program is executing. Dynamic allocation is useful for data structures like linked lists where the size is not fixed beforehand.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Dynamic Memory Allocation in C

The document discusses dynamic memory allocation in C using functions like malloc(), calloc(), realloc() and free(). It explains that dynamic allocation allows memory to be allocated at runtime unlike static allocation. This allows the size to be increased or decreased as needed while the program is executing. Dynamic allocation is useful for data structures like linked lists where the size is not fixed beforehand.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

UNIT - V

Dynamic memory allocation in C


The concept of dynamic memory allocation in c language enables the C
programmer to allocate memory at runtime. Dynamic memory allocation in c
language is possible by 4 functions of stdlib.h header file.
1. malloc()
2. calloc()
3. realloc()
4. free()
Static Memory Allocation Dynamic Memory Allocation
Memory is allocated at compile time. Memory is allocated at run time.
Memory can't be increased while Memory can be increased while
executing program. executing program.
Used in array. Used in linked list.

malloc() Allocates single block of requested memory.


calloc() Allocates multiple block of requested memory.
realloc() Reallocates the memory occupied by malloc() or calloc() functions.
free() Frees the dynamically allocated memory.

malloc() function in C
The malloc() function allocates single block of requested memory.
It doesn't initialize memory at execution time, so it has garbage value initially.
It returns NULL if memory is not sufficient.
Syntax
ptr=(cast-type*)malloc(byte-size)  
calloc() function in C
The calloc() function allocates multiple block of requested memory.
It initially initialize all bytes to zero.
It returns NULL if memory is not sufficient.
Syntax
ptr=(cast-type*)calloc(number, byte-size)  
realloc() function in C
If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by
realloc() function. In short, it changes the memory size.
Syntax
ptr=realloc(ptr, new-size)  
free() function in C
The memory occupied by malloc() or calloc() functions must be released by calling
free() function. Otherwise, it will consume memory until program exit.
Syntax
free(ptr)  
Linked List
A linked list is a sequence of data structures, which are connected together via links.
Linked List is a sequence of links which contains items. Each link contains a
connection to another link. Linked list is the second most-used data structure after
array.

Why Linked List?


Arrays can be used to store linear data of similar types, but arrays have the following
limitations.
1) The size of the arrays is fixed: So we must know the upper limit on the number of
elements in advance. Also, generally, the allocated memory is equal to the upper limit
irrespective of the usage.
2) Inserting a new element in an array of elements is expensive because the room has
to be created for the new elements and to create room existing elements have to be
shifted.
For example, in a system, if we maintain a sorted list of IDs in an array id[].
id[] = [1000, 1010, 1050, 2000, 2040].
And if we want to insert a new ID 1005, then to maintain the sorted order, we have to
move all the elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques are used.
For example, to delete 1010 in id[], everything after 1010 has to be moved.
Advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion
Drawbacks:
1) Random access is not allowed. We have to access elements sequentially starting
from the first node. So we cannot do binary search with linked lists efficiently with its
default implementation. Read about it here.
2) Extra memory space for a pointer is required with each element of the list.
3) Not cache friendly. Since array elements are contiguous locations, there is locality
of reference which is not there in case of linked lists.
Preprocessors
The C Preprocessor is not a part of the compiler, but is a separate step in the
compilation process. In simple terms, a C Preprocessor is just a text substitution tool
and it instructs the compiler to do required pre-processing before the actual
compilation. We'll refer to the C Preprocessor as CPP.
All preprocessor commands begin with a hash symbol (#). It must be the first
nonblank character, and for readability, a preprocessor directive should begin in the
first column. The following section lists down all the important preprocessor
directives −
Sr.No. Directive & Description
1 #define Substitutes a preprocessor macro.
2 #include Inserts a particular header from another file.
3 #undef Undefines a preprocessor macro.
4 #ifdef Returns true if this macro is defined.
5 #ifndef Returns true if this macro is not defined.
6 #if Tests if a compile time condition is true.
7 #else The alternative for #if.
8 #elif #else and #if in one statement.
9 #endif Ends preprocessor conditional.
10 #error Prints error message on stderr.
11 #pragma Issues special commands to the compiler, using a
standardized method.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy