By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,902 Members | 4,532 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,902 IT Pros & Developers. It's quick & easy.

delete char * - why does it work

P: n/a
interesting case:

class MyString{
char * strRep; // initialized to char array

public:
~MyString(){delete strRep;} //why would this work
// just like 'delete [] strRep;'
};
isn't delete first calls the destructor for the object and then
deallocates the memory (by operator delete)?

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
puzzlecracker wrote:
interesting case:

class MyString{
char * strRep; // initialized to char array

public:
~MyString(){delete strRep;} //why would this work
// just like 'delete [] strRep;'


It doesn't if strRep was allocated with new[]. It does if strRep was
allocated with new.

char *c = new char[10];
delete c; // error

char *c = new char;
delete c; // ok

That's undefined behavior. On my system, mismatched new and delete
crashes almost everytime.
Jonatha

Jul 23 '05 #2

P: n/a
let's say it is allocated as char *c ="dummy";

IT WILL WORK!

Jul 23 '05 #3

P: n/a
puzzlecracker wrote:
let's say it is allocated as char *c ="dummy";

IT WILL WORK!


Please quote what you are answering to next time.

Depends on what you mean by 'it will work'. If you intend to crash your
system, it may or may not work. If you intend to run into undefined
behavior, it will work. If you intend to have a well-behaving program,
it won't. Deleting something you haven't allocated yourself is illegal.
Jonathan

Jul 23 '05 #4

P: n/a
"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
let's say it is allocated as char *c ="dummy";

IT WILL WORK!

It is undefined behaviour, looking as if it worked.
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #5

P: n/a
the behavior is undefined - but why does it work on all compilers?????

Jul 23 '05 #6

P: n/a
Hi

puzzlecracker wrote:
the behavior is undefined - but why does it work on all compilers?????


Come on... don't answer your own questions.
Markus

Jul 23 '05 #7

P: n/a
"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com
the behavior is undefined - but why does it work on all compilers?????


I doubt that you have tested all compilers.

You really need to get over this nonsense. Undefined means that anything can
happen. Included in the "anything" is that it might work as you expect.
There is no explanation in standard C++ as to why something with undefined
behaviour might "work". That all depends on the details of compiler
implementations etc. If you want to know, then go ask the authors of the
compiler.

If you write correct code, you are entitled to expect it to work. If you
write code that invokes undefined behaviour, you have no right to expect
anything. The fact that code with undefined behaviour sometimes works in the
way expected by the programmers who write it is neither new nor interesting
information. Programmers who rely on such "good fortune" are fools.

--
John Carson

Jul 23 '05 #8

P: n/a
"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
the behavior is undefined - but why does it work on all compilers?????

It does not. Proof? Try VS 7.1 on Win XP SP2 with:

class MyString
{
char* strRep;

public:
MyString (char* t) : strRep (t)
{
}

~MyString ()
{
delete strRep;
}
};

MyString s = "test";
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.