1

Why do we need dynamic memory allocation despite we can use variable-length arrays instead?

We can allocate dynamic memory at run-time with a variable length array as:

unsigned int x;
printf("Enter size:");
scanf("%d",&x);
int arr[x];

Also we can allocate memory at run-time by using one of dynamic memory allocation functions as:

unsigned int x,*p;
printf("Enter size:");
scanf("%d",&x);
p = (unsigned int *)malloc(x);

So, In both case we can allocate memory during run-time.So, Why do we need dynamic memory allocation despite we can use variable-length arrays instead? And what benefits we can get when using one of dynamic memory allocation functions than arrays?

Edit code for passing array:

unsigned int *func(unsigned int *p)
{
   *p=10; // any processing on array

   return p;  // we could return a pointer to an array

}

int main()
{
  unsigned int x,*P;
  printf("Enter size:");
  scanf("%d",&x);
  unsigned int arr[x];

  p = func(arr);
}
Islam Abdeen
  • 539
  • 3
  • 10

3 Answers3

3

Because it has different use cases.

You use automatic memory when you allocate it in a calling routine and you can pass that memory to callees at will. But you will never be able to return it to a caller.

Imagine a library that would use opaque structures to store data - a kind of OOP in C. The API is likely to have routines with this signature:

void * buildEltOfTypeXX(param1, param2, ...);

to create and initialize the opaque structures. In that case, it is evident that the memory must use dynamic allocation (malloc) and not automatic because you would return a dangling pointer. And with that pattern, the caller will get ownership of the memory and will have to free it when it no longer needs it.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • @ Serge We can also return address of array to caller.Actually we don't pass an array, we copy address of the first element. look at the edited code i wrote in my post.So still no difference – Islam Abdeen May 20 '16 at 17:31
  • @IslamAbdeen If you return the address of a local array to the caller, the results are undefined, because once a function returns, its local variables are all gone. – Steve Summit Jun 28 '21 at 00:28
1

In Variable length array (VLA), when you use int arr[x], even if x is calculated dynamically arr will be stored in stack, which has limited space, this makes it unsafe. Because there are chance you run out of space. So if you know the size you use static array, otherwise you write unsafe code.

And in this condition we need Dynamic memory allocation where memory is stored in heap, 'heap' (large pool of memory) usually refers to the memory that is managed by malloc (C) and new (C++) for dynamic memory allocation.

Otherwise, VLA is easier to use, like in simple coding competition/questions, where you have limited size testcases, if it works then its fine otherwise use dynamic allocation, I won't recommend using it in some high level programming( Operating Systems, Browsers, Libraries, Graphics, Banking Applications) unless you are sure it won't cause problem. Also it may be not be compatible with some compiler.

May be this question will also be helpful.

Suryansh Singh
  • 1,123
  • 7
  • 15
0

Because int arr[x], when x is a variable value like in your example, isn't a valid standard C (or C++) code.

At least: this is true (If I'm not wrong) with al standardized version of C++ (C++98, C++03, C++11, C++14) and with the first C stantardization (C89).

Variable length array are allowed in C99 but the following (and current) C11 standard made they an optional feature.

So you can rely in variable length (non allocated) array only if you are using a C99 compliant compiler. Never in C++.

p.s.: sorry for my bad English.

max66
  • 65,235
  • 10
  • 71
  • 111
  • @ Serge Ballesta - well, it's complicated; the question wasn't tagged "C"; I've edited it adding only the "C" tag, and not the "C++" one, because there was place for only another tag and Islam use `malloc()` and not `new`; but, IMHO, the question deserves an answer that cover both languages. – max66 May 19 '16 at 15:14
  • @ Serge Ballesta - it's OK ;-) . Reading your answer I understand that, most likely, Islam had doubts about the usefulness of the dynamically allocated memory. So I think that your answer is better than mine. But I think also that it would appropriate to explain to him also that variable length array aren't correct standard C/C++ (with the exception of C99 and, optionally, C11). Request: coul'd you edit your answer to, briefly, add this aspect? – max66 May 19 '16 at 15:30
  • I think you should leave that answer here. It is a nice complement to mine (even if written before) reminding that VLA is not a reliable concept, even in C – Serge Ballesta May 19 '16 at 15:40