问题
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]
回答1:
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.
回答2:
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 the static
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 the free
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.
回答3:
Because in array size should be available at compile-time, while using pointer let you determine at run-time the size of it.
回答4:
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.
回答5:
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 the malloc
statement. Specifying the size of a declared array otherwise must be constant.
int foo = 5;
char bar[foo]; // This will cause a compile error
char bar[5]; // size is fixed, array size specifiers MUST be constant in C
void some_func (void)
{
// do some work that might change foo, e.g. get a value from user
char* arr = malloc(foo * sizeof(char)); //foo is variable, thus the size is variable!
}
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)
来源:https://stackoverflow.com/questions/42581505/what-is-the-difference-between-malloc-array-and-regular-array-when-in-both-i-hav