What is better for runtime of array size during runtime? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; }

3rd Jul 2018, 3:34 PM
Shahil Ahmed
Shahil Ahmed - avatar
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
3rd Jul 2018, 3:43 PM
Hatsy Rei
Hatsy Rei - avatar
+ 5
Hatsy Rei Thanks
3rd Jul 2018, 4:16 PM
Shahil Ahmed
Shahil Ahmed - avatar