Reputation: 459
Consider these codes in C:
int n;
scanf("\n%d ",&n);
int arr[n];
and this.
int n;
scanf("\n%d ",&n);
int *p = (int *)malloc(n*sizeof(int));
Why do we need dynamic memory allocation for this in the first place?
and is the first code not Dynamic Memory Allocation as the array is created during runtime?
When should I use malloc()?
Upvotes: 1
Views: 935
Reputation: 60058
int n;
scanf("\n%d ",&n);
int arr[n];
where the n
is a runtime-dynamic value (as it is in this case) was not supported in older versions of C, which required that the n
be an integer constant expression.
Modern versions of C have lifted this limitation, allowing dynamically sized arrays (variable length arrays, or VLAs), but unlike the old fixed-sized arrays, VLAs have restrictions.
They aren't allowed to be jumped across (with break
/continue
/goto
), they might not be be async-signal-safe, they will crash your program if they get too large, and they will often generate larger code than plain old local arrays.
Also, because they're local, you can't return a pointer to them to your caller.
The size limitations is one of their biggest restrictions. VLAs are carved out of the call stack, which is typically only a couple of kilo/mega-bytes large, and you get a typically undiagnosed crash if you exceed this limit.
malloc
'd memory doesn't have such a limit, malloc
failures are clearly reported via a returned NULL
, and because malloc
'd memory is global to the process, you can pass a pointer to it back to your caller.
`
Upvotes: 1
Reputation: 8945
Just to be sure you understand:
When "functions call functions," their local variables are stored in an area of memory known as "the stack," so named because it works like a push-down LIFO stack. When a function is entered, the stack grows. When the function returns, it shrinks. And if a function recursively calls itself, there's no confusion because each instance has its own area on the stack.
malloc()
allocates memory "upon request" from a different area called "the heap," so named because it has no particular built-in organization. These areas of memory are referenced indirectly, using "pointers," which are variables whose value is understood to be a memory address.
The "C" language conveniently allows you to use "array notation" when using pointers, just as it does with local variables that are allocated on the stack.
Upvotes: 0
Reputation: 164699
int arr[n];
allocates memory on the stack. It is automatically allocated and then deallocated at the end of the block. A pointer to stack memory cannot be safely returned from a function.
Consider this.
#include <stdio.h>
char *make_array() {
int n;
scanf("%d",&n);
// Memory for `arr` is allocated on the stack.
char arr[n];
// And then deallocated when the function exits.
// A good compiler and editor will warn you if you try
// to return it.
return arr;
}
int main() {
// This pointer is to memory which has already been freed. Other things
// will overwrite it.
char *arr = make_array();
arr[0] = 'c';
arr[1] = '\0';
// This could be the letter c, but it's probably gibberish.
puts(arr);
}
If you need memory to live beyond the life of the current function, you need to allocate it on the heap with malloc
and manage it yourself.
Upvotes: 2
Reputation: 740
int n;
scanf("\n%d ",&n);
int arr[n];
If it works then that means you are using compiler that still interprets it as "DMA" the only difference here would be that it will get deallocated at the end of the scope but I don't think it's a good practice for C.
Upvotes: 0