Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Whats the benefit in using malloc over the method of declaring a variable array for run time use

Consider the two codes below :

#include<stdio.h> 
void main() {
   int num; 
   scanf("%d",&num); 
   int arr[num]; 
   for(int i=0;i<num;i++){
      arr[i] = i+1;
   }
}
#include<stdio.h> 
#include<stdlib.h>
void main() {
   int num; 
   scanf("%d",&num); 
   int*p=(int*)malloc(num*size of(int));
   for(int i=0;i<num;i++){
      *(p+i) = i+1;
   }
}

Above two codes both allocates memory for array of size of num which is given by user and then initialise it . But first one is having the size of the array to be variable , so at compile time the compiler gives some memory space which is not(maybe) exactly num but maybe more than that but while at run time the num size gets allocated to it later on .

While for the case of malloc one there was no such problem of having a random memory space being allocated initially and hence that’s how we can say that malloc is better in terms of this feature and hence more suited for use ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Above two codes both allocates memory for array of size of num which is given by user and then initialise it

In the first case, arr is declared as a local variable, so it uses memory from the stack, and it exists only for the duration of the function in which it’s declared. If that function is main, then of course it will exist for the life of the program, but arrays declared this way in other functions with much shorter lifetimes will likewise have shorter lifetimes.

In the second case, the array is declared "dynamically" — it uses memory from the heap instead of the stack, and will continue to exist even after the function in which it was created exits. Because programs typically have much more heap space than stack space, using malloc can also be useful because it allows creation of larger arrays.

So, for example, it’s very common for a program to operate on data that’s read from a file, or perhaps from several files. Imagine writing a function to open a file, read the data into an array, and return the array to the caller. If that function declares the array as a local variable, it’ll go out of scope when the function exits, and you won’t be able to return it to the caller. Alternatively, you could have the caller create the array and pass it into the function that reads the data, but then the caller has to know ahead of time how much data is in the file, which isn’t always possible. Furthermore, the programmer might not know in advance how many files, and therefore how many arrays, might be read. All these problems go away if you instead create an array dynamically each time the file-reading function is called: you get a new array for each file, you can easily return the array to the caller, and the array can be sized to suit the amount of data in the file.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading