Click here to Skip to main content
15,884,592 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I want to take user input and make it the size of a dynamic array,
and i did this as you can see in code below.

the question is: I just wondring if this is right way to do it ?
i have tested and even did a memory leak test but everything was just fine.


Thank you :)

What I have tried:

int userinput = 0;

cout << "Enter array size: " << std::endl:
cin >> userinput;

//I have class named Runner.

Runner * arr = new Runner[userinput];


delete [] arr;
Posted
Updated 4-Jan-17 17:50pm

1 solution

Yep, looks good to me.
The only thing you need to be aware of using this technique - if the Runner objects have a constructor that requires a parameter, this cannot be done. That's when you either need to use a std::vector or an array of pointers to type Runner instead, and then initialise (and destroy) each element separately.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900