how does this work?

C

Chris

say I have an class like this:

----------------------------------------
class grid {
int** GRID;
public:
grid() {
GRID = new int* [34];
for (int i=0; i<34; i++)
GRID = new int [34];
}
~grid() {
for (int i=0; i<34; i++)
delete [] GRID;
delete [] GRID;
}
};
----------------------------------------
is it safe to do this?

----------------------------------------
grid* cube = new grid [34];
delete [] cube;
----------------------------------------

I guess my question is, when you de-allocate something, does it do what
the destructor says to do?

I know somebody is going to tell me to use vectors because they're
safer. While I agree with you, know that this question is only to help
me gain a deeper understanding of how it works.

Thanks for any help!
 
S

samit

Ya cris i don't see a problem with this... in C++ the compiler adds
code calling the deconstructor whenever your object is going to be
deleted.
And that i guess would be whenever you explicity delete an object on
the heap or its deleted when it is out of scope.
Hope that answers your question :)
 
B

Bart

Chris wrote:
----------------------------------------
grid* cube = new grid [34];
delete [] cube;
----------------------------------------

I guess my question is, when you de-allocate something, does it do what
the destructor says to do?

I know somebody is going to tell me to use vectors because they're
safer. While I agree with you, know that this question is only to help
me gain a deeper understanding of how it works.

Printing some text in each function is a good way to see what your
program is doing.

To answer your question, yes. Just as 'new' invokes the constructor for
every object created, 'delete' will invoke the destructor for every
object destroyed. Here you have an array of 34 items so the constructor
will be invoked 34 times, and then the destructor will be invoked 34
times.

Regards,
Bart.
 
F

Frederick Gotham

Only use ALL CAPS for the names of macros; I have changed "GRID" to
"pp_grid". I would also suggest a capital letter for the names of types and
functions.

Chris posted:
class Grid {
int **ppgrid;

public:
Grid() {
ppgrid = new int*[34];


Here, "ppgrid" is assigned the address of a newly allocated array of 34
non-const pointers to non-const.

for (int i=0; i<34; i++)
ppgrid = new int [34];
}



Apart from the lone trailing brace, everything is okay here.

~Grid() {
for (int i=0; i<34; i++)
delete [] ppgrid;
delete [] ppgrid;
}
};



This is OK too.



Yes, it's perfectly OK.

I guess my question is, when you de-allocate something, does it do what
the destructor says to do?


Yes. Operator delete calls the objects' destructors.

I know somebody is going to tell me to use vectors because they're
safer. While I agree with you, know that this question is only to help
me gain a deeper understanding of how it works.


Where possible, I find it's better to ask questions over on comp.lang.c
(unless the question pertains to features of C++) because you receive a lot
less noise along the lines of "use std::string instead".
 
M

Marcus Kwok

Frederick Gotham said:
Chris posted:
class Grid {
int **ppgrid;

public:
Grid() {
ppgrid = new int*[34]; [...]

for (int i=0; i<34; i++)
ppgrid = new int [34];
}



Apart from the lone trailing brace, everything is okay here.


The closing brace actually matches the opening brace for the
constructor, but it is not aligned in the conventional way.
 
P

Puppet_Sock

Frederick Gotham wrote:
[snip]
Where possible, I find it's better to ask questions over on comp.lang.c
(unless the question pertains to features of C++) because you receive a lot
less noise along the lines of "use std::string instead".

I would estimate, though I'm ready to be told I'm wrong, that "new"
and "delete" pertain to features of C++.
Socks
 
B

Bart

Puppet_Sock said:
Frederick Gotham wrote:
[snip]
Where possible, I find it's better to ask questions over on comp.lang.c
(unless the question pertains to features of C++) because you receive a lot
less noise along the lines of "use std::string instead".

I would estimate, though I'm ready to be told I'm wrong, that "new"
and "delete" pertain to features of C++.

They do, and the specific problem here was about the invocation of
destructors on the elements of an array. It has absolutely nothing to
do with C and everything to do with C++.

Regards,
Bart.
 
B

Bart

Frederick said:
Where possible, I find it's better to ask questions over on comp.lang.c
(unless the question pertains to features of C++) because you receive a lot
less noise along the lines of "use std::string instead".

Besides the fact that this was definitely a C++ question I find your
comment about "noise" rather bizarre. There's a reason for such
comments. A programming language is not just a bunch of features thrown
together that you can choose from at random to fit your needs (although
critics would say C++ is just that). There are some style guidelines
and prefered idioms justified by how the language is intended to be
used. Those style guidelines exist in C and other languages as well.

It exasperates me when I see programmers who still use C++ like a
super-C, ignoring most of the standard library and language features.
People have no problem using all the abstraction and libraries when
writing Java but when they switch to C++ they seem to suffer some kind
of brain damage.

Anyway, sorry for the rant.

Regards,
Bart.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top