Malloc Exercise
struct ABC {
        int **ip;
}*sp;
See Answer
Step 1 : List all possible pointers from outermost to innermost
Pointer
spspis a single pointeripipis a double pointer*ip*ipis a single pointerStep 2 : Allocate from Outermost pointer and move inner
// 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
**sp->ip = 65;
Step 4 : Free from inner most
free(*sp->ip);
free(sp->ip);
free(sp);
Step 5 : See full program below
#include <stdio.h>
#include <stdlib.h>
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;
}
struct ABC {
        int ***ip;
}*sp;
See Answer
Step 1 : List all possible pointers from outermost to innermost
Pointer
spspis a single pointeripipis a triple pointer*ip*ipis a double pointer**ip**ipis a single pointerStep 2 : Allocate from Outermost pointer and move inner
// "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
***sp->ip = 65;
printf("***sp->ip = %d\n", ***sp->ip);
Step 4 : Free from inner most
free(**sp->ip);
free(*sp->ip);
free(sp->ip);
free(sp);
Step 5 : See full program below
#include <stdio.h>
#include <stdlib.h>
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;
}
struct ABC {
        int **ip;
}**sp;
See Answer
Step 1 : List all possible pointers from outermost to innermost
Pointer
spspis a double pointer*sp*spis a single pointeripipis a double pointer*ip*ipis a single pointerStep 2 : Allocate from Outermost pointer and move inner
// "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
**(**sp).ip = 65;
printf("**(**sp).ip = %d\n", **(**sp).ip);
Step 4 : Free from inner most
free(*(**sp).ip);
free((**sp).ip);
free(*sp);
free(sp);
Step 5 : See full program below
#include <stdio.h>
#include <stdlib.h>
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;
}
struct ABC {
        int ***ip;
}***sp;
See Answer
Step 1 : List all possible pointers from outermost to innermost
Pointer
spspis a triple pointer*sp*spis a double pointer**sp**spis a single pointeripipis a triple pointer*ip*ipis a double pointer**ip**ipis a single pointerStep 2 : Allocate from Outermost pointer and move inner
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
***(***sp).ip = 65;
printf("***(***sp).ip = %d\n", ***(***sp).ip);
Step 4 : Free from inner most
free(**(***sp).ip);
free(*(***sp).ip);
free((***sp).ip);
free(**sp);
free(*sp);
free(sp);
Step 5 : See full program below
#include <stdio.h>
#include <stdlib.h>
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;
}
struct MNP {
        int *ip;
};
struct PQR {
        struct MNP **mnp_ptr;
};
struct ABC {
        struct PQR *pqr_ptr;
}*abc_ptr;
See Answer
Step 1 : List all possible pointers from outermost to innermost
Pointer
abc_ptrabc_ptris a single pointerpqr_ptrpqr_ptris a single pointermnp_ptrmnp_ptris a double pointer*mnp_ptr*mnp_ptris a single pointeripipis a single pointerStep 2 : Allocate from Outermost pointer and move inner
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
*(**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
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
#include <stdio.h>
#include <stdlib.h>
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;
}
Current Module
Previous Module
Next Module
Other Modules