Solved

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

Posted on 2000-02-22
3
177 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now