By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,173 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,173 IT Pros & Developers. It's quick & easy.

new and delete question

P: n/a
Hi,

I am new to C++, and I have a simple question but I can't seem to find
a direct answer.

If I use new to create something in the scope of a function and then I
return the pointer that I get to another function, do I need to worry
about the memory space being used by anything else and causing the
program to crash?

In short, I want to make sure that:
new reserves memory and that memory will remain intact until I
explicitly call delete, even if the scope has changed.

Also, does the destructor of an object automatically get called at any
point or do I have to manually call it?

Thanks

Feb 10 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* md*****@gmail.com:
>
I am new to C++, and I have a simple question but I can't seem to find
a direct answer.

If I use new to create something in the scope of a function and then I
return the pointer that I get to another function, do I need to worry
about the memory space being used by anything else and causing the
program to crash?
Yes, but only due to bugs in your program. ;-)

A (raw) pointer is very low-level kind of object in C++. It has no
automatic behavior. Nothing happens when a pointer variable goes out of
scope.

If you have copied the pointer value somewhere, and that value refers to
dynamically allocated memory, you still have access to that memory.

To avoid pointer bugs, simply don't use raw pointers directly.

Use smart pointers such as std::auto_ptr and boost::shared_ptr.

In short, I want to make sure that:
new reserves memory and that memory will remain intact until I
explicitly call delete, even if the scope has changed.
See above.

Also, does the destructor of an object automatically get called at any
point or do I have to manually call it?
Normally you don't call destructors manually. An object's destructor is
automatically called when you 'delete' something you 'new'ed, or for an
automatic (local) variable, when execution leaves the defining scope.
Note that for an automatic (local) raw pointer variable, nothing
happens: the variable itself has no destructor, no automatic behavior,
even if the pointed to object has.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 10 '07 #2

P: n/a
md*****@gmail.com wrote:
Hi,

I am new to C++, and I have a simple question but I can't seem to find
a direct answer.

If I use new to create something in the scope of a function and then I
return the pointer that I get to another function, do I need to worry
about the memory space being used by anything else and causing the
program to crash?
No. Memory allocated by new will remain valid until deallocated by delete.
In short, I want to make sure that:
new reserves memory and that memory will remain intact until I
explicitly call delete, even if the scope has changed.
That will happen.
Also, does the destructor of an object automatically get called at any
point or do I have to manually call it?

For a non-dynamic object (not created by new), the destructor will
automatically invoked when the object goes out of scope. For an object
allocated with new, the destructor will be invoked when you delete it.

There are only a couple of edge cases where you need to manually call a
destructor, and since you're a newbie, you should not hit those
(placement new).
Feb 10 '07 #3

P: n/a
md*****@gmail.com wrote:
Hi,

I am new to C++, and I have a simple question but I can't seem to find
a direct answer.

If I use new to create something in the scope of a function and then I
return the pointer that I get to another function, do I need to worry
about the memory space being used by anything else and causing the
program to crash?
No.
In short, I want to make sure that:
new reserves memory and that memory will remain intact until I
explicitly call delete, even if the scope has changed.
The pointee will not go away without an explicit delete.

Also, does the destructor of an object automatically get called at any
point or do I have to manually call it?
Destructors of object are called automatically if the objects go out of
scope. Also, when you do

delete p;

the destructor for the pointee *p will be called automatically. Note that in
a scope like

{
T* p;
p = new T (...);
...
delete p;
}

this does not involve calling any destructor twice: delete p; calls the
destructor for type T on the object *p whereas the pointer p going out of
scope calls the destructor for type T* on the object p (this destructor is
a null-op, as far as I know).
BTW: are you sure you want to use raw pointers? They are a pain and hard to
use correctly (mostly because it is very difficult to match every new with
one and only one delete along _each_ possible path of flow control: think
of how exceptions might divert the path of execution to some unknown target
location!).
Best

Kai-Uwe Bux
Feb 10 '07 #4

P: n/a

md*****@gmail.com wrote:
>
If I use new to create something in the scope of a function and then I
return the pointer that I get to another function, do I need to worry
about the memory space being used by anything else and causing the
program to crash?
You need to learn about basic memory types in C/C++ programs. Whithout it it
will be hard for you to program. Any free book about C++ in the inet can
tell you about. It is not newsgroup topic, for has no C++ specific
limitations.

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 11 '07 #5

P: n/a

Grizlyk wrote:
>
It is not newsgroup topic, for has no C++ specific limitations.
I want to say, that you can not learn C++ from newsgroup, you need large,
more based and detailed explanations, that can be found only in books or
manuals.
--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/

Feb 11 '07 #6

P: n/a
On 10 Feb, 16:03, mdin...@gmail.com wrote:
Hi,

I am new to C++, and I have a simple question but I can't seem to find
a direct answer.
What you are asking might seem simple, but as soon as you scratch the
surface you will realise it is anything but. You are asking about an
advanced part of C++.
If I use new to create something in the scope of a function and then I
return the pointer that I get to another function, do I need to worry
about the memory space being used by anything else and causing the
program to crash?
No, you can be sure that the object will continue to exist until you
delete it. That is the problem. If you use new to create something in
the scope of a function and then you return the pointer that you get
to another function, you do not need to worry about the memory space
being used by anything else and causing the program to crash? But
that's the easy part. What you do need to worry about is the hard
part:

You need to be certain that in all current and likely future uses of
your function, the new'ed pointer is deleted no less than and no more
than once.
You need to be certain that any and all possibilities for copying,
modifying, passing the pointer do not violate the above point.
You need to be certain that the above two points hold, both throughout
the normal flow of execution, and in the presence of exceptions.
In short, I want to make sure that:
new reserves memory and that memory will remain intact until I
explicitly call delete, even if the scope has changed.
That is something you can be sure of. However, it should not be what
you are trying to ensure. Not calling delete until you have finished
with the object is easy. Ensuring that delete is still called, exactly
once, regardless of the path taken through the code (including the
path taken if an exception is thrown at any point) is much harder. You
should be trying to ensure that ownership semantics are clear and
unambiguous, and are not likely to introduce bugs as code is modified.

As someone new to C++, you shouldn't be worrying about manually
managing ownership. You should be using classes that manage that for
you. Read about smart pointers and the RAII concept.
Also, does the destructor of an object automatically get called at any
point or do I have to manually call it?
For objects with automatic or static storage duration (roughly, local
and global variables respectively), your compiler will take care of
calling destructors when required. For objects with dynamic storage
duration (i.e. objects allocated with new or new[]) nothing happens
automatically. You, and you alone, are responsible for ensuring that
each and every new is paired with one and only one delete, and that
each and every new[] is paired with one and only one delete[].

The need for that certainty and the difficulty in achieving it is the
reason why things like smart pointers were invented.

Gavin Deane

Feb 11 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.