Solved

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

Posted on 2000-02-22
3
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

717 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