Malloc structures 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 Structure Objects for structure pointer inside structure
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of malloc() is present in stdlib.h
Step 2 : Define a pointer
struct ABC
{
int a;
int b;
};
struct ABC *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call malloc() to allocate 1 Structure Object of memory
ptr = malloc( sizeof(struct ABC) );
This allocates 1 Structure Object of memory in Heap
ptr can be used to access this 1 Structure Object of memory in heap
In our case sizeof(struct ABC) is equal to 8 Bytes
We have created One Structure Object(8 Bytes) in heap !
Step 4 : Use
(*ptr)
,ptr[0]
,ptr->
to write to memory
(*ptr).a = 65;
(*ptr).b = 66;
OR
ptr[0].a = 65;
ptr[0].b = 66;
OR
ptr->a = 65;
ptr->b = 66;
(*ptr)
, ptr[0]
and ptr->
can be used interchangeably
(*ptr)
, ptr[0]
and ptr->
are the ways to access memory. It is called dereferencing of pointer formally
Step 5 : Use
(*ptr)
,ptr[0]
,ptr->
to read from memory
printf("(*ptr).a = %d\n", (*ptr).a);
OR
printf("ptr[0].a = %d\n", ptr[0].a);
OR
printf("ptr->a = %d\n", ptr->a);
(*ptr)
, ptr[0]
, ptr->
can be used interchangeably
(*ptr)
, ptr[0]
, ptr->
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
4struct ABC
5{
6 int a;
7 int b;
8};
9
10int main(void)
11{
12 ptr = malloc(sizeof(struct ABC));
13
14 (*ptr).a = 10;
15 printf("(*ptr).a = %d\n", (*ptr).a);
16
17 ptr[0].a = 20;
18 printf("ptr[0].a = %d\n", ptr[0].a);
19
20 ptr->a = 30;
21 printf("ptr->a = %d\n", ptr->a);
22
23 free(ptr);
24
25 return 0;
26}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of calloc() is present in stdlib.h
Step 2 : Define a pointer
struct ABC
{
int a;
int b;
};
struct ABC *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call calloc() to allocate 1 Structure Object of memory
ptr = calloc( 1, sizeof(struct ABC) );
OR
ptr = calloc( sizeof(struct ABC), 1 );
This allocates 1 Structure Object of memory in Heap
ptr can be used to access this 1 Structure Object of memory in heap
In our case sizeof(struct ABC) is equal to 8 Bytes
We have created One Structure Object(8 Bytes) in heap !
Step 4 : Use
(*ptr)
,ptr[0]
,ptr->
to write to memory
(*ptr).a = 65;
(*ptr).b = 66;
OR
ptr[0].a = 65;
ptr[0].b = 66;
OR
ptr->a = 65;
ptr->b = 66;
(*ptr)
, ptr[0]
and ptr->
can be used interchangeably
(*ptr)
, ptr[0]
and ptr->
are the ways to access memory. It is called dereferencing of pointer formally
Step 5 : Use
(*ptr)
,ptr[0]
,ptr->
to read from memory
printf("(*ptr).a = %d\n", (*ptr).a);
OR
printf("ptr[0].a = %d\n", ptr[0].a);
OR
printf("ptr->a = %d\n", ptr->a);
(*ptr)
, ptr[0]
, ptr->
can be used interchangeably
(*ptr)
, ptr[0]
, ptr->
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
4struct ABC
5{
6 int a;
7 int b;
8};
9
10int main(void)
11{
12 ptr = calloc( 1, sizeof(struct ABC) );
13
14 (*ptr).a = 10;
15 printf("(*ptr).a = %d\n", (*ptr).a);
16
17 ptr[0].a = 20;
18 printf("ptr[0].a = %d\n", ptr[0].a);
19
20 ptr->a = 30;
21 printf("ptr->a = %d\n", ptr->a);
22
23 free(ptr);
24
25 return 0;
26}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of realloc() is present in stdlib.h
Step 2 : Define a pointer
struct ABC
{
int a;
int b;
};
struct ABC *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call realloc() to allocate 1 Structure Object of memory
ptr = realloc( NULL, sizeof(struct ABC) );
This allocates 1 Structure Object of memory in Heap
ptr can be used to access this 1 Structure Object of memory in heap
In our case sizeof(struct ABC) is equal to 8 Bytes
We have created One Structure Object(8 Bytes) in heap !
Step 4 : Use
(*ptr)
,ptr[0]
,ptr->
to write to memory
(*ptr).a = 65;
(*ptr).b = 66;
OR
ptr[0].a = 65;
ptr[0].b = 66;
OR
ptr->a = 65;
ptr->b = 66;
(*ptr)
, ptr[0]
and ptr->
can be used interchangeably
(*ptr)
, ptr[0]
and ptr->
are the ways to access memory. It is called dereferencing of pointer formally
Step 5 : Use
(*ptr)
,ptr[0]
,ptr->
to read from memory
printf("(*ptr).a = %d\n", (*ptr).a);
OR
printf("ptr[0].a = %d\n", ptr[0].a);
OR
printf("ptr->a = %d\n", ptr->a);
(*ptr)
, ptr[0]
, ptr->
can be used interchangeably
(*ptr)
, ptr[0]
, ptr->
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
4struct ABC
5{
6 int a;
7 int b;
8};
9
10int main(void)
11{
12 ptr = realloc( NULL, sizeof(struct ABC) );
13
14 (*ptr).a = 10;
15 printf("(*ptr).a = %d\n", (*ptr).a);
16
17 ptr[0].a = 20;
18 printf("ptr[0].a = %d\n", ptr[0].a);
19
20 ptr->a = 30;
21 printf("ptr->a = %d\n", ptr->a);
22
23 free(ptr);
24
25 return 0;
26}
malloc, calloc, realloc are memory allocation functions
free is memory deallocation function
stdlib.h contains prototypes of malloc, calloc, realloc, free
malloc(sizeof(struct ABC))
is same ascalloc(1, sizeof(struct ABC))
malloc(sizeof(struct ABC))
is same ascalloc(sizeof(struct ABC), 1)
malloc(sizeof(struct ABC))
is same asrealloc(NULL, sizeof(struct ABC))
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of malloc() is present in stdlib.h
Step 2 : Define a pointer
struct ABC
{
int a;
int b;
};
struct ABC *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call malloc() to allocate 10 Structure Objects
ptr = malloc( 10 * sizeof(struct ABC) );
This allocates 10 Structure Objects(in this case 80 Bytes) of memory in Heap
ptr can be used to access these 10 Structure Objects of memory in heap
We have created Ten Structure Objects in heap !
Step 4.1 : Use
(*ptr)
,ptr[0]
,ptr->
to write to First Structure Object in heap memory
(*ptr).a = 0x11223344;
OR
ptr[0].a = 0x11223344;
OR
ptr->a = 0x11223344;
(*ptr)
, ptr[0]
, ptr->
can be used interchangeably
(*ptr)
, ptr[0]
, ptr->
are the ways to access memory. It is called dereferencing of pointer formally
Step 4.2 : ptr[ ] to write to any allocated Structure in heap memory
ptr[0].a = 0x11223344;
ptr[1].a = 0x55667788;
ptr[2].a = 0x99aabbcc;
ptr[9].a = 0xddeeff00;
Step 5.1 : Use
(*ptr)
,ptr[0]
,ptr->
to read from first Structure Object in heap memory
printf("(*ptr).a = %x\n", (*ptr).a);
OR
printf("ptr[0].a = %x\n", ptr[0].a);
OR
printf("ptr->a = %x\n", ptr->a);
(*ptr)
and ptr[0]
and ptr->
can be used interchangeably
(*ptr)
, ptr[0]
, ptr->
are the ways to access memory. It is called dereferencing of pointer formally
Step 5.2 : ptr[ ] to read from any allocated Structure Object in heap memory
printf("ptr[0].a = %x\n", ptr[0].a);
printf("ptr[1].a = %x\n", ptr[1].a);
printf("ptr[2].a = %x\n", ptr[2].a);
printf("ptr[9].a = %x\n", ptr[9].a);
Step 6 : Always free the heap memory after usage
free(ptr);
This frees 10 Structure Objects (80 Bytes) in Heap which was allocated in Step 3
See full program below
1#include <stdio.h>
2#include <stdlib.h>
3
4struct ABC
5{
6 int a;
7 int b;
8};
9
10int main(void)
11{
12 struct ABC *ptr;
13
14 ptr = malloc( 10 * sizeof(struct ABC) );
15
16 (*ptr).a = 0x11223344;
17
18 printf("(*ptr).a = %x\n", (*ptr).a);
19
20 ptr[0].a = 0x11223344;
21 ptr[1].a = 0x55667788;
22 ptr[2].a = 0x99aabbcc;
23 ptr[9].a = 0xddeeff00;
24
25 printf("ptr[0].a = %x\n", ptr[0].a);
26 printf("ptr[1].a = %x\n", ptr[1].a);
27 printf("ptr[2].a = %x\n", ptr[2].a);
28 printf("ptr[9].a = %x\n", ptr[9].a);
29
30 free(ptr);
31
32 return 0;
33}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of calloc() is present in stdlib.h
Step 2 : Define a pointer
struct ABC
{
int a;
int b;
};
struct ABC *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call calloc() to allocate 10 Structure Objects
ptr = calloc( 1, 10 * sizeof(struct ABC) );
OR
ptr = calloc( 10 * sizeof(struct ABC), 1 );
This allocates 10 Structure Objects(in this case 80 Bytes) of memory in Heap
ptr can be used to access these 10 Structure Objects of memory in heap
We have created Ten Structure Objects in heap !
Step 4.1 : Use
(*ptr)
,ptr[0]
,ptr->
to write to First Structure Object in heap memory
(*ptr).a = 0x11223344;
OR
ptr[0].a = 0x11223344;
OR
ptr->a = 0x11223344;
(*ptr)
, ptr[0]
, ptr->
can be used interchangeably
(*ptr)
, ptr[0]
, ptr->
are the ways to access memory. It is called dereferencing of pointer formally
Step 4.2 : ptr[ ] to write to any allocated Structure in heap memory
ptr[0].a = 0x11223344;
ptr[1].a = 0x55667788;
ptr[2].a = 0x99aabbcc;
ptr[9].a = 0xddeeff00;
Step 5.1 : Use
(*ptr)
,ptr[0]
,ptr->
to read from first Structure Object in heap memory
printf("(*ptr).a = %x\n", (*ptr).a);
OR
printf("ptr[0].a = %x\n", ptr[0].a);
OR
printf("ptr->a = %x\n", ptr->a);
(*ptr)
and ptr[0]
and ptr->
can be used interchangeably
(*ptr)
, ptr[0]
, ptr->
are the ways to access memory. It is called dereferencing of pointer formally
Step 5.2 : ptr[ ] to read from any allocated Structure Object in heap memory
printf("ptr[0].a = %x\n", ptr[0].a);
printf("ptr[1].a = %x\n", ptr[1].a);
printf("ptr[2].a = %x\n", ptr[2].a);
printf("ptr[9].a = %x\n", ptr[9].a);
Step 6 : Always free the heap memory after usage
free(ptr);
This frees 10 Structure Objects (80 Bytes) in Heap which was allocated in Step 3
See full program below
1#include <stdio.h>
2#include <stdlib.h>
3
4struct ABC
5{
6 int a;
7 int b;
8};
9
10int main(void)
11{
12 struct ABC *ptr;
13
14 ptr = calloc( 1, 10 * sizeof(struct ABC) );
15
16 (*ptr).a = 0x11223344;
17
18 printf("(*ptr).a = %x\n", (*ptr).a);
19
20 ptr[0].a = 0x11223344;
21 ptr[1].a = 0x55667788;
22 ptr[2].a = 0x99aabbcc;
23 ptr[9].a = 0xddeeff00;
24
25 printf("ptr[0].a = %x\n", ptr[0].a);
26 printf("ptr[1].a = %x\n", ptr[1].a);
27 printf("ptr[2].a = %x\n", ptr[2].a);
28 printf("ptr[9].a = %x\n", ptr[9].a);
29
30 free(ptr);
31
32 return 0;
33}
Step 1 : Include stdlib.h
#include <stdlib.h>
Because, prototype of realloc() is present in stdlib.h
Step 2 : Define a pointer
struct ABC
{
int a;
int b;
};
struct ABC *ptr;
sizeof(ptr) is 8 Bytes
Step 3 : Call malloc() to allocate 10 Structure Objects
ptr = realloc( NULL, 10 * sizeof(struct ABC) );
This allocates 10 Structure Objects(in this case 80 Bytes) of memory in Heap
ptr can be used to access these 10 Structure Objects of memory in heap
We have created Ten Structure Objects in heap !
Step 4.1 : Use
(*ptr)
,ptr[0]
,ptr->
to write to First Structure Object in heap memory
(*ptr).a = 0x11223344;
OR
ptr[0].a = 0x11223344;
OR
ptr->a = 0x11223344;
(*ptr)
, ptr[0]
, ptr->
can be used interchangeably
(*ptr)
, ptr[0]
, ptr->
are the ways to access memory. It is called dereferencing of pointer formally
Step 4.2 : ptr[ ] to write to any allocated Structure in heap memory
ptr[0].a = 0x11223344;
ptr[1].a = 0x55667788;
ptr[2].a = 0x99aabbcc;
ptr[9].a = 0xddeeff00;
Step 5.1 : Use
(*ptr)
,ptr[0]
,ptr->
to read from first Structure Object in heap memory
printf("(*ptr).a = %x\n", (*ptr).a);
OR
printf("ptr[0].a = %x\n", ptr[0].a);
OR
printf("ptr->a = %x\n", ptr->a);
(*ptr)
and ptr[0]
and ptr->
can be used interchangeably
(*ptr)
, ptr[0]
, ptr->
are the ways to access memory. It is called dereferencing of pointer formally
Step 5.2 : ptr[ ] to read from any allocated Structure Object in heap memory
printf("ptr[0].a = %x\n", ptr[0].a);
printf("ptr[1].a = %x\n", ptr[1].a);
printf("ptr[2].a = %x\n", ptr[2].a);
printf("ptr[9].a = %x\n", ptr[9].a);
Step 6 : Always free the heap memory after usage
free(ptr);
This frees 10 Structure Objects (80 Bytes) in Heap which was allocated in Step 3
See full program below
1#include <stdio.h>
2#include <stdlib.h>
3
4struct ABC
5{
6 int a;
7 int b;
8};
9
10int main(void)
11{
12 struct ABC *ptr;
13
14 ptr = realloc( NULL, 10 * sizeof(struct ABC) );
15
16 (*ptr).a = 0x11223344;
17
18 printf("(*ptr).a = %x\n", (*ptr).a);
19
20 ptr[0].a = 0x11223344;
21 ptr[1].a = 0x55667788;
22 ptr[2].a = 0x99aabbcc;
23 ptr[9].a = 0xddeeff00;
24
25 printf("ptr[0].a = %x\n", ptr[0].a);
26 printf("ptr[1].a = %x\n", ptr[1].a);
27 printf("ptr[2].a = %x\n", ptr[2].a);
28 printf("ptr[9].a = %x\n", ptr[9].a);
29
30 free(ptr);
31
32 return 0;
33}
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
{
int a;
int b;
};
struct XYZ
{
struct ABC *ptr;
};
Step 3 : Define a variable of structure
struct XYZ X;
sizeof(X.ptr) is 8 Bytes
Step 4 : Call malloc() to allocate 10 Structure Objects
X.ptr = malloc( 10 * sizeof(struct ABC) );
This allocates 10 Structure Objects(80 Bytes) of memory in Heap
X.ptr can be used to access these 10 Structure Objects(80 Bytes) of memory in heap
We have created Ten Structure Objects in heap !
Step 5.1 : Use
(*X.ptr)
orX.ptr[0]
orX.ptr->
to write to first Structure Object in heap
(*X.ptr).a = 0x11223344;
OR
X.ptr[0].a = 0x11223344;
OR
X.ptr->a = 0x11223344;
(*X.ptr)
and X.ptr[0]
and X.ptr->
can be used interchangeably
(*X.ptr)
, X.ptr[0]
, X.ptr->
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 Structure Objects in Heap
X.ptr[0].a = 0x11223344;
X.ptr[1].a = 0x55667788;
X.ptr[2].a = 0x99aabbcc;
X.ptr[9].a = 0xddeeff00;
Step 6.1 : Use
(*X.ptr)
orX.ptr[0]
orX.ptr->
to read from first Structure Object from heap
printf("(*X.ptr).a = %d\n", (*X.ptr).a);
OR
printf("X.ptr[0].a = %d\n", X.ptr[0].a);
OR
printf("X.ptr->a = %d\n", X.ptr->a);
(*X.ptr)
and X.ptr[0]
and X.ptr->
can be used interchangeably
(*X.ptr)
, X.ptr[0]
, X.ptr->
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 Structure Objects in Heap
printf("X.ptr[0].a = %d\n", X.ptr[0].a);
printf("X.ptr[1].a = %d\n", X.ptr[1].a);
printf("X.ptr[2].a = %d\n", X.ptr[2].a);
printf("X.ptr[9].a = %d\n", X.ptr[9].a);
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 a;
7 int b;
8};
9
10struct XYZ
11{
12 struct ABC *ptr;
13};
14
15int main(void)
16{
17 struct XYZ X;
18
19 X.ptr = malloc( 10 * sizeof(struct ABC) );
20
21 (*X.ptr).a = 0x11223344;
22 printf("(*X.ptr).a = %x\n", (*X.ptr).a);
23
24 X.ptr[0].a = 0x11223344;
25 printf("X.ptr[0].a = %x\n", X.ptr[0].a);
26
27 X.ptr->a = 0x11223344;
28 printf("X.ptr->a = %x\n", X.ptr->a);
29
30 X.ptr[0].a = 0x11223344;
31 X.ptr[1].a = 0x55667788;
32 X.ptr[2].a = 0x99aabbcc;
33 X.ptr[9].a = 0xddeeff00;
34
35 printf("X.ptr[0].a = %x\n", X.ptr[0].a);
36 printf("X.ptr[1].a = %x\n", X.ptr[1].a);
37 printf("X.ptr[2].a = %x\n", X.ptr[2].a);
38 printf("X.ptr[9].a = %x\n", X.ptr[9].a);
39
40 free(X.ptr);
41
42 return 0;
43}
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
{
int a;
int b;
};
struct XYZ
{
struct ABC *ptr;
};
Step 3 : Define a variable of structure
struct XYZ X;
sizeof(X.ptr) is 8 Bytes
Step 4 : Define a pointer to X
struct XYZ *P;
P = &X;
Step 4 : Call malloc() to allocate 10 Structure Objects
P->ptr = malloc( 10 * sizeof(struct ABC) );
OR
P[0].ptr = malloc( 10 * sizeof(struct ABC) );
OR
(*P).ptr = malloc( 10 * sizeof(struct ABC) );
This allocates 10 Structure Objects(80 Bytes) of memory in Heap
P->ptr, P[0].ptr, (*P).ptr can be used to access these 10 Structure Objects(80 Bytes) of memory in heap
We have created Ten Structure Objects in heap !
Step 5.1 : Use
P
to write to first Structure Object in heap
P->ptr->a = 0x11223344;
OR
P->ptr[0].a = 0x11223344;
OR
(*P->ptr).a = 0x11223344;
(*P->ptr)
and P->ptr[0]
and P->ptr->
can be used interchangeably
(*P->ptr)
and P->ptr[0]
and P->ptr->
are the ways to access memory. It is called dereferencing of pointer formally
Step 5.2 : Use P->ptr[0], P->ptr[1] … P->ptr[9] to write to allocated Structure Objects in Heap
P->ptr[0].a = 0x11223344;
P->ptr[1].a = 0x55667788;
P->ptr[2].a = 0x99aabbcc;
P->ptr[9].a = 0xddeeff00;
Step 6.1 : Use
(*P->ptr)
andP->ptr[0]
andP->ptr->
to read from first Structure Object from heap
printf("(*P->ptr).a = %d\n", (*P->ptr).a);
OR
printf("P->ptr[0].a = %d\n", P->ptr[0].a);
OR
printf("P->ptr->a = %d\n", P->ptr->a);
(*P->ptr)
and P->ptr[0]
and P->ptr->
can be used interchangeably
(*P->ptr)
and P->ptr[0]
and P->ptr->
are the ways to access memory. It is called dereferencing of pointer formally
Step 6.2 : Use P->ptr[0], P->ptr[1] … P->ptr[9] to read from allocated Structure Objects in Heap
printf("P->ptr[0].a = %x\n", P->ptr[0].a);
printf("P->ptr[1].a = %x\n", P->ptr[1].a);
printf("P->ptr[2].a = %x\n", P->ptr[2].a);
printf("P->ptr[9].a = %x\n", P->ptr[9].a);
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 a;
7 int b;
8};
9
10struct XYZ
11{
12 struct ABC *ptr;
13};
14
15int main(void)
16{
17 struct XYZ X;
18
19 struct XYZ *P;
20
21 P = &X;
22
23 P->ptr = malloc( 10 * sizeof(struct ABC) );
24
25 (*P->ptr).a = 0x11223344;
26 printf("(*P->ptr).a = %x\n", (*P->ptr).a);
27
28 P->ptr[0].a = 0x11223344;
29 printf("P->ptr[0].a = %x\n", P->ptr[0].a);
30
31 P->ptr->a = 0x11223344;
32 printf("P->ptr->a = %x\n", P->ptr->a);
33
34 P->ptr[0].a = 0x11223344;
35 P->ptr[1].a = 0x55667788;
36 P->ptr[2].a = 0x99aabbcc;
37 P->ptr[9].a = 0xddeeff00;
38
39 printf("P->ptr[0].a = %x\n", P->ptr[0].a);
40 printf("P->ptr[1].a = %x\n", P->ptr[1].a);
41 printf("P->ptr[2].a = %x\n", P->ptr[2].a);
42 printf("P->ptr[9].a = %x\n", P->ptr[9].a);
43
44 free(X.ptr);
45
46 return 0;
47}
Current Module
Previous Module
Next Module
Other Modules