What is the difference between malloc()
-ed array and regular array when in both I have to specify memory size, for example
char* arr = malloc(50 * sizeof(char))
vs
int arr [50]
What is the difference between malloc()
-ed array and regular array when in both I have to specify memory size, for example
char* arr = malloc(50 * sizeof(char))
vs
int arr [50]
With the
malloc
the size you use can be a variable! That means the size could change depending on the change in the variable before execution reaches themalloc
statement. Specifying the size of a declared array otherwise must be constant.Note that you said you have said you use
malloc
to create an array. This is incorrect.malloc
merely allocates some contiguous memory and gives you a pointer to the start of that memory - technically this is not the some thing as an array (it can be used as if it was in quite a few circumstances, but not all circumstances)Well, there are too many differences. To start with, read about arrays are not pointers and vice versa.
That said, three major differences from the usability point of view (which I feel you're interested about)
An array has a scope limited to its enclosing block, but dynamically allocated memories live unless deallocated manually. So, arrays local to a function cannot be
retrun
ed but a pointer, returned viamalloc()
-ing , can be.For non-VLA case, array size must be a compile time constant but for
malloc()
size is specified at runtime. In other words, for arrays, you need to know the size at compile time whereas, formalloc()
-ing, it's perfectly possible to determine the requested size at runtime.Arrays cannot be re-sized. Once defined, they use all the memory required for their size. OTOH, a
malloc()
-ed pointer, pointing to some amount of memory, can very well berealloc()
-ed to some other amount of memory, as needed.Because in array size should be available at compile-time, while using pointer let you determine at run-time the size of it.
The chief difference is that dynamically-allocated memory can be resized as necessary; arrays (static, automatic, or variable-length) cannot be resized once they have been instantiated.
A secondary difference has to do with the array's lifetime. Arrays declared at file scope or with the
static
keyword have lifetimes that extend over the lifetime of the program. Arrays declared within a function or block without thestatic
keyword have lifetimes that are limited to the extent of their enclosing scope (which is why you can't return a pointer to a local array from a function - the array ceases to exist when the function exits).Dynamic memory falls somewhere in between - its lifetime extends from the initial
*alloc
call until thefree
call, which may be in different functions. You control its lifetime directly.Because of how memory is often managed for
auto
(local) variables, automatic and variable-length arrays often can't be arbitrarily large - attempting to create a local array that's more than a megabyte or so in size can lead to a runtime error on popular platforms. You typically don't have such limits on dynamically-allocated memory.From this link :
Dynamic memory allocation allows your program to obtain more memory space while running, or to release it if it's not required.
In simple terms, Dynamic memory allocation allows you to manually handle memory space for your program.
Here you can also read that in static allocation the required memory is allocated by the compiler and the exact size and type of storage must be known at compile time. On the other hand, in dynamic memory allocation, memory allocated "on the fly" during run time and the dynamically allocated space is usually placed in a program segment known as the heap or the free store.