Malloc Exercise ==================== .. tab-set:: .. tab-item:: Question 1 : Consider below code snippet and store value in memory pointed by ``ip`` .. code-block:: c struct ABC { int **ip; }*sp; .. dropdown:: See Answer * Step 1 : List all possible pointers from outermost to innermost ======================= ================================ Pointer ======================= ================================ ``sp`` ``sp`` is a single pointer ``ip`` ``ip`` is a double pointer ``*ip`` ``*ip`` is a single pointer ======================= ================================ * Step 2 : Allocate from Outermost pointer and move inner .. code-block:: c // single pointer "sp" points to array of structures sp = malloc(sizeof(struct ABC)); // double pointer "ip" points to array of single pointers sp->ip = malloc(sizeof(int *)); // single pointer "*ip" points to array of integers *sp->ip = malloc(sizeof(int)); * Step 3 : Derefer 3 times to store user data as there are 3 pointers .. code-block:: c **sp->ip = 65; * Step 4 : Free from inner most .. code-block:: c free(*sp->ip); free(sp->ip); free(sp); * Step 5 : See full program below .. code-block:: c #include #include struct ABC { int **ip; }*sp; int main(void) { // single pointer "sp" points to array of structures sp = malloc(sizeof(struct ABC)); // double pointer "ip" points to array of single pointers sp->ip = malloc(sizeof(int *)); // single pointer "*ip" points to array of integers *sp->ip = malloc(sizeof(int)); **sp->ip = 65; printf("**sp->ip = %d\n", **sp->ip); // Free in the opposite order of allocation free(*sp->ip); free(sp->ip); free(sp); return 0; } .. tab-set:: .. tab-item:: Question 2 : Consider below code snippet and store value in memory pointed by ``ip`` .. code-block:: c struct ABC { int ***ip; }*sp; .. dropdown:: See Answer * Step 1 : List all possible pointers from outermost to innermost ======================= ================================ Pointer ======================= ================================ ``sp`` ``sp`` is a single pointer ``ip`` ``ip`` is a triple pointer ``*ip`` ``*ip`` is a double pointer ``**ip`` ``**ip`` is a single pointer ======================= ================================ * Step 2 : Allocate from Outermost pointer and move inner .. code-block:: c // "sp" is a single pointer and points to array of structures sp = malloc(sizeof(struct ABC)); // "ip" is a triple pointer and points to array of double pointers sp->ip = malloc(sizeof(int **)); // "*ip" is a double pointer and points to array of single pointers *sp->ip = malloc(sizeof(int *)); // "**ip" is a single pointer and points to array of integers **sp->ip = malloc(sizeof(int)); * Step 3 : Derefer 4 times to store user data as there are 4 pointers .. code-block:: c ***sp->ip = 65; printf("***sp->ip = %d\n", ***sp->ip); * Step 4 : Free from inner most .. code-block:: c free(**sp->ip); free(*sp->ip); free(sp->ip); free(sp); * Step 5 : See full program below .. code-block:: c #include #include struct ABC { int ***ip; }*sp; int main(void) { // Allocate 4 times. Since there are 4 pointers // "sp" is a single pointer and points to array of structures sp = malloc(sizeof(struct ABC)); // "ip" is a triple pointer and points to array of double pointers sp->ip = malloc(sizeof(int **)); // "*ip" is a double pointer and points to array of single pointers *sp->ip = malloc(sizeof(int *)); // "**ip" is a single pointer and points to array of integers **sp->ip = malloc(sizeof(int)); // Derefer 4 times. Since there are 4 pointers ***sp->ip = 65; printf("***sp->ip = %d\n", ***sp->ip); // Free in the opposite order of allocation free(**sp->ip); free(*sp->ip); free(sp->ip); free(sp); return 0; } .. tab-set:: .. tab-item:: Question 3 : Consider below code snippet and store value in memory pointed by ``ip`` .. code-block:: c struct ABC { int **ip; }**sp; .. dropdown:: See Answer * Step 1 : List all possible pointers from outermost to innermost ======================= ================================ Pointer ======================= ================================ ``sp`` ``sp`` is a double pointer ``*sp`` ``*sp`` is a single pointer ``ip`` ``ip`` is a double pointer ``*ip`` ``*ip`` is a single pointer ======================= ================================ * Step 2 : Allocate from Outermost pointer and move inner .. code-block:: c // "sp" is a double pointer and points to array of single pointers sp = malloc(sizeof(struct ABC *)); // "*sp" is a single pointer and points to array of structures *sp = malloc(sizeof(struct ABC)); // "ip" is a double pointer and points to array of single pointers (**sp).ip = malloc(sizeof(int *)); // "*ip" is a single pointer and points to array of integers *(**sp).ip = malloc(sizeof(int)); * Step 3 : Derefer 4 times to store user data as there are 4 pointers .. code-block:: c **(**sp).ip = 65; printf("**(**sp).ip = %d\n", **(**sp).ip); * Step 4 : Free from inner most .. code-block:: c free(*(**sp).ip); free((**sp).ip); free(*sp); free(sp); * Step 5 : See full program below .. code-block:: c #include #include struct ABC { int **ip; }**sp; int main(void) { // Allocate 4 times. Since there are 4 pointers // "sp" is a double pointer and points to array of single pointers sp = malloc(sizeof(struct ABC *)); // "*sp" is a single pointer and points to array of structures *sp = malloc(sizeof(struct ABC)); // "ip" is a double pointer and points to array of single pointers (**sp).ip = malloc(sizeof(int *)); // "*ip" is a single pointer and points to array of integers *(**sp).ip = malloc(sizeof(int)); // Derefer 4 times. Since there are 4 pointers **(**sp).ip = 65; printf("**(**sp).ip = %d\n", **(**sp).ip); // Free in the opposite order of allocation free(*(**sp).ip); free((**sp).ip); free(*sp); free(sp); return 0; } .. tab-set:: .. tab-item:: Question 4 : Consider below code snippet and store value in memory pointed by ``ip`` .. code-block:: c struct ABC { int ***ip; }***sp; .. dropdown:: See Answer * Step 1 : List all possible pointers from outermost to innermost ======================= ================================ Pointer ======================= ================================ ``sp`` ``sp`` is a triple pointer ``*sp`` ``*sp`` is a double pointer ``**sp`` ``**sp`` is a single pointer ``ip`` ``ip`` is a triple pointer ``*ip`` ``*ip`` is a double pointer ``**ip`` ``**ip`` is a single pointer ======================= ================================ * Step 2 : Allocate from Outermost pointer and move inner .. code-block:: c sp = malloc(sizeof(struct ABC **)); *sp = malloc(sizeof(struct ABC *)); **sp = malloc(sizeof(struct ABC )); (***sp).ip = malloc(sizeof(int **)); *(***sp).ip = malloc(sizeof(int *)); **(***sp).ip = malloc(sizeof(int )); * Step 3 : Derefer 6 times to store user data as there are 6 pointers .. code-block:: c ***(***sp).ip = 65; printf("***(***sp).ip = %d\n", ***(***sp).ip); * Step 4 : Free from inner most .. code-block:: c free(**(***sp).ip); free(*(***sp).ip); free((***sp).ip); free(**sp); free(*sp); free(sp); * Step 5 : See full program below .. code-block:: c #include #include struct ABC { int ***ip; }***sp; int main(void) { // Allocate 6 times. Since there are 6 pointers sp = malloc(sizeof(struct ABC **)); *sp = malloc(sizeof(struct ABC *)); **sp = malloc(sizeof(struct ABC )); (***sp).ip = malloc(sizeof(int **)); *(***sp).ip = malloc(sizeof(int *)); **(***sp).ip = malloc(sizeof(int )); ***(***sp).ip = 65; printf("***(***sp).ip = %d\n", ***(***sp).ip); free(**(***sp).ip); free(*(***sp).ip); free((***sp).ip); free(**sp); free(*sp); free(sp); return 0; } .. tab-set:: .. tab-item:: Question 5 : Consider below code snippet and store value in memory pointed by ``ip`` .. code-block:: c struct MNP { int *ip; }; struct PQR { struct MNP **mnp_ptr; }; struct ABC { struct PQR *pqr_ptr; }*abc_ptr; .. dropdown:: See Answer * Step 1 : List all possible pointers from outermost to innermost ======================= ================================ Pointer ======================= ================================ ``abc_ptr`` ``abc_ptr`` is a single pointer ``pqr_ptr`` ``pqr_ptr`` is a single pointer ``mnp_ptr`` ``mnp_ptr`` is a double pointer ``*mnp_ptr`` ``*mnp_ptr`` is a single pointer ``ip`` ``ip`` is a single pointer ======================= ================================ * Step 2 : Allocate from Outermost pointer and move inner .. code-block:: c abc_ptr = malloc(sizeof(struct ABC)); abc_ptr->pqr_ptr = malloc(sizeof(struct PQR)); abc_ptr->pqr_ptr->mnp_ptr = malloc(sizeof(struct MNP *)); *abc_ptr->pqr_ptr->mnp_ptr = malloc(sizeof(struct MNP)); (**abc_ptr->pqr_ptr->mnp_ptr).ip = malloc(sizeof(int)); * Step 3 : Derefer 5 times to store user data as there are 5 pointers .. code-block:: c *(**abc_ptr->pqr_ptr->mnp_ptr).ip = 65; printf("*(**abc_ptr->pqr_ptr->mnp_ptr).ip = %d\n", *(**abc_ptr->pqr_ptr->mnp_ptr).ip); * Step 4 : Free from inner most .. code-block:: c free((**abc_ptr->pqr_ptr->mnp_ptr).ip); free(*abc_ptr->pqr_ptr->mnp_ptr); free(abc_ptr->pqr_ptr->mnp_ptr); free(abc_ptr->pqr_ptr); free(abc_ptr); * Step 5 : See full program below .. code-block:: c #include #include struct MNP { int *ip; }; struct PQR { struct MNP **mnp_ptr; }; struct ABC { struct PQR *pqr_ptr; }*abc_ptr; int main(void) { // Allocate 5 times. Since there are 5 pointers abc_ptr = malloc(sizeof(struct ABC)); abc_ptr->pqr_ptr = malloc(sizeof(struct PQR)); abc_ptr->pqr_ptr->mnp_ptr = malloc(sizeof(struct MNP *)); *abc_ptr->pqr_ptr->mnp_ptr = malloc(sizeof(struct MNP)); (**abc_ptr->pqr_ptr->mnp_ptr).ip = malloc(sizeof(int)); *(**abc_ptr->pqr_ptr->mnp_ptr).ip = 65; printf("*(**abc_ptr->pqr_ptr->mnp_ptr).ip = %d\n", *(**abc_ptr->pqr_ptr->mnp_ptr).ip); free((**abc_ptr->pqr_ptr->mnp_ptr).ip); free(*abc_ptr->pqr_ptr->mnp_ptr); free(abc_ptr->pqr_ptr->mnp_ptr); free(abc_ptr->pqr_ptr); free(abc_ptr); 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`