Malloc char 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 40 bytes of memory for character pointer inside structure
Allocate 40 bytes 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
char *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call malloc() to allocate 1 Byte of memory
ptr = malloc(1);
This allocates 1 Byte of memory in Heap
ptr can be used to access this 1 Byte of memory in heap
We have created one character 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 char *ptr;
7
8 ptr = malloc(1);
9
10 *ptr = 65;
11
12 printf("*ptr = %d\n", *ptr);
13 printf("*ptr = %c\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
char *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call calloc() to allocate 1 Byte of memory
ptr = calloc(1, 1);
This allocates 1 Byte of memory in Heap
ptr can be used to access this 1 Byte of memory in heap
We have created one character 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 char *ptr;
7
8 ptr = calloc(1, 1);
9
10 *ptr = 65;
11
12 printf("*ptr = %d\n", *ptr);
13 printf("*ptr = %c\n", *ptr);
14
15 free(ptr);
16
17 return 0;
18}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of realloc() is present in stdlib.h
Step 2 : Define a pointer
char *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call realloc() to allocate 1 Byte of memory
ptr = realloc(NULL, 1);
This allocates 1 Byte of memory in Heap
ptr can be used to access this 1 Byte of memory in heap
We have created one character 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 char *ptr;
7
8 ptr = realloc(NULL, 1);
9
10 *ptr = 65;
11
12 printf("*ptr = %d\n", *ptr);
13 printf("*ptr = %c\n", *ptr);
14
15 free(ptr);
16
17 return 0;
18}
malloc, calloc, realloc are memory allocation functions
free is memory deallocation function
stdlib.h contains prototypes of malloc, calloc, realloc, free
malloc(1)
is same ascalloc(1, 1)
malloc(1)
is same asrealloc(NULL, 1)
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of malloc() is present in stdlib.h
Step 2 : Define a pointer
char *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call malloc() to allocate 40 Bytes of memory
ptr = malloc(40);
This allocates 40 Bytes of memory in Heap
ptr can be used to access these 40 Bytes of memory in heap
We have created Forty characters in heap !
Step 4.1 : Use *ptr or ptr[0] to write to first byte in heap 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 4.2 : ptr[ ] to write to any allocated byte in heap memory
ptr[0] = 66;
ptr[1] = 66;
ptr[2] = 67;
ptr[39] = 100;
Step 5.1 : Use *ptr or ptr[0] to read from first byte 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 byte 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[39] = %d\n", ptr[39]);
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 char *ptr;
7
8 ptr = malloc(40);
9
10 *ptr = 65;
11
12 printf("*ptr = %d\n", *ptr);
13 printf("*ptr = %c\n", *ptr);
14
15 ptr[0] = 66;
16 ptr[1] = 66;
17 ptr[2] = 67;
18 ptr[39] = 100;
19
20 printf("ptr[0] = %d\n", ptr[0]);
21 printf("ptr[1] = %d\n", ptr[1]);
22 printf("ptr[2] = %d\n", ptr[2]);
23 printf("ptr[39] = %d\n", ptr[39]);
24
25 free(ptr);
26
27 return 0;
28}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of calloc() is present in stdlib.h
Step 2 : Define a pointer
char *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call calloc() to allocate 40 Bytes of memory
ptr = calloc(1, 40);
OR
ptr = calloc(40, 1);
OR
ptr = calloc(10, 4);
OR
ptr = calloc(4, 10);
OR
ptr = calloc(2, 20);
OR
ptr = calloc(20, 2);
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 !
We have created Forty characters in heap !
Step 4.1 : Use *ptr or ptr[0] to write to first byte in heap 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 4.2 : ptr[ ] to write to any allocated byte in heap memory
ptr[0] = 66;
ptr[1] = 66;
ptr[2] = 67;
ptr[39] = 100;
Step 5.1 : Use *ptr or ptr[0] to read from first byte 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 byte 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[39] = %d\n", ptr[39]);
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 char *ptr;
7
8 ptr = calloc(1, 40);
9
10 *ptr = 65;
11
12 printf("*ptr = %d\n", *ptr);
13 printf("*ptr = %c\n", *ptr);
14
15 ptr[0] = 66;
16 ptr[1] = 66;
17 ptr[2] = 67;
18 ptr[39] = 100;
19
20 printf("ptr[0] = %d\n", ptr[0]);
21 printf("ptr[1] = %d\n", ptr[1]);
22 printf("ptr[2] = %d\n", ptr[2]);
23 printf("ptr[39] = %d\n", ptr[39]);
24
25 free(ptr);
26
27 return 0;
28}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of realloc() is present in stdlib.h
Step 2 : Define a pointer
char *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call realloc() to allocate 40 Bytes of memory
ptr = realloc(NULL, 40);
This allocates 40 Bytes of memory in Heap
ptr can be used to access these 40 Bytes of memory in heap
We have created Forty characters in heap !
Step 4.1 : Use *ptr or ptr[0] to write to first byte in heap 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 4.2 : ptr[ ] to write to any allocated byte in heap memory
ptr[0] = 66;
ptr[1] = 66;
ptr[2] = 67;
ptr[39] = 100;
Step 5.1 : Use *ptr or ptr[0] to read from first byte 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 byte 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[39] = %d\n", ptr[39]);
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 char *ptr;
7
8 ptr = realloc(NULL, 40);
9
10 *ptr = 65;
11
12 printf("*ptr = %d\n", *ptr);
13 printf("*ptr = %c\n", *ptr);
14
15 ptr[0] = 66;
16 ptr[1] = 66;
17 ptr[2] = 67;
18 ptr[39] = 100;
19
20 printf("ptr[0] = %d\n", ptr[0]);
21 printf("ptr[1] = %d\n", ptr[1]);
22 printf("ptr[2] = %d\n", ptr[2]);
23 printf("ptr[39] = %d\n", ptr[39]);
24
25 free(ptr);
26
27 return 0;
28}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of malloc() is present in stdlib.h
Step 2 : Define a structure having a character pointer as a member
struct ABC
{
char *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(40);
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 Forty characters in heap !
Step 5.1 : Use *X.ptr or X.ptr[0] to write to first Byte in heap
*X.ptr = 65;
OR
X.ptr[0] = 65;
*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[39] to write to allocated Bytes in Heap
X.ptr[0] = 66;
X.ptr[1] = 67;
X.ptr[2] = 68;
X.ptr[39] = 69;
Step 6.1 : Use *X.ptr or X.ptr[0] to read from first Byte 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[39] to read from allocated Bytes 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[3] = %d\n", X.ptr[39]);
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 char *ptr;
7};
8
9int main(void)
10{
11 struct ABC X;
12
13 X.ptr = malloc(40);
14
15 *X.ptr = 65;
16
17 printf("*X.ptr = %c\n", *X.ptr);
18
19 X.ptr[0] = 66;
20 X.ptr[1] = 67;
21 X.ptr[2] = 68;
22 X.ptr[39] = 69;
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[3] = %d\n", X.ptr[39]);
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 a character pointer as a member
struct ABC
{
char *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(40);
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 Forty characters in heap !
Step 7.1 : Use *P->ptr or P->ptr[0] to write to first Byte in heap
*P->ptr = 65;
OR
P->ptr[0] = 65;
*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[39] to write to allocated Bytes in Heap
P->ptr[0] = 66;
P->ptr[1] = 67;
P->ptr[2] = 68;
P->ptr[39] = 69;
Step 8.1 : Use *P->ptr or P->ptr[0] to read from first Byte 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[39] to read from allocated Bytes 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[3] = %d\n", P->ptr[39]);
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 char *ptr;
7};
8
9int main(void)
10{
11 struct ABC X;
12 struct ABC *P;
13
14 P = &X;
15
16 P->ptr = malloc(40);
17
18 *P->ptr = 65;
19
20 printf("*P->ptr = %c\n", *P->ptr);
21
22 P->ptr[0] = 66;
23 P->ptr[1] = 67;
24 P->ptr[2] = 68;
25 P->ptr[39] = 69;
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[3] = %d\n", P->ptr[39]);
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
char *
char * get_memory(void)
{
}
Step 3 : Define a pointer inside function
char * get_memory(void)
{
char *p;
}
sizeof(ptr) is 8 Bytes
Step 4 : Call malloc() to allocate 40 Bytes of memory
char * get_memory(void)
{
char *p;
p = malloc(40);
}
This allocates 40 Bytes of memory in Heap
ptr can be used to access these 40 Bytes of memory in heap
We have created Forty characters in heap !
Step 5 : Return p
char * get_memory(void)
{
char *p;
p = malloc(40);
return p;
}
Step 6 : Call the function
get_memory
char *ptr = get_memory();
Step 7.1 : Use *ptr or ptr[0] to write to first byte in heap 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 7.2 : ptr[ ] to write to any allocated byte in heap memory
ptr[0] = 66;
ptr[1] = 66;
ptr[2] = 67;
ptr[39] = 100;
Step 8.1 : Use *ptr or ptr[0] to read from first byte 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 byte 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[39] = %d\n", ptr[39]);
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
4char * get_memory(void)
5{
6 char *p;
7
8 p = malloc(40);
9
10 return p;
11}
12
13int main(void)
14{
15 char *ptr;
16
17 ptr = get_memory();
18
19 *ptr = 65;
20
21 printf("*ptr = %d\n", *ptr);
22 printf("*ptr = %c\n", *ptr);
23
24 ptr[0] = 66;
25 ptr[1] = 66;
26 ptr[2] = 67;
27 ptr[39] = 100;
28
29 printf("ptr[0] = %d\n", ptr[0]);
30 printf("ptr[1] = %d\n", ptr[1]);
31 printf("ptr[2] = %d\n", ptr[2]);
32 printf("ptr[39] = %d\n", ptr[39]);
33
34 free(ptr);
35
36 return 0;
37}
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 char *ptr;
7
8 *ptr = 65; // This is invalid. Program crashes or corrupts memory
9
10 printf("*ptr = %d\n", *ptr);
11
12 ptr[0] = 66;
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 char *ptr;
7
8 ptr = malloc(10);
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 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 char *ptr;
7
8 ptr = malloc(10);
9
10 *ptr = 65;
11
12 printf("*ptr = %d\n", *ptr);
13
14 // This is invalid. index 11 means Byte 12.
15 // Only 10 Bytes are allocated in heap
16 ptr[11] = 66;
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 char *ptr;
7
8 ptr = malloc(10);
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