473,231 Members | 1,375 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,231 software developers and data experts.

Question on delete [] vs just plain delete

Hi,

I am new to c++. I recently spend an enormous among of time
troubleshooting a seeminly innocuous piece of code. Although I narrow
down this piece of code as the culprit but I don't understand why. Can
some guru help to enlighten me? Thank you.

// I created an array of pointers to object pointers:

Object ** obs = new Object * [9];

// After that I populate this array:

for(int i=0; i<9; i++){
obs[i] = new Object(i);
}

// After using them i deleted the array:

for(int i=0; i<9; i++){
delete obs[i];
}

delete [] obs;

delete obs; // this is the problem, during execution the program
hanged.

Apparently, the above caused undefined behaviour. But I don't know why
i shouldn't do that. Thanks again for any sharing!

Cheers,
Damon

Jul 23 '05 #1
11 1752
"DamonChong" <so********@excite.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
Hi,

I am new to c++. I recently spend an enormous among of time
troubleshooting a seeminly innocuous piece of code. Although I narrow
down this piece of code as the culprit but I don't understand why. Can
some guru help to enlighten me? Thank you.
While the gurus answer more difficult questions, I'll field this one.

// I created an array of pointers to object pointers:

Object ** obs = new Object * [9];
/////////// Great, you've allocated an array of pointers to Object

// After that I populate this array:

for(int i=0; i<9; i++){
obs[i] = new Object(i);
/////////// And now you've allocated the objects. }

// After using them i deleted the array:

for(int i=0; i<9; i++){
delete obs[i];
/////////// Now you've deallocated the objects.
}

delete [] obs;
/////////// And now you've deallocated the array.
/////////// Good. So you've deallocated everything you've allocated. Nothing
left to deallocate.

/////////// and yet....
delete obs; // this is the problem, during execution the program
hanged.


/////////// So the previous line hangs since you're deallocating something
which you shouldn't

Jul 23 '05 #2
Hi Damon

Efrat already answered your post, but I will recommend you do not use
new/delete as a newbie C++ programmer. Instead focus on the standard library
and just don't use pointers.
Pointers are difficult and better not used until you understand the basics
of C++ - including the standard library. By going this route, you will also
learn how little use there actually is of pointers in modern C++.

/Peter
"DamonChong" <so********@excite.com> skrev i en meddelelse
news:11*********************@c13g2000cwb.googlegro ups.com...
Hi,

I am new to c++. I recently spend an enormous among of time
troubleshooting a seeminly innocuous piece of code. Although I narrow
down this piece of code as the culprit but I don't understand why. Can
some guru help to enlighten me? Thank you.

// I created an array of pointers to object pointers:

Object ** obs = new Object * [9];

// After that I populate this array:

for(int i=0; i<9; i++){
obs[i] = new Object(i);
}

// After using them i deleted the array:

for(int i=0; i<9; i++){
delete obs[i];
}

delete [] obs;

delete obs; // this is the problem, during execution the program
hanged.

Apparently, the above caused undefined behaviour. But I don't know why
i shouldn't do that. Thanks again for any sharing!

Cheers,
Damon

Jul 23 '05 #3
ajk
On 29 Jan 2005 01:39:32 -0800, "DamonChong" <so********@excite.com>
wrote:
delete [] obs;

delete obs; // this is the problem, during execution the program

normally you use delete [] on any array you allocate to tell the
compiler that the ptr you are deleting is an array.

it is just one of those not obvious c++ rules you need to learn :)

in any case I would suggest you use vector<> instead of array since it
is safer and more convenient. check up the online help/book for info.
STL in general can help you a lot.

/ajk
--
"Those are my principles. If you don't like them I have others."
Groucho Marx.
Jul 23 '05 #4
Thanks alot. Kind of silly I suppose to make such mistake. ;P

Jul 23 '05 #5
"DamonChong" <so********@excite.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Thanks alot. Kind of silly I suppose to make such mistake. ;P


No problem. BTW, explicit memory management is both very powerful, and
very prone to making "silly" mistakes (which I don't think are silly at
all). Consequently, perhaps you might want to read Peter Koch Larsen's
response (with which I completely agree).

Specifically, you could write your code this way:

#include <vector>

std::vector<Object> obs[9];

for(int i = 0; i < 9; ++i)
obs[i] = Object(i);

Or if Object doesn't have a default constructor,

#include <vector>
#include <boost/shared_ptr.hpp>

std::vector<boost::shared_ptr<Object> > obs(9);

for(int i = 0; i < 9; ++i)
obs[i].reset(new Object(i));

(You could google for boost smart_ptr for the above).
Jul 23 '05 #6
Jon
While I would agree that pointers are maybe not obvious for a beginner to
say that there is "little use" of pointers in modern c++ is just plain wrong
in my opinion. Of course it maybe depends on what you define as "modern c++"
but that is another question...

"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:%j********************@news000.worldonline.dk ...
Hi Damon

Efrat already answered your post, but I will recommend you do not use
new/delete as a newbie C++ programmer. Instead focus on the standard
library and just don't use pointers.
Pointers are difficult and better not used until you understand the basics
of C++ - including the standard library. By going this route, you will
also learn how little use there actually is of pointers in modern C++.

/Peter
"DamonChong" <so********@excite.com> skrev i en meddelelse
news:11*********************@c13g2000cwb.googlegro ups.com...
Hi,

I am new to c++. I recently spend an enormous among of time
troubleshooting a seeminly innocuous piece of code. Although I narrow
down this piece of code as the culprit but I don't understand why. Can
some guru help to enlighten me? Thank you.

// I created an array of pointers to object pointers:

Object ** obs = new Object * [9];

// After that I populate this array:

for(int i=0; i<9; i++){
obs[i] = new Object(i);
}

// After using them i deleted the array:

for(int i=0; i<9; i++){
delete obs[i];
}

delete [] obs;

delete obs; // this is the problem, during execution the program
hanged.

Apparently, the above caused undefined behaviour. But I don't know why
i shouldn't do that. Thanks again for any sharing!

Cheers,
Damon


Jul 23 '05 #7

My question is why dynamically allocate an array of Object pointers. Why not
just just

Object *obparray[9];

??

Is memory that sparse now days?

"DamonChong" <so********@excite.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
Hi,

I am new to c++. I recently spend an enormous among of time
troubleshooting a seeminly innocuous piece of code. Although I narrow
down this piece of code as the culprit but I don't understand why. Can
some guru help to enlighten me? Thank you.

// I created an array of pointers to object pointers:

Object ** obs = new Object * [9];

// After that I populate this array:

for(int i=0; i<9; i++){
obs[i] = new Object(i);
}

// After using them i deleted the array:

for(int i=0; i<9; i++){
delete obs[i];
}

delete [] obs;

delete obs; // this is the problem, during execution the program
hanged.

Apparently, the above caused undefined behaviour. But I don't know why
i shouldn't do that. Thanks again for any sharing!

Cheers,
Damon

Jul 23 '05 #8
"Bradley" <bn*****@kc.rr.com> wrote in message
news:fl*********************@twister.rdc-kc.rr.com...

My question is why dynamically allocate an array of Object pointers. Why not just just

Object *obparray[9];

??

Is memory that sparse now days?


It's possible that Object doesn't have a default constructor.
Jul 23 '05 #9

"Efrat Regev" <ef*********@yahoo.com> wrote in message
news:xO********************@adelphia.com...
"Bradley" <bn*****@kc.rr.com> wrote in message
news:fl*********************@twister.rdc-kc.rr.com...

My question is why dynamically allocate an array of Object pointers. Why

not
just just

Object *obparray[9];

??

Is memory that sparse now days?


It's possible that Object doesn't have a default constructor.


Oops! my bad - I misunderstood your post.
Jul 23 '05 #10
Efrat Regev wrote:
Specifically, you could write your code this way:

#include <vector>

std::vector<Object> obs[9];

for(int i = 0; i < 9; ++i)
obs[i] = Object(i);

Or if Object doesn't have a default constructor,

#include <vector>
#include <boost/shared_ptr.hpp>

std::vector<boost::shared_ptr<Object> > obs(9);

for(int i = 0; i < 9; ++i)
obs[i].reset(new Object(i));

(You could google for boost smart_ptr for the above).


You don't need to go to those lengths; this would do,
if Object is copyable:

.. std::vector<Object> obs;
.. for(int i = 0; i != 9; ++i)
.. obs.push_back( Object(i) );

Jul 23 '05 #11

Efrat Regev wrote:
Specifically, you could write your code this way:

#include <vector>

std::vector<Object> obs[9];

for(int i = 0; i < 9; ++i)
obs[i] = Object(i);


Actually, you mean
std::vector<Object> obs(9);

which is one vector with initially 9 elements, and not
9 vectors with initially no elements.

With your [9] declaration, the compiler will complain that it
can't assign Object(0) to an empty vector of Objects.

You could also be a bit clearer, and write the size only once:

#include <vector>

std::vector<Object> obs; // empty

for(int i = 0; i < 9; ++i)
obs.pushback( Object(i) ); // add one element at a time.

Now, there's only one occurence of 9 - if the vector should ever
change size, you can do so in one place.

HTH,
Michiel Salters

Jul 23 '05 #12

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

Similar topics

4
by: Shea Martin | last post by:
Which of the following do I use delete instead of just delete. //1.) // not sure about this one, as char is of size 1 char *str = new char; //2.) //not sure about this one, as it is a...
3
by: lallous | last post by:
Hello Consider this: // allocate a buffer, and cast to MYSTRUCT MYSTRUCT *p = (MYSTRUCT *) new char; // free the buffer // 1. can I free directly as:
7
by: MSG Servicos de Informatica | last post by:
Hi MSDN Team! I have a doubt about the "MailMessage" class in Framework .NET 1.1. My SMTP server works in a local network that no have access to the internet for security reasons. And in my...
12
by: dennist685 | last post by:
Can't edit, delete or add row in an Access database in a website 2003 When I implement a walkthrough using Northwind I have no trouble doing this. Also, in a windowsforms project I have no...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
9
by: CGW | last post by:
I asked the question yesterday, but know better how to ask it, today: I'm trying to use the File.Copy method to copy a file from a client to server (.Net web app under IIS ). It looks to me that...
7
by: AB | last post by:
Hi all, A thought crossed my mind.... if I allocate memory for an array at runtime using.... int* arr = new int ; what happens when I then de-allocate memory using
25
by: eggie5 | last post by:
I have a form where a user can change his password, but I'm confused on how to prevent this from being transmitted in plain text. Well, I know how not to transmit it in plain text - use any type...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.