Question:
C / C++ - What are the point of dynamically allocated arrays?
sam
2015-02-06 20:05:37 UTC
I've read everywhere that static arrays reserve memory at compile time, while dynamically allocated are created at run time.

I dont understand what this implies though...

I can do:

int n = 5
int* array = (int*)malloc(n*sizeof(int));

or I can do:
int n = 5
int array[5];

They do the same thing don't they?
Three answers:
justme
2015-02-07 05:27:14 UTC
In your first example:

int n = 5

int* array = (int*)malloc(n*sizeof(int));



array is allocated and stays allocated until you free it. You can also resize it by using realloc if need be.



You could also use:

int* array = new int[n];



BUT as far as I know you can not resize it. (I could be wrong but I always use malloc so...)



In your second example:

int n = 5

int array[5];



the variable n is useless and has no bearing on the size of the array. You could have set n to 100000 and still the array would be 5.



This array is reserved a compile time and will only be available while it is in scope. It can not be resized since its a fixed length.



The point of dynamically allocated arrays is so you can create an array that its size is unknown at compile time. Say you need an array of ints. You don't know how many because you are reading them from a file that could have 1 or 10 million (or more). You dynamically allocate an array for say 1000. You find there are more in the file so you reallocate the array for another 1000, and continue until all ints are read from the file.



This is a real world scenario and an array declared at compile time will not work unless you make it incredibly large at compile time. The problem is it would waste memory if there is only a few ints in the file. Not to mention, it would be on the stack which may not be able to handle that much since the stack is limited in size.



Dynamically allocated memory is from the memory pool and is quite large.
husoski
2015-02-06 20:53:36 UTC
First, there's almost no point in using malloc() in a C++ program. You can use:



int* array = new int[n];



...which is easier to type, and easier to read. Also, the array is an array of class-type objects, new will ensure that constructors are called for every array member, and the corresponding delete[] will call destructors. The malloc()/calloc() and free() functions won't do that.



The thing you can't do in C++ (yet?) is use a non-constant expression for the dimension of an array. So, you can't calculuate the size of the array, based on arguments or input values, and allocate just the right array size for your problem.



You need dynamic memory to do that in C++. (C added variable length arrays in 1999, but C++ never adopted that change.)



Dynamic memory also allows you to return a variable-sized result from a function. There are better ways to do this than with pointers and arrays, but think about how you would handle the following problem with what you know now:



Write a function that takes a string of comma-separated decimal numbers, and returns the median value of that list as an int. (i.e. the middle value in the list after sorting.)



You'll need an int array to sort the values, but you don't know how big to make that array until you count the commas in the input string. This is a case where a dynamic array will be useful, only allocating enough memory and never too much.
Ratchetr
2015-02-06 21:02:18 UTC
In both your examples, you have 5 hard coded.

What happens if you need 6? Or 10? Or 10000?



Do you want to have to change, recompile and test your code every time you need to go from 5 to 6? Or 6 to 10? or 10 to 10000?



What if the size of the array is determined by some value the user enters, or that you read from a file? Do you want to make the size of the array 10000? Even if the user enters 2? Or you read 2 from a file?



In the bad old days, we would just try to make the array bigger than anyone could possibly need. But that was a horrible waste of memory, and there was always some yahoo out there that needed an array just a bit bigger than we guessed you could ever possibly need.



Today, arrays are a bit outdated for this sort of problem. You want to use some sort of collection class, like a Vector or List that just grows as you need it to. Then you don't have to guess about the maximum size of an array. Memory is allocated as needed.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...