C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Dynamic Memory Allocation - Getting Started

C Programming

In this tutorial we will learn about dynamic memory allocation in C programming language.

We learned about arrays in the Array tutorial. So, we know that to create an array we have to specify the number of elements in the array before the code is compiled.

int arr[10];

If we don't specify the size then we will get an error during code compilation.

Following code will throw an error during compilation.

int arr[];	// this will throw an error

Also, we will be wasting memory if the size of the array is too big compared to the number of data saved.

And we will also face problem if the size of the array is less than the number of data to be saved.

This is were dynamic memory allocation comes into picture as it allows us to allocate memory at run time.

Functions used to dynamically allocate memory

Following are the functions we use when we want to allocate memory dynamically.

FunctionDescription
mallocThis function allocates required number of bytes and returns a pointer to the first byte of the allocated memory location.
callocThis function allocates an array of elements and initializes them to zero and returns a pointer to the allocated memory location.
reallocThis function will modify the size of the previously allocated memory location.
freeThis function will free the previously allocated memory location.

Memory area

Following image represents the memory area.

All the program instructions, global and static variables are stored in the memory region called the permanent storage area.

Local variables are stored in the memory region called the stack.

The free memory area between the permanent storage area and stack is called the heap. This is the area available for the dynamic allocation during the execution of the program.

The size of the heap keeps changing during the program execution due to the creation and removable of variables that are local to functions and blocks.

So, a common problem we can encounter is overflow of memory during dynamic memory allocation.

The above mentioned memory allocation functions returns NULL when there is no sufficient memory space for allocation.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT