[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2000-02-22
3
Medium Priority
?
199 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 120 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

649 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