+ 5
What is better for runtime of array size during runtime?
Which way is better an why(complete info) #include <iostream> using namespace std; int main(){ /*Using Dynamic memory allocation*/ int Dsize; int *arrnm; cin>>Dsize; arrnm = new int[Dsize]; /*Using a other way*/ int size; cin>>size; int arrname[size]; return 0; }
2 Answers
+ 6
The standard way to initialize an array whose size is known only during runtime, is dynamic memory allocation.
int* arr = new int[n];
Some compilers may not accept
int arr[n];
where n is a variable, since the expression by right requires a constant for size declaration.
https://stackoverflow.com/questions/30499228/c-array-size-must-be-an-const-expression
https://stackoverflow.com/questions/9219712/c-array-expression-must-have-a-constant-value
+ 5
Hatsy Rei Thanks