473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how does this work?

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[i] = new int [34];
}
~grid() {
for (int i=0; i<34; i++)
delete [] GRID[i];
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!

Sep 7 '06 #1
8 1639
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 :-)

Sep 7 '06 #2
On 6 Sep 2006 22:41:02 -0700 in comp.lang.c++, "Chris"
<ch*********@gmail.comwrote,
>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?
If it doesn't, your compiler is junk.

Sep 7 '06 #3
Chris wrote:
<snip>
----------------------------------------
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.

Sep 7 '06 #4

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[i] = new int [34];
}

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

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

This is OK too.

----------------------------------------
is it safe to do this?

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

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".

--

Frederick Gotham
Sep 8 '06 #5
Frederick Gotham <fg*******@spam.comwrote:
Chris posted:
>class Grid {
int **ppgrid;

public:
Grid() {
ppgrid = new int*[34];
[...]
>
> for (int i=0; i<34; i++)
ppgrid[i] = 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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 8 '06 #6
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

Sep 8 '06 #7
Puppet_Sock wrote:
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.

Sep 8 '06 #8
Frederick Gotham wrote:
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.

Sep 8 '06 #9

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

Similar topics

7
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
3
by: Julian | last post by:
Hi I am trying to update a date field in my table but some how this simple code does not work, I know the select work because if I write the fields, it will show the data from the table but why...
5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
22
by: Robert Bralic | last post by:
CAN anybody tell me any address where I can download some small(1000-2000) lines C++ proghram source. Or send me ,a small(1000-2000) lines C++ program source that I can compille with gpp under...
12
by: Frank Hauptlorenz | last post by:
Hello Out there! I have a DB2 V7.2 Database (Fix11) on Win 2000 Professional. It was before a NT 4 based Domain - now it is a Win 2000 Domain. The database server is a domain member. Now...
0
by: Jarod_24 | last post by:
How does tabindex work in ASP .net pages I dosen't seem to work quite like in regular forms. and there isn't any TabStop property either. 1 .How do you prevent a control form beign "tabbed"....
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
14
by: webEater | last post by:
I have a problem, it's not browser specific, and I don't get a solution. I have an (X)HTML document, I show you a part of it: .... <!--<div class="pad">--> <div id="eventImages"><img src=""...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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
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
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,...

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.