Malloc int single pointer
In this section, you are going to learn
How to allocate memory using malloc ?
How to allocate memory using calloc ?
How to allocate memory using realloc ?
Topics in this section,
Allocate 10 Integers for integer pointer inside structure
Allocate Integers of memory inside function and return the pointer for use in caller
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of malloc() is present in stdlib.h
Step 2 : Define a pointer
int *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call malloc() to allocate 1 Integer of memory
ptr = malloc(4);
// This is not portable
// Because it assumes the size of integer as 4 Bytes
OR
ptr = malloc( sizeof(int) );
// This is portable
// Creates one Integer in Heap
This allocates 1 Integer of memory in Heap
ptr can be used to access this 1 Integer of memory in heap
In our case sizeof(int) is equal to 4 Bytes
We have created one Integer(4 Bytes) in heap !
Step 4 : Use *ptr or ptr[0] to write to memory
*ptr = 65;
OR
ptr[0] = 65;
This statement uses 4 Bytes for writing !
*ptr and ptr[0] can be used interchangeably
*ptr, ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 5 : Use *ptr or ptr[0] to read from memory
printf("*ptr = %d\n", *ptr);
OR
printf("ptr[0] = %d\n", ptr[0]);
This statement uses 4 Bytes for Reading !
*ptr and ptr[0] can be used interchangeably
*ptr, ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 6 : Always free the heap memory after usage
free(ptr);
See full program below
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void)
5{
6 int *ptr;
7
8 // Creates one Integer in Heap
9 ptr = malloc( sizeof(int) );
10
11 *ptr = 65;
12
13 printf("*ptr = %d\n", *ptr);
14
15 free(ptr);
16
17 return 0;
18}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of calloc() is present in stdlib.h
Step 2 : Define a pointer
int *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call calloc() to allocate 1 Integer of memory
ptr = calloc(1, 4);
OR
ptr = calloc(4, 1);
OR
ptr = calloc( 1, sizeof(int) );
OR
ptr = calloc( sizeof(int), 1 );
This allocates 1 Integer(4 Bytes) of memory in Heap
ptr can be used to access this 1 Integer(4 Bytes) of memory in heap
We have created one Integer(4 Bytes) in heap !
Step 4 : Use *ptr or ptr[0] to write to memory
*ptr = 65;
OR
ptr[0] = 65;
This statement uses 4 Bytes for Writing !
*ptr and ptr[0] can be used interchangeably
*ptr, ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 5 : Use *ptr or ptr[0] to read from memory
printf("*ptr = %d\n", *ptr);
OR
printf("ptr[0] = %d\n", ptr[0]);
This statement uses 4 Bytes for Reading !
*ptr and ptr[0] can be used interchangeably
*ptr, ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 6 : Always free the heap memory after usage
free(ptr);
See full program below
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void)
5{
6 int *ptr;
7
8 ptr = calloc(1, sizeof(int) );
9
10 *ptr = 65;
11
12 printf("*ptr = %d\n", *ptr);
13
14 free(ptr);
15
16 return 0;
17}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of realloc() is present in stdlib.h
Step 2 : Define a pointer
int *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call realloc() to allocate 1 Integer of memory
ptr = realloc(NULL, 4 );
OR
ptr = realloc(NULL, sizeof(int) );
This allocates 1 Integer of memory in Heap
ptr can be used to access this 1 Integer of memory in heap
We have created one Integer in heap !
Step 4 : Use *ptr or ptr[0] to write to memory
*ptr = 65;
OR
ptr[0] = 65;
*ptr and ptr[0] can be used interchangeably
*ptr, ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 5 : Use *ptr or ptr[0] to read from memory
printf("*ptr = %d\n", *ptr);
OR
printf("ptr[0] = %d\n", ptr[0]);
*ptr and ptr[0] can be used interchangeably
*ptr, ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 6 : Always free the heap memory after usage
free(ptr);
See full program below
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void)
5{
6 int *ptr;
7
8 ptr = realloc(NULL, sizeof(int) );
9
10 *ptr = 65;
11
12 printf("*ptr = %d\n", *ptr);
13
14 free(ptr);
15
16 return 0;
17}
malloc, calloc, realloc are memory allocation functions
free is memory deallocation function
stdlib.h contains prototypes of malloc, calloc, realloc, free
malloc(4)
is same asmalloc(sizeof(int))
malloc(4)
is same ascalloc(1, sizeof(int))
malloc(4)
is same asrealloc(NULL, sizeof(int))
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of malloc() is present in stdlib.h
Step 2 : Define a pointer
int *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call malloc() to allocate 10 Integers of memory
ptr = malloc(40);
OR
ptr = malloc( 10 * sizeof(int) );
This allocates 40 Bytes of memory in Heap
ptr can be used to access these 40 Bytes of memory in heap
OR
This allocates 10 Integers of memory in Heap
ptr can be used to access these 10 Integers of memory in heap
We have created Ten Integers in heap !
Step 4.1 : Use *ptr or ptr[0] to write to First Integer in heap memory
*ptr = 0x11223344;
OR
ptr[0] = 0x11223344;
*ptr and ptr[0] can be used interchangeably
*ptr, ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 4.2 : ptr[ ] to write to any allocated Integer in heap memory
ptr[0] = 0x11223344;
ptr[1] = 0x55667788;
ptr[2] = 0x99aabbcc;
ptr[9] = 0xddeeff00;
Step 5.1 : Use *ptr or ptr[0] to read from first Integer in heap memory
printf("*ptr = %d\n", *ptr);
OR
printf("ptr[0] = %d\n", ptr[0]);
*ptr and ptr[0] can be used interchangeably
*ptr, ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 5.2 : ptr[ ] to read from any allocated Integer in heap memory
printf("ptr[0] = %d\n", ptr[0]);
printf("ptr[1] = %d\n", ptr[1]);
printf("ptr[2] = %d\n", ptr[2]);
printf("ptr[9] = %d\n", ptr[9]);
Step 6 : Always free the heap memory after usage
free(ptr);
This frees 40 Bytes in Heap which was allocated in Step 3
See full program below
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void)
5{
6 int *ptr;
7
8 ptr = malloc( 10 * sizeof(int) );
9
10 *ptr = 0x11223344;
11
12 printf("*ptr = %d\n", *ptr);
13
14 ptr[0] = 0x11223344;
15 ptr[1] = 0x55667788;
16 ptr[2] = 0x99aabbcc;
17 ptr[9] = 0xddeeff00;
18
19 printf("ptr[0] = %d\n", ptr[0]);
20 printf("ptr[1] = %d\n", ptr[1]);
21 printf("ptr[2] = %d\n", ptr[2]);
22 printf("ptr[9] = %d\n", ptr[9]);
23
24 free(ptr);
25
26 return 0;
27}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of calloc() is present in stdlib.h
Step 2 : Define a pointer
int *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call calloc() to allocate 40 Bytes of memory
ptr = calloc(1, 10 * sizeof(int) );
OR
ptr = calloc(10 * sizeof(int), 1);
OR
ptr = calloc(10, sizeof(int));
OR
ptr = calloc(sizeof(int), 10);
This allocates 40 Bytes of memory in Heap
ptr can be used to access these 40 Bytes of memory in heap
Just make sure multiplication of two arguments is equal to 40 !
OR
This allocates 10 Integers of memory in Heap
ptr can be used to access these 10 Integers of memory in heap
Just make sure multiplication of two arguments is equal to 40 !
We have created Ten Integers in heap !
Step 4.1 : Use *ptr or ptr[0] to write to first Integer in heap memory
*ptr = 0x11223344;
OR
ptr[0] = 0x11223344;
*ptr and ptr[0] can be used interchangeably
*ptr, ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 4.2 : ptr[ ] to write to any allocated Integer in heap memory
ptr[0] = 0x11223344;
ptr[1] = 0x55667788;
ptr[2] = 0x99aabbcc;
ptr[9] = 0xddeeff00;
Step 5.1 : Use *ptr or ptr[0] to read from first Integer in heap memory
printf("*ptr = %d\n", *ptr);
OR
printf("ptr[0] = %d\n", ptr[0]);
*ptr and ptr[0] can be used interchangeably
*ptr, ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 5.2 : ptr[ ] to read from any allocated Integer in heap memory
printf("ptr[0] = %d\n", ptr[0]);
printf("ptr[1] = %d\n", ptr[1]);
printf("ptr[2] = %d\n", ptr[2]);
printf("ptr[9] = %d\n", ptr[9]);
Step 6 : Always free the heap memory after usage
free(ptr);
This frees 40 Bytes in Heap which was allocated in Step 3
See full program below
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void)
5{
6 int *ptr;
7
8 ptr = calloc(10, sizeof(int) );
9
10 *ptr = 0x11223344;
11
12 printf("*ptr = %d\n", *ptr);
13
14 ptr[0] = 0x11223344;
15 ptr[1] = 0x55667788;
16 ptr[2] = 0x99aabbcc;
17 ptr[9] = 0xddeeff00;
18
19 printf("ptr[0] = %d\n", ptr[0]);
20 printf("ptr[1] = %d\n", ptr[1]);
21 printf("ptr[2] = %d\n", ptr[2]);
22 printf("ptr[9] = %d\n", ptr[9]);
23
24 free(ptr);
25
26 return 0;
27}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of realloc() is present in stdlib.h
Step 2 : Define a pointer
int *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call realloc() to allocate 40 Bytes of memory
ptr = realloc(NULL, 10 * sizeof(int) );
This allocates 40 Bytes of memory in Heap
ptr can be used to access these 40 Bytes of memory in heap
This allocates 10 Integers of memory in Heap
ptr can be used to access these 10 Integers of memory in heap
We have created Ten Integers in heap !
Step 4.1 : Use *ptr or ptr[0] to write to first Integer in heap memory
*ptr = 0x11223344;
OR
ptr[0] = 0x11223344;
*ptr and ptr[0] can be used interchangeably
*ptr, ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 4.2 : ptr[ ] to write to any allocated Integer in heap memory
ptr[0] = 0x11223344;
ptr[1] = 0x55667788;
ptr[2] = 0x99aabbcc;
ptr[9] = 0xddeeff00;
Step 5.1 : Use *ptr or ptr[0] to read from first Integer in heap memory
printf("*ptr = %d\n", *ptr);
OR
printf("ptr[0] = %d\n", ptr[0]);
*ptr and ptr[0] can be used interchangeably
*ptr, ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 5.2 : ptr[ ] to read from any allocated Integer in heap memory
printf("ptr[0] = %d\n", ptr[0]);
printf("ptr[1] = %d\n", ptr[1]);
printf("ptr[2] = %d\n", ptr[2]);
printf("ptr[9] = %d\n", ptr[9]);
Step 6 : Always free the heap memory after usage
free(ptr);
This frees 40 Bytes in Heap which was allocated in Step 3
See full program below
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void)
5{
6 int *ptr;
7
8 ptr = realloc(NULL, 10 * sizeof(int) );
9
10 *ptr = 0x11223344;
11
12 printf("*ptr = %d\n", *ptr);
13
14 ptr[0] = 0x11223344;
15 ptr[1] = 0x55667788;
16 ptr[2] = 0x99aabbcc;
17 ptr[9] = 0xddeeff00;
18
19 printf("ptr[0] = %d\n", ptr[0]);
20 printf("ptr[1] = %d\n", ptr[1]);
21 printf("ptr[2] = %d\n", ptr[2]);
22 printf("ptr[9] = %d\n", ptr[9]);
23
24 free(ptr);
25
26 return 0;
27}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of malloc() is present in stdlib.h
Step 2 : Define a structure having an integer pointer as a member
struct ABC
{
int *ptr;
};
Step 3 : Define a variable of structure
struct ABC X;
sizeof(X.ptr) is 8 Bytes
Step 4 : Call malloc() to allocate 40 Bytes of memory
X.ptr = malloc( 10 * sizeof(int) );
This allocates 40 Bytes of memory in Heap
X.ptr can be used to access these 40 Bytes of memory in heap
We have created Ten Integers in heap !
Step 5.1 : Use *X.ptr or X.ptr[0] to write to first Integer in heap
*X.ptr = 0x11223344;
OR
X.ptr[0] = 0x11223344;
*X.ptr and X.ptr[0] can be used interchangeably
*X.ptr, X.ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 5.2 : Use X.ptr[0], X.ptr[1] … X.ptr[9] to write to allocated Integers in Heap
X.ptr[0] = 0x11223344;
X.ptr[1] = 0x55667788;
X.ptr[2] = 0x99aabbcc;
X.ptr[9] = 0xddeeff00;
Step 6.1 : Use *X.ptr or X.ptr[0] to read from first Integer from heap
printf("*X.ptr = %d\n", *X.ptr);
OR
printf("X.ptr[0] = %d\n", X.ptr[0]);
*X.ptr and X.ptr[0] can be used interchangeably
*X.ptr, X.ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 6.2 : Use X.ptr[0], X.ptr[1] … X.ptr[9] to read from allocated Integers in Heap
printf("X.ptr[0] = %d\n", X.ptr[0]);
printf("X.ptr[1] = %d\n", X.ptr[1]);
printf("X.ptr[2] = %d\n", X.ptr[2]);
printf("X.ptr[9] = %d\n", X.ptr[9]);
Step 7 : Always free the heap memory after usage
free(X.ptr);
See full program below
1#include <stdio.h>
2#include <stdlib.h>
3
4struct ABC
5{
6 int *ptr;
7};
8
9int main(void)
10{
11 struct ABC X;
12
13 X.ptr = malloc( 10 * sizeof(int) );
14
15 *X.ptr = 0x11223344;
16
17 printf("*X.ptr = %c\n", *X.ptr);
18
19 X.ptr[0] = 0x11223344;
20 X.ptr[1] = 0x55667788;
21 X.ptr[2] = 0x99aabbcc;
22 X.ptr[9] = 0xddeeff00;
23
24 printf("X.ptr[0] = %d\n", X.ptr[0]);
25 printf("X.ptr[1] = %d\n", X.ptr[1]);
26 printf("X.ptr[2] = %d\n", X.ptr[2]);
27 printf("X.ptr[9] = %d\n", X.ptr[9]);
28
29 free(X.ptr);
30
31 return 0;
32}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of malloc() is present in stdlib.h
Step 2 : Define a structure having an integer pointer as a member
struct ABC
{
int *ptr;
};
Step 3 : Define a variable of structure
struct ABC X;
sizeof(X.ptr) is 8 Bytes
Step 4 : Define a Pointer of structure
struct ABC *P;
Step 5 : Let
P
hold the address ofX
P = &X;
Step 6 : Call malloc() to allocate 40 Bytes of memory
P->ptr = malloc( 10 * sizeof(int) );
This allocates 40 Bytes of memory in Heap
P->ptr can be used to access these 40 Bytes of memory in heap
We have created Ten Integers in heap !
Step 7.1 : Use *P->ptr or P->ptr[0] to write to first Integer in heap
*P->ptr = 0x11223344;
OR
P->ptr[0] = 0x11223344;
*P->ptr and P->ptr[0] can be used interchangeably
*P->ptr, P->ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 7.2 : Use P->ptr[0], P->ptr[1] … P->ptr[9] to write to allocated Integers in Heap
ptr[0] = 0x11223344;
ptr[1] = 0x55667788;
ptr[2] = 0x99aabbcc;
ptr[9] = 0xddeeff00;
Step 8.1 : Use *P->ptr or P->ptr[0] to read from first Integer from heap
printf("*P->ptr = %d\n", *P->ptr);
OR
printf("P->ptr[0] = %d\n", P->ptr[0]);
*P->ptr and P->ptr[0] can be used interchangeably
*P->ptr, P->ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 8.2 : Use P->ptr[0], P->ptr[1] … P->ptr[9] to read from allocated Integers in Heap
printf("P->ptr[0] = %d\n", P->ptr[0]);
printf("P->ptr[1] = %d\n", P->ptr[1]);
printf("P->ptr[2] = %d\n", P->ptr[2]);
printf("P->ptr[9] = %d\n", P->ptr[9]);
Step 9 : Always free the heap memory after usage
free(P->ptr);
See full program below
1#include <stdio.h>
2#include <stdlib.h>
3
4struct ABC
5{
6 int *ptr;
7};
8
9int main(void)
10{
11 struct ABC X;
12 struct ABC *P;
13
14 P = &X;
15
16 P->ptr = malloc( 10 * sizeof(int) );
17
18 *P->ptr = 0x11223344;
19
20 printf("*P->ptr = %c\n", *P->ptr);
21
22 ptr[0] = 0x11223344;
23 ptr[1] = 0x55667788;
24 ptr[2] = 0x99aabbcc;
25 ptr[9] = 0xddeeff00;
26
27 printf("P->ptr[0] = %d\n", P->ptr[0]);
28 printf("P->ptr[1] = %d\n", P->ptr[1]);
29 printf("P->ptr[2] = %d\n", P->ptr[2]);
30 printf("P->ptr[9] = %d\n", P->ptr[9]);
31
32 free(P->ptr);
33
34 return 0;
35}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of malloc() is present in stdlib.h
Step 2 : Define a function whose return type is
int *
int * get_memory(void)
{
}
Step 3 : Define a pointer inside function
int * get_memory(void)
{
int *p;
}
sizeof(ptr) is 8 Bytes
Step 4 : Call malloc() to allocate 10 Integers (40 Bytes) of memory
int * get_memory(void)
{
int *p;
p = malloc( 10 * sizeof(int) );
}
This allocates 40 Bytes of memory in Heap
ptr can be used to access these 40 Bytes of memory in heap
We have created Ten Integers in heap !
Step 5 : Return p
int * get_memory(void)
{
int *p;
p = malloc( 10 * sizeof(int) );
return p;
}
Step 6 : Call the function
get_memory
int *ptr = get_memory();
Step 7.1 : Use *ptr or ptr[0] to write to first Integer in heap memory
*ptr = 0x11223344;
OR
ptr[0] = 0x11223344;
*ptr and ptr[0] can be used interchangeably
*ptr, ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 7.2 : ptr[ ] to write to any allocated Integer in heap memory
ptr[0] = 0x11223344;
ptr[1] = 0x55667788;
ptr[2] = 0x99aabbcc;
ptr[9] = 0xddeeff00;
Step 8.1 : Use *ptr or ptr[0] to read from first Integer in heap memory
printf("*ptr = %d\n", *ptr);
OR
printf("ptr[0] = %d\n", ptr[0]);
*ptr and ptr[0] can be used interchangeably
*ptr, ptr[0] are the ways to access memory. It is called dereferencing of pointer formally
Step 8.2 : ptr[ ] to read from any allocated Integer in heap memory
printf("ptr[0] = %d\n", ptr[0]);
printf("ptr[1] = %d\n", ptr[1]);
printf("ptr[2] = %d\n", ptr[2]);
printf("ptr[9] = %d\n", ptr[9]);
Step 9 : Always free the heap memory after usage
free(ptr);
This frees 40 Bytes in Heap which was allocated in Step 3
See full program below
1#include <stdio.h>
2#include <stdlib.h>
3
4int * get_memory(void)
5{
6 int *p;
7
8 p = malloc( 10 * sizeof(int) );
9
10 return p;
11}
12
13int main(void)
14{
15 int *ptr;
16
17 ptr = get_memory();
18
19 *ptr = 0x11223344;
20
21 printf("*ptr = %d\n", *ptr);
22
23 ptr[0] = 0x11223344;
24 ptr[1] = 0x55667788;
25 ptr[2] = 0x99aabbcc;
26 ptr[9] = 0xddeeff00;
27
28 printf("ptr[0] = %d\n", ptr[0]);
29 printf("ptr[1] = %d\n", ptr[1]);
30 printf("ptr[2] = %d\n", ptr[2]);
31 printf("ptr[9] = %d\n", ptr[9]);
32
33 free(ptr);
34
35 return 0;
36}
Let us look at some of the common and dangerous mistakes done when doing dynamic memory handling
Use without calling malloc, calloc, realloc
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void)
5{
6 int *ptr;
7
8 *ptr = 0x11223344; // This is invalid. Program crashes or corrupts memory
9
10 printf("*ptr = %d\n", *ptr);
11
12 ptr[0] = 0x11223344;
13
14 printf("ptr[0] = %d\n", ptr[0]);
15
16 free(ptr);
17
18 return 0;
19}
Use after free
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void)
5{
6 int *ptr;
7
8 ptr = malloc(10 * sizeof(int) );
9
10 *ptr = 0x11223344;
11
12 printf("*ptr = %d\n", *ptr);
13
14 ptr[0] = 0x11223344;
15
16 printf("ptr[0] = %d\n", ptr[0]);
17
18 free(ptr);
19
20 *ptr = 65; // This is invalid. Program crashes or corrupts memory
21
22 printf("*ptr = %d\n", *ptr);
23
24 return 0;
25}
Not allocating enough memory and hence accessing un-allocated heap space
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void)
5{
6 int *ptr;
7
8 ptr = malloc( 10 * sizeof(int) );
9
10 *ptr = 0x11223344;
11
12 printf("*ptr = %d\n", *ptr);
13
14 // This is invalid. index 11 means Integer 12.
15 // Only 10 Integers are allocated in heap
16 ptr[11] = 0x11223344;
17
18 printf("ptr[0] = %d\n", ptr[0]);
19
20 free(ptr);
21
22 return 0;
23}
Forgot to call free. Causing memory leak
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void)
5{
6 int *ptr;
7
8 ptr = malloc(10 * sizeof(int));
9
10 *ptr = 65;
11
12 printf("*ptr = %d\n", *ptr);
13
14 ptr[0] = 66;
15
16 printf("ptr[0] = %d\n", ptr[0]);
17
18 // No free. Causes memory leak
19
20 return 0;
21}
Current Module
Previous Module
Next Module
Other Modules