473,506 Members | 9,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new and delete question

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
6 1872
* 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
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
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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2421
by: Matan Nassau | last post by:
Hello. i have a composite which i want to delete. this is a composite which represents a boolean expression (see a previous post of mine with more details at...
16
3844
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
0
2245
by: Suzanne | last post by:
I'd like to know how can I put up a confirmation question when the user tries to delete a row in the datagrid by clicking on the row header and pressing the Delete key? I have found this code on...
1
8547
by: Barry L. Camp | last post by:
Hi all, Wondering if someone can help with a nagging problem I am having using a GridView and an ObjectDataSource. I have a simple situation where I am trying to delete a row from a table, but...
12
3348
by: yufufi | last post by:
Hello, How does delete know how much memory to deallocate from the given pointer? AFAIK this informations is put there by new. new puts the size of the allocated memory before the just before...
10
2072
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a...
15
1767
by: tom | last post by:
why delete the dynamically allocated memory twice causes an error, see the code below: int _tmain(int argc, _TCHAR* argv) { int *pi = new int(12); cout<<*pi; delete pi; delete pi; }
29
2222
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I remembered delete is implemented through operator overloading, but I am not quite clear. Could anyone recommend some links about how delete is implemented so that I can...
7
1800
by: subramanian100in | last post by:
The following discussion is for learning purpose only. 1) Suppose for a type T, I have T *ptr = new T; To free ptr, suppose I use ( I deliberately omit in delete.) delete ptr;
18
3634
by: aj | last post by:
I have the following snippet of code. On some platforms, the delete calls works, on Linux, it core dumps (memory dump) at the delete call. Am I responsible for deleting the memory that...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.