Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > delete char * - why does it work

Reply
Thread Tools

delete char * - why does it work

 
 
puzzlecracker
Guest
Posts: n/a
 
      07-16-2005
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)?

 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      07-16-2005
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

 
Reply With Quote
 
 
 
 
puzzlecracker
Guest
Posts: n/a
 
      07-16-2005
let's say it is allocated as char *c ="dummy";

IT WILL WORK!

 
Reply With Quote
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      07-16-2005
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

 
Reply With Quote
 
Jakob Bieling
Guest
Posts: n/a
 
      07-16-2005
"puzzlecracker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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)


 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      07-16-2005
the behavior is undefined - but why does it work on all compilers?????

 
Reply With Quote
 
Markus Moll
Guest
Posts: n/a
 
      07-16-2005
Hi

puzzlecracker wrote:

> the behavior is undefined - but why does it work on all compilers?????


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


Markus

 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      07-16-2005
"puzzlecracker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

 
Reply With Quote
 
Jakob Bieling
Guest
Posts: n/a
 
      07-17-2005
"puzzlecracker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 03-09-2011 11:00 PM
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 02-11-2011 05:00 AM
Why does "delete" not delete? D. Susman C++ 11 03-11-2008 10:03 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Can someone tell me why I can't delete this file? and why it blue screens WinXP Pro on delete? zZz Computer Support 1 01-12-2005 02:37 AM



Advertisments