Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When we declare an array, we need to reserve some memory to store the elements o

ID: 3537214 • Letter: W

Question

When we declare an array, we need to reserve some memory to store the elements of this array. This memory allocation and it is static. That is when we declare an array, we specify the number of elements in that array and a fixed memory is allocated. Once declared the size of the array cannot be changed.

But there are situations in which we may want to declare the variable and not allocate memory for it until we use that variable. We may also want to free the memory allocated for a variable after its use is over.

The dynamic allocation of memory during the program execution is achieved through two built in functions malloc or calloc, realloc and free. There is also sizeof()function used to determine the number of bytes occupied by an entity in memory.

For example:

Dynamic_memory.c

#include<math.h>

#include<stdio.h>

#define l 4

main()

{

             int *a, i,*p;

            float *b,*c;

            a=(int*) malloc(l);

            for (i=0; i<l; i++)

           {

                        *(a+i)=i;

}

            for (i=0; i<l; i++)

            {

                         printf ("a %d %d %u ",i,*(a+i),(a+i));

            }

             p=(int*) calloc(l,4);

             for (i=0; i<l; i++)

         {

*(p+i)=i*2;

}

            for (i=0; i<l; i++)

            {

                        printf(" p %d %d %u %d ",i,*(p+i),(p+i),sizeof(p));

            }       

b=(float*)malloc(l);

for(i=0;i<l+2;i++)

          {

*(b+i)=i*4.0;

}

            for(i=0;i<l+2;i++)

            {

                          printf(" b %d %f %u %d ", i,*(b+i),(b+i),sizeof(b));

            }

c= calloc(l,8);

for(i=0;i<l;i++)

           {

*(c+i)=i*4.0;

}

for(i=0;i<l+1;i++)

{

                         printf(" c %d %f %u %d ", i,*(c+i),(c+i),sizeof(c));

}

            for(i=l+1;i<l+2;i++)

{

             printf(" b %d %f %u ", i,*(b+i),(b+i));

}

}

Explanation / Answer

In the program above we create an integer pointer *a and two floating point pointers *b,*c. Now we want to store a string of numbers using these pointers, instead of using a fixed size array.

To achieve this we fist have to allocate enough space in the memory to which the pointers point to. We will allocate just enough memory to store the elements we want to . For example in the above program we want to store 4 integer values. By using the malloc function we will allocate memory for an integer array of size 4. Note the usage of malloc () here. We invoke it by the statement a=(int*) malloc(l); We could also do the same with p=(int*) malloc(l*sizeof(int)); or p=(int*) malloc(l*size(a)); where l=4. One important thing to note here is that since malloc returns a void pointer, we need to type cast it before equating it to the pointer we want. In the case above a is an integer so we have to use (int *) before malloc. Similarly we see the use of a malloc to allocate memory for a float pointer b . We use the pointer p to demonstrate that we can do exactly the same using the function calloc. The function is exactly the same , except that we need not type cast it now and also that we have to give a second argument to calloc,which is the number of bytes need to store one variable. In most of the modern compiler this value is ignored by the function.

So far we have seen operations which is pretty much like that in the fixed size arrays. The only difference is that we assign the memory only just before the variable is used. This by itself is not very useful unless we can change this memory allocation , that is increase it if we want more and remove it if we don't want to use the variable again in the program. This is what realloc and free will do for us. Let us look at that with an example.

realloc.c

/*     Dynamic memory allocation realloc and free */

        #include<math.h>

        #include<stdio.h>

        #define l 5

        main()

        {

        float *a;

        int i;

        a=(float*) malloc(l);

        for(i=0;i<l;i++)

            { *(a+i)=i*3.0;}

        for(i=0;i<l;i++)

        {

         printf("a %d %f %u ",

                   i,*(a+i),(a+i));

        }

       a=(float*)realloc(a,sizeof(float)*(l+3));

        for(i=0;i<l+3;i++)

            { *(a+i)=i*3.0;}

        for(i=0;i<l+3;i++)

        {

         printf("a %d %f %u ",

                   i,*(a+i),(a+i));

        }

        free(a);

        }

In the above program we have a pointer a which is of type float . We then give it enough momory space to store 5 elements. After filling and printing out the address of those locations and the value stored in them we increase the"size" of the memory allocated by the statement "a=(float*)realloc(a,sizeof(float)*(l+3));". This statement will increase the memory space to store 3 more floating points. You can see by yourself that if you remove this line and try to store more than5 elements into a you will get segmentation fault. The last statement in the program "free(a);" free all the space allotted to a .

Thus by using malloc, realloc and free we can change the memory taken up by the program as required by it. This is particularly useful when we have to use large temporary arrays in a calculation.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote