473,508 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about delete/new

I work with many dynamically allocated variables in
my program including double **, int *, char *.

For ex:

double **d;
d = new (double *) [10];
for(int i = 0; i < 10; i++) {
d[i] = new double[10];
}

to setup a 2d array of doubles.

My memory clear function looks like this:

clearDoubleArray(double **d, int rows)
{
for(int i = 0; i < rows; i++) {
delete [] d[i];
}

delete [] d;

d = NULL;
}

In my program, the same variable is allocated and deallocated a great deal
of times.
Sometimes I'm not sure whether d has been initialized, or has been deleted.
Is it safe
to call clearDoubleArray() on a var that has not been allocated? Or should I
keep track?
Just for the sake of argument, assume that whenever d has not been
allocated, that rows = 0,
so it does not use the delete[] d[i] loop shown above.

Will delete[] d cause problems?

Also, I try to set d to NULL on the last statement of the function, but it
does not change
where d is pointing to. How can I fix that?

Thanks
Shaun
Jul 22 '05 #1
4 1285
"pedicini" <sc********@nospam.yahoo.com> wrote...
I work with many dynamically allocated variables in
my program including double **, int *, char *.

For ex:

double **d;
d = new (double *) [10];
for(int i = 0; i < 10; i++) {
d[i] = new double[10];
}

to setup a 2d array of doubles.

My memory clear function looks like this:

clearDoubleArray(double **d, int rows)
No return type?
{
for(int i = 0; i < rows; i++) {
delete [] d[i];
}

delete [] d;

d = NULL;
}

In my program, the same variable is allocated and deallocated a great deal
of times.
Sometimes I'm not sure whether d has been initialized, or has been
deleted.
Is it safe
to call clearDoubleArray() on a var that has not been allocated? Or should
I
keep track?
(a) You should keep track at least to know what 'rows' to pass in.
(b) If 'd' is NULL, delete or delete[] will be OK, but an attempt to
dereference it to get d[i] is going to fail.
(c) Every time you deallocate I recommend setting it to NULL.
Just for the sake of argument, assume that whenever d has not been
allocated, that rows = 0,
so it does not use the delete[] d[i] loop shown above.

Will delete[] d cause problems?
Not if 'd' is NULL. If it's _uninitialised_, it contains garbage.
An attempt to 'delete[]' will cause undefined behaviour.

Also, I try to set d to NULL on the last statement of the function, but it
does not change
where d is pointing to. How can I fix that?


Pass 'd' by reference:

void clearDoubleArray(double ** &d, int rows)
...

Victor
Jul 22 '05 #2

"pedicini" <sc********@nospam.yahoo.com> wrote in message
news:ck**********@cronkite.cc.uga.edu...
I work with many dynamically allocated variables in
my program including double **, int *, char *.


try let the STL do the work for you:

typedef std::vector< double > tDoubleVector;
typedef std::vector< tDoubleVector > t2D_DoubleVector;

typedef std::auto_ptr< int > tIntPtr;

etc.........
-c
Jul 22 '05 #3
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:0k0bd.117331$He1.23104@attbi_s01...
"pedicini" <sc********@nospam.yahoo.com> wrote...
I work with many dynamically allocated variables in
my program including double **, int *, char *.

For ex:

double **d;
d = new (double *) [10];
for(int i = 0; i < 10; i++) {
d[i] = new double[10];
}

to setup a 2d array of doubles.

My memory clear function looks like this:

clearDoubleArray(double **d, int rows)
No return type?


forgot, void clearDoubleArray
{
for(int i = 0; i < rows; i++) {
delete [] d[i];
}

delete [] d;

d = NULL;
}

In my program, the same variable is allocated and deallocated a great deal of times.
Sometimes I'm not sure whether d has been initialized, or has been
deleted.
Is it safe
to call clearDoubleArray() on a var that has not been allocated? Or should I
keep track?
(a) You should keep track at least to know what 'rows' to pass in.
(b) If 'd' is NULL, delete or delete[] will be OK, but an attempt to
dereference it to get d[i] is going to fail.
(c) Every time you deallocate I recommend setting it to NULL.
Just for the sake of argument, assume that whenever d has not been
allocated, that rows = 0,
so it does not use the delete[] d[i] loop shown above.

Will delete[] d cause problems?


Not if 'd' is NULL. If it's _uninitialised_, it contains garbage.
An attempt to 'delete[]' will cause undefined behaviour.


so if I had:

double **d;
delete [] d;

This would cause undefined behavior? is that right?
But this is okay:
double **d = NULL;
delete [] d;

Thanks for your help Victor,
Shaun


Also, I try to set d to NULL on the last statement of the function, but it does not change
where d is pointing to. How can I fix that?


Pass 'd' by reference:

void clearDoubleArray(double ** &d, int rows)
...

Victor

Jul 22 '05 #4
pedicini wrote:
[...]
so if I had:

double **d;
delete [] d;

This would cause undefined behavior? is that right?
Yes.
But this is okay:
double **d = NULL;
delete [] d;


Yes.

V
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5527
by: J | last post by:
Dear Advanced users, Here are parts of a code sample and I need to find out a few things from this. Please tell me your input. I am supposed to get others help as well to make the answers as...
6
3957
by: Jeff Williams | last post by:
Ok, everyone loves to talk about dynamic arrays and ptr's etc, they provide endless conversation :) so here goes: Will this leak memory (my intuition says yes): void foo(vector<int*>& vec) {...
4
1293
by: WertmanTheMad | last post by:
Ok here goes, another odd SQL Question ....as always.. How do I get ... The value of a paramater passed to say a Trigger OR The SQL Itself Like this , say I have a Trigger set on delete, ...
20
6525
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
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...
5
588
by: WaterWalk | last post by:
Hello. The question about "deleting this inside the class's member function" is discussed many times in this group and in the "C++ FAQs". But I still have two more questions. 1. Take the...
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;
0
1487
by: topmind | last post by:
siddharthkh...@hotmail.com wrote: Good luck. The behavior and conventions of web versus fat-client (or paper-oriented) reports are so different that making a generic anything that serves both of...
0
1133
by: =?Utf-8?B?SmVhbi1GcmFuY29pcyBCcmV0b24=?= | last post by:
"siddharthkhare@hotmail.com" wrote: The context is important in this kind of design concern : I assume there's a lot of user and that application will evolve to add richer functionality. My...
10
3550
by: JohnO | last post by:
Hi All, This question is related to iSeries V5R4 and db2. I want to implement an AFTER DELETE trigger to save the deleted rows to an archive table, I initially defined it as a FOR EACH...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
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
7401
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...
1
7063
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5640
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,...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
432
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.