Malloc int single pointer =========================== * In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to allocate memory using malloc ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to allocate memory using calloc ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to allocate memory using realloc ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Allocate 1 Integer using malloc ` * :ref:`Allocate 1 Integer using calloc ` * :ref:`Allocate 1 Integer using realloc ` * :ref:`Allocate 10 Integers using malloc ` * :ref:`Allocate 10 Integers using calloc ` * :ref:`Allocate 10 Integers using realloc ` * Allocate 10 Integers for integer pointer inside structure * :ref:`Using structure object ` * :ref:`Using structure pointer ` * :ref:`Allocate Integers of memory inside function and return the pointer for use in caller ` * :ref:`Grave mistakes of dynamic memory allocation ` .. _int_sp_1_integer_malloc: .. tab-set:: .. tab-item:: Allocate 1 Integer using malloc * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of malloc() is present in stdlib.h * Step 2 : Define a pointer .. code-block:: c int *ptr; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(ptr) is 8 Bytes * Step 3 : Call malloc() to allocate 1 Integer of memory .. code-block:: c ptr = malloc(4); // This is not portable // Because it assumes the size of integer as 4 Bytes OR .. code-block:: c ptr = malloc( sizeof(int) ); // This is portable // Creates one Integer in Heap .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * 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 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created one Integer(4 Bytes) in heap ! * Step 4 : Use \*ptr or ptr[0] to write to memory .. code-block:: c *ptr = 65; OR .. code-block:: c ptr[0] = 65; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This statement uses 4 Bytes for writing ! .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*ptr and ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c printf("*ptr = %d\n", *ptr); OR .. code-block:: c printf("ptr[0] = %d\n", ptr[0]); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This statement uses 4 Bytes for Reading ! .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*ptr and ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c free(ptr); * See full program below .. code-block:: c :linenos: :emphasize-lines: 8, 9, 15 #include #include int main(void) { int *ptr; // Creates one Integer in Heap ptr = malloc( sizeof(int) ); *ptr = 65; printf("*ptr = %d\n", *ptr); free(ptr); return 0; } .. _int_sp_1_integer_calloc: .. tab-set:: .. tab-item:: Allocate 1 Integer using calloc * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of calloc() is present in stdlib.h * Step 2 : Define a pointer .. code-block:: c int *ptr; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(ptr) is 8 Bytes * Step 3 : Call calloc() to allocate 1 Integer of memory .. code-block:: c ptr = calloc(1, 4); OR .. code-block:: c ptr = calloc(4, 1); OR .. code-block:: c ptr = calloc( 1, sizeof(int) ); OR .. code-block:: c ptr = calloc( sizeof(int), 1 ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * 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 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created one Integer(4 Bytes) in heap ! * Step 4 : Use \*ptr or ptr[0] to write to memory .. code-block:: c *ptr = 65; OR .. code-block:: c ptr[0] = 65; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This statement uses 4 Bytes for Writing ! .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*ptr and ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c printf("*ptr = %d\n", *ptr); OR .. code-block:: c printf("ptr[0] = %d\n", ptr[0]); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This statement uses 4 Bytes for Reading ! .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*ptr and ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c free(ptr); * See full program below .. code-block:: c :linenos: :emphasize-lines: 8, 14 #include #include int main(void) { int *ptr; ptr = calloc(1, sizeof(int) ); *ptr = 65; printf("*ptr = %d\n", *ptr); free(ptr); return 0; } .. _int_sp_1_integer_realloc: .. tab-set:: .. tab-item:: Allocate 1 Integer using realloc * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of realloc() is present in stdlib.h * Step 2 : Define a pointer .. code-block:: c int *ptr; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(ptr) is 8 Bytes * Step 3 : Call realloc() to allocate 1 Integer of memory .. code-block:: c ptr = realloc(NULL, 4 ); OR .. code-block:: c ptr = realloc(NULL, sizeof(int) ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 1 Integer of memory in Heap * ptr can be used to access this 1 Integer of memory in heap .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created one Integer in heap ! * Step 4 : Use \*ptr or ptr[0] to write to memory .. code-block:: c *ptr = 65; OR .. code-block:: c ptr[0] = 65; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*ptr and ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c printf("*ptr = %d\n", *ptr); OR .. code-block:: c printf("ptr[0] = %d\n", ptr[0]); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*ptr and ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c free(ptr); * See full program below .. code-block:: c :linenos: :emphasize-lines: 8, 14 #include #include int main(void) { int *ptr; ptr = realloc(NULL, sizeof(int) ); *ptr = 65; printf("*ptr = %d\n", *ptr); free(ptr); return 0; } .. tab-set:: .. tab-item:: Summary of 1 Integer allocation * 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 as ``malloc(sizeof(int))`` * ``malloc(4)`` is same as ``calloc(1, sizeof(int))`` * ``malloc(4)`` is same as ``realloc(NULL, sizeof(int))`` .. _int_sp_10_integers_malloc: .. tab-set:: .. tab-item:: Allocate 10 Integers using malloc * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of malloc() is present in stdlib.h * Step 2 : Define a pointer .. code-block:: c int *ptr; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(ptr) is 8 Bytes * Step 3 : Call malloc() to allocate 10 Integers of memory .. code-block:: c ptr = malloc(40); OR .. code-block:: c ptr = malloc( 10 * sizeof(int) ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 40 Bytes of memory in Heap * ptr can be used to access these 40 Bytes of memory in heap OR .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 10 Integers of memory in Heap * ptr can be used to access these 10 Integers of memory in heap .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created Ten Integers in heap ! * Step 4.1 : Use \*ptr or ptr[0] to write to First Integer in heap memory .. code-block:: c *ptr = 0x11223344; OR .. code-block:: c ptr[0] = 0x11223344; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*ptr and ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c 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 .. code-block:: c printf("*ptr = %d\n", *ptr); OR .. code-block:: c printf("ptr[0] = %d\n", ptr[0]); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*ptr and ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c 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 .. code-block:: c free(ptr); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This frees 40 Bytes in Heap which was allocated in Step 3 * See full program below .. code-block:: c :linenos: :emphasize-lines: 8, 24 #include #include int main(void) { int *ptr; ptr = malloc( 10 * sizeof(int) ); *ptr = 0x11223344; printf("*ptr = %d\n", *ptr); ptr[0] = 0x11223344; ptr[1] = 0x55667788; ptr[2] = 0x99aabbcc; ptr[9] = 0xddeeff00; 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]); free(ptr); return 0; } .. _int_sp_10_integers_calloc: .. tab-set:: .. tab-item:: Allocate 10 Integers using calloc * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of calloc() is present in stdlib.h * Step 2 : Define a pointer .. code-block:: c int *ptr; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(ptr) is 8 Bytes * Step 3 : Call calloc() to allocate 40 Bytes of memory .. code-block:: c ptr = calloc(1, 10 * sizeof(int) ); OR .. code-block:: c ptr = calloc(10 * sizeof(int), 1); OR .. code-block:: c ptr = calloc(10, sizeof(int)); OR .. code-block:: c ptr = calloc(sizeof(int), 10); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * 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 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * 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 ! .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created Ten Integers in heap ! * Step 4.1 : Use \*ptr or ptr[0] to write to first Integer in heap memory .. code-block:: c *ptr = 0x11223344; OR .. code-block:: c ptr[0] = 0x11223344; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*ptr and ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c 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 .. code-block:: c printf("*ptr = %d\n", *ptr); OR .. code-block:: c printf("ptr[0] = %d\n", ptr[0]); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*ptr and ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c 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 .. code-block:: c free(ptr); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This frees 40 Bytes in Heap which was allocated in Step 3 * See full program below .. code-block:: c :linenos: :emphasize-lines: 8, 24 #include #include int main(void) { int *ptr; ptr = calloc(10, sizeof(int) ); *ptr = 0x11223344; printf("*ptr = %d\n", *ptr); ptr[0] = 0x11223344; ptr[1] = 0x55667788; ptr[2] = 0x99aabbcc; ptr[9] = 0xddeeff00; 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]); free(ptr); return 0; } .. _int_sp_10_integers_realloc: .. tab-set:: .. tab-item:: Allocate 10 Integers using realloc * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of realloc() is present in stdlib.h * Step 2 : Define a pointer .. code-block:: c int *ptr; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(ptr) is 8 Bytes * Step 3 : Call realloc() to allocate 40 Bytes of memory .. code-block:: c ptr = realloc(NULL, 10 * sizeof(int) ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 40 Bytes of memory in Heap * ptr can be used to access these 40 Bytes of memory in heap .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 10 Integers of memory in Heap * ptr can be used to access these 10 Integers of memory in heap .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created Ten Integers in heap ! * Step 4.1 : Use \*ptr or ptr[0] to write to first Integer in heap memory .. code-block:: c *ptr = 0x11223344; OR .. code-block:: c ptr[0] = 0x11223344; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*ptr and ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c 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 .. code-block:: c printf("*ptr = %d\n", *ptr); OR .. code-block:: c printf("ptr[0] = %d\n", ptr[0]); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*ptr and ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c 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 .. code-block:: c free(ptr); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This frees 40 Bytes in Heap which was allocated in Step 3 * See full program below .. code-block:: c :linenos: :emphasize-lines: 8, 24 #include #include int main(void) { int *ptr; ptr = realloc(NULL, 10 * sizeof(int) ); *ptr = 0x11223344; printf("*ptr = %d\n", *ptr); ptr[0] = 0x11223344; ptr[1] = 0x55667788; ptr[2] = 0x99aabbcc; ptr[9] = 0xddeeff00; 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]); free(ptr); return 0; } .. _int_sp_10_integers_structure_object: .. tab-set:: .. tab-item:: Allocate 40 bytes of memory for integer pointer inside structure : Using structure object * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of malloc() is present in stdlib.h * Step 2 : Define a structure having an integer pointer as a member .. code-block:: c struct ABC { int *ptr; }; * Step 3 : Define a variable of structure .. code-block:: c struct ABC X; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(X.ptr) is 8 Bytes * Step 4 : Call malloc() to allocate 40 Bytes of memory .. code-block:: c X.ptr = malloc( 10 * sizeof(int) ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 40 Bytes of memory in Heap * X.ptr can be used to access these 40 Bytes of memory in heap .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created Ten Integers in heap ! * Step 5.1 : Use \*X.ptr or X.ptr[0] to write to first Integer in heap .. code-block:: c *X.ptr = 0x11223344; OR .. code-block:: c X.ptr[0] = 0x11223344; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*X.ptr and X.ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c 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 .. code-block:: c printf("*X.ptr = %d\n", *X.ptr); OR .. code-block:: c printf("X.ptr[0] = %d\n", X.ptr[0]); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*X.ptr and X.ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c 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 .. code-block:: c free(X.ptr); * See full program below .. code-block:: c :linenos: :emphasize-lines: 11, 13, 29 #include #include struct ABC { int *ptr; }; int main(void) { struct ABC X; X.ptr = malloc( 10 * sizeof(int) ); *X.ptr = 0x11223344; printf("*X.ptr = %c\n", *X.ptr); X.ptr[0] = 0x11223344; X.ptr[1] = 0x55667788; X.ptr[2] = 0x99aabbcc; X.ptr[9] = 0xddeeff00; 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]); free(X.ptr); return 0; } .. _int_sp_10_integers_structure_pointer: .. tab-set:: .. tab-item:: Allocate 40 bytes of memory for an integer pointer inside structure : Using structure pointer * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of malloc() is present in stdlib.h * Step 2 : Define a structure having an integer pointer as a member .. code-block:: c struct ABC { int *ptr; }; * Step 3 : Define a variable of structure .. code-block:: c struct ABC X; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(X.ptr) is 8 Bytes * Step 4 : Define a Pointer of structure .. code-block:: c struct ABC *P; * Step 5 : Let ``P`` hold the address of ``X`` .. code-block:: c P = &X; * Step 6 : Call malloc() to allocate 40 Bytes of memory .. code-block:: c P->ptr = malloc( 10 * sizeof(int) ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 40 Bytes of memory in Heap * P->ptr can be used to access these 40 Bytes of memory in heap .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created Ten Integers in heap ! * Step 7.1 : Use \*P->ptr or P->ptr[0] to write to first Integer in heap .. code-block:: c *P->ptr = 0x11223344; OR .. code-block:: c P->ptr[0] = 0x11223344; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*P->ptr and P->ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c 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 .. code-block:: c printf("*P->ptr = %d\n", *P->ptr); OR .. code-block:: c printf("P->ptr[0] = %d\n", P->ptr[0]); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*P->ptr and P->ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c 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 .. code-block:: c free(P->ptr); * See full program below .. code-block:: c :linenos: :emphasize-lines: 11, 12, 14, 16, 32 #include #include struct ABC { int *ptr; }; int main(void) { struct ABC X; struct ABC *P; P = &X; P->ptr = malloc( 10 * sizeof(int) ); *P->ptr = 0x11223344; printf("*P->ptr = %c\n", *P->ptr); ptr[0] = 0x11223344; ptr[1] = 0x55667788; ptr[2] = 0x99aabbcc; ptr[9] = 0xddeeff00; 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]); free(P->ptr); return 0; } .. _int_sp_alloc_in_callee_free_in_caller: .. tab-set:: .. tab-item:: Allocate 40 bytes of memory inside function and return the pointer for use in caller * Step 1 : Include stdlib.h .. code-block:: c #include .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Because, prototype of malloc() is present in stdlib.h * Step 2 : Define a function whose return type is ``int *`` .. code-block:: c :emphasize-lines: 1 int * get_memory(void) { } * Step 3 : Define a pointer inside function .. code-block:: c :emphasize-lines: 3 int * get_memory(void) { int *p; } .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow sizeof(ptr) is 8 Bytes * Step 4 : Call malloc() to allocate 10 Integers (40 Bytes) of memory .. code-block:: c :emphasize-lines: 5 int * get_memory(void) { int *p; p = malloc( 10 * sizeof(int) ); } .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * This allocates 40 Bytes of memory in Heap * ptr can be used to access these 40 Bytes of memory in heap .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow We have created Ten Integers in heap ! * Step 5 : Return p .. code-block:: c :emphasize-lines: 7 int * get_memory(void) { int *p; p = malloc( 10 * sizeof(int) ); return p; } * Step 6 : Call the function ``get_memory`` .. code-block:: c int *ptr = get_memory(); * Step 7.1 : Use \*ptr or ptr[0] to write to first Integer in heap memory .. code-block:: c *ptr = 0x11223344; OR .. code-block:: c ptr[0] = 0x11223344; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*ptr and ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c 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 .. code-block:: c printf("*ptr = %d\n", *ptr); OR .. code-block:: c printf("ptr[0] = %d\n", ptr[0]); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*ptr and ptr[0] can be used interchangeably .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*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 .. code-block:: c 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 .. code-block:: c free(ptr); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow This frees 40 Bytes in Heap which was allocated in Step 3 * See full program below .. code-block:: c :linenos: :emphasize-lines: 8, 17, 33 #include #include int * get_memory(void) { int *p; p = malloc( 10 * sizeof(int) ); return p; } int main(void) { int *ptr; ptr = get_memory(); *ptr = 0x11223344; printf("*ptr = %d\n", *ptr); ptr[0] = 0x11223344; ptr[1] = 0x55667788; ptr[2] = 0x99aabbcc; ptr[9] = 0xddeeff00; 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]); free(ptr); return 0; } .. _int_sp_alloc_grave_mistakes: .. tab-set:: .. tab-item:: Grave mistakes of dynamic memory allocation * Let us look at some of the common and dangerous mistakes done when doing dynamic memory handling .. tab-set:: .. tab-item:: Use without calling malloc, calloc, realloc .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Use without calling malloc, calloc, realloc .. code-block:: c :linenos: :emphasize-lines: 8 #include #include int main(void) { int *ptr; *ptr = 0x11223344; // This is invalid. Program crashes or corrupts memory printf("*ptr = %d\n", *ptr); ptr[0] = 0x11223344; printf("ptr[0] = %d\n", ptr[0]); free(ptr); return 0; } .. tab-set:: .. tab-item:: Use after free .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Use after free .. code-block:: c :linenos: :emphasize-lines: 8, 20 #include #include int main(void) { int *ptr; ptr = malloc(10 * sizeof(int) ); *ptr = 0x11223344; printf("*ptr = %d\n", *ptr); ptr[0] = 0x11223344; printf("ptr[0] = %d\n", ptr[0]); free(ptr); *ptr = 65; // This is invalid. Program crashes or corrupts memory printf("*ptr = %d\n", *ptr); return 0; } .. tab-set:: .. tab-item:: Not allocating enough memory and hence accessing un-allocated heap space .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Not allocating enough memory and hence accessing un-allocated heap space .. code-block:: c :linenos: :emphasize-lines: 8, 16, 20 #include #include int main(void) { int *ptr; ptr = malloc( 10 * sizeof(int) ); *ptr = 0x11223344; printf("*ptr = %d\n", *ptr); // This is invalid. index 11 means Integer 12. // Only 10 Integers are allocated in heap ptr[11] = 0x11223344; printf("ptr[0] = %d\n", ptr[0]); free(ptr); return 0; } .. tab-set:: .. tab-item:: Forgot to call free. Causing memory leak .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Forgot to call free. Causing memory leak .. code-block:: c :linenos: :emphasize-lines: 8, 20 #include #include int main(void) { int *ptr; ptr = malloc(10 * sizeof(int)); *ptr = 65; printf("*ptr = %d\n", *ptr); ptr[0] = 66; printf("ptr[0] = %d\n", ptr[0]); // No free. Causes memory leak return 0; } .. card:: See Also * Current Module * :doc:`../malloc_ptr` * Previous Module * :doc:`../../array_n_ptrs/array_n_ptrs` * Next Module * :doc:`../../typecasting_n_ptr/typecasting_n_ptr` * Other Modules * :doc:`../../variable_and_ptr/variable_and_ptr` * :doc:`../../funcs_n_ptrs/funcs_n_ptrs` * :doc:`../../memcpy_ptr/memcpy_ptr` * :doc:`../../const_ptr/const_ptr` * :doc:`../../void_ptr/void_ptr` * :doc:`../../array_of_ptr/array_of_ptr` * :doc:`../../ptr_to_array/ptr_to_array` * :doc:`../../function_ptr/function_ptr` * :doc:`../../pre_incr_ptr/pre_incr_ptr` * :doc:`../../post_incr_ptr/post_incr_ptr` * :doc:`../../pre_decr_ptr/pre_decr_ptr` * :doc:`../../post_decr_ptr/post_decr_ptr`