Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > delete pointer

Reply
Thread Tools

delete pointer

 
 
Girish Shetty
Guest
Posts: n/a
 
      08-10-2005

"Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Girish Shetty wrote:
> >
> > > Is this well-defined behaviour, or an implementation detail?
> > >
> > > Does this mean this would also work?
> > >
> > > Cat *pearl = NULL;
> > > pearl->eat();

> >
> > Yeah.. even this will work.
> > As of my knowledge, its well defined behavior, because, the c code for

above
> > two lines of code will be something like this:
> >
> > Cat *pearl = NULL;
> > cat_eat(pearl) // See we are passing pearl pointer to the function
> > cat_eat.
> >
> > and the defination of cat_eat will be like this:
> >
> > void cat_eat(cat* this) {
> > cout << "\n Cat Eats.\n" ;
> > }
> >
> > And in this function, we are not using this pointer (cat* this). Its

very
> > obvious that, it will fail / CRASH, if in case u were using this pointer

to
> > access any of the member of that class, which u were not doing.

>
> Man.
> Don't guess but learn how the language works.
> You are walking on thin ice here. In fact all of the above has undefined
> behaviour. It may work or it may not work. Who knows, it is undefined.
>
> Read it from my lips, aeh, keystrokes:
> dereferencing an pointer with a 0 value is undefined. It doesn't matter
> that the compiler optimizes the dereference away under the hood. In the
> source code
>
> pearl->eat()
>
> you dereference a 0 pointer -> undefined behaviour. Period.
>
> --


When you are not going to use a pointer in your function, does it make any
difference whether you have something valid or NULL or what soever it
is????????

Regards
Girish


> Karl Heinz Buchegger
> http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-10-2005
Girish Shetty wrote:
>
>
> When you are not going to use a pointer in your function, does it make any
> difference whether you have something valid or NULL or what soever it
> is????????


It is completely irrelevant what the function does or what it does not.
It is the *call* that gives undefined behaviour.

You are not allowed to dereference a 0 pointer. Something you
undoubtly do when you do:

pearl->eat();

It doesn't matter that the compiler is able to optimize that in a way
such that the actual pointer value is not needed for making the call.
In the source code there is a dereference operation, and that gives
undefined behaviour if that pointer has a 0 value.

--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Earl Purple
Guest
Posts: n/a
 
      08-10-2005
It will probably work with most compilers as the compiler is unlikely
to do dereference the pointer at all - in fact the function is likely
to be inlined.
Not advisable to code in this manner though.

 
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
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
various way to delete pointer to pointer morz C++ 7 02-06-2006 08:19 AM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments