Link to home
Start Free TrialLog in
Avatar of Delion
Delion

asked on

Easy (I hope) Visual C++ question- Why NEW?

 Ok- hopefully, this is an easy question.  I'm trying to learn Visual C++ 6.0 from books (Couldn't find a local course on it that wasn't full) and I've got a few questions.  For now, though, here's one-
   Why use the NEW command when initializing variables and classes?  For example, why say "new int a;" instead of "int a;"?  
   I've consulted 3 books, and so far the only difference that they tell me is that I've got to use the delete command after I'm done with them or else I get a memory leak.  This seems more like a disadvantage than anything else, so what advantages does the NEW command provide?
-Delion
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

A 2nd advantage of dynamically allocated data is that it can be allocated in arrays whose sizes are determined at run-time.  For example, if you want to read a text file into a character array, you could declare the array like

char TextArray[1000];

but what if the file is more than 1000 characters long?  Well, you could declarre it to be 10000 characters long, but that might still not be enough.  What is more, if the file is short, say only 100 characters, then 10000 characters would be a terrible waste of space.  So you can use "new" to allcoate an array of just the right size once you learn the needed size at run-time.
Avatar of Delion

ASKER

Adjusted points to 30