Solved

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

Posted on 2000-02-22
3
182 Views
Last Modified: 2010-04-02
 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
0
Comment
Question by:Delion
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 30 total points
ID: 2548089
new creates data "dynamically" instead of "locally".  local data is contained in a scope, usually the scopy of a function  When that function ends the data is destroyed.  But dynamic data exists until it is destroyed (with delete).  So you can use new to allocate data within a function and have that data continue to exist after the function ends.  There are many times when this sort of thing is needed.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2548099
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.
0
 

Author Comment

by:Delion
ID: 2552406
Adjusted points to 30
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question