Connecting Tech Pros Worldwide Forums | Help | Site Map

delete char * - why does it work

puzzlecracker
Guest
 
Posts: n/a
#1: Jul 23 '05
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)?


Jonathan Mcdougall
Guest
 
Posts: n/a
#2: Jul 23 '05

re: delete char * - why does it work


puzzlecracker wrote:[color=blue]
> interesting case:
>
> class MyString{
> char * strRep; // initialized to char array
>
> public:
> ~MyString(){delete strRep;} //why would this work
> // just like 'delete [] strRep;'[/color]

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

puzzlecracker
Guest
 
Posts: n/a
#3: Jul 23 '05

re: delete char * - why does it work


let's say it is allocated as char *c ="dummy";

IT WILL WORK!

Jonathan Mcdougall
Guest
 
Posts: n/a
#4: Jul 23 '05

re: delete char * - why does it work


puzzlecracker wrote:[color=blue]
> let's say it is allocated as char *c ="dummy";
>
> IT WILL WORK![/color]

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

Jakob Bieling
Guest
 
Posts: n/a
#5: Jul 23 '05

re: delete char * - why does it work


"puzzlecracker" <ironsel2000@gmail.com> wrote in message
news:1121539923.514908.228300@g43g2000cwa.googlegr oups.com...[color=blue]
> let's say it is allocated as char *c ="dummy";
>
> IT WILL WORK![/color]


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

(reply address in rot13, unscramble first)


puzzlecracker
Guest
 
Posts: n/a
#6: Jul 23 '05

re: delete char * - why does it work


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

Markus Moll
Guest
 
Posts: n/a
#7: Jul 23 '05

re: delete char * - why does it work


Hi

puzzlecracker wrote:
[color=blue]
> the behavior is undefined - but why does it work on all compilers?????[/color]

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


Markus

John Carson
Guest
 
Posts: n/a
#8: Jul 23 '05

re: delete char * - why does it work


"puzzlecracker" <ironsel2000@gmail.com> wrote in message
news:1121550231.108466.186350@o13g2000cwo.googlegr oups.com[color=blue]
> the behavior is undefined - but why does it work on all compilers?????[/color]

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

Jakob Bieling
Guest
 
Posts: n/a
#9: Jul 23 '05

re: delete char * - why does it work


"puzzlecracker" <ironsel2000@gmail.com> wrote in message
news:1121550231.108466.186350@o13g2000cwo.googlegr oups.com...[color=blue]
> the behavior is undefined - but why does it work on all compilers?????[/color]


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)


Closed Thread