Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Pointers and polymorphism explained [preview, PDF, part of my attempted "Correct C++ Tutorial"]

Reply
Thread Tools

Re: Pointers and polymorphism explained [preview, PDF, part of my attempted "Correct C++ Tutorial"]

 
 
Robert Macy
Guest
Posts: n/a
 
      10-26-2005
Alf,

I don't think I can find errors for you, but read it I will.

Small note, the "green" color for highlighting on my screen is almost
no highlight whatsoever. Looks like the adjacent letters, unless I
really, really look at it.

Good subject, pointers.

- Robert -

 
Reply With Quote
 
 
 
 
Robert Macy
Guest
Posts: n/a
 
      10-26-2005
Alf,

The program called bad_pointer02.cpp on page 4 [page 6 of pdf] returns
the proper value, 43. It did not seem to yield undefined behaviour.

I use MSVC line command running on XP.

I can't see where the program is wrong. The function is called, x is
created, the pointer is set to the location, and then return...oops. I
get it. But the program still returned the proper value. hmm... Bet
this happens everytime until you count on it.

- Robert -

 
Reply With Quote
 
 
 
 
Mirek Fidler
Guest
Posts: n/a
 
      10-26-2005
Robert Macy wrote:
> Alf,
>
> The program called bad_pointer02.cpp on page 4 [page 6 of pdf] returns
> the proper value, 43. It did not seem to yield undefined behaviour.


Actully, that is the tricky part of undefined behaviour. It often works
on one platform and fails on another.

Mirek
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-27-2005
* Robert Macy:
>
> Small note, the "green" color for highlighting on my screen is almost
> no highlight whatsoever. Looks like the adjacent letters, unless I
> really, really look at it.


Yes, that was intentionally. The idea is to make it clear, as one reads
the text, that this word or term is a new one, defined by the context.
Without drawing the eyes towards that word or term, and without making
it seem like a good idea to just skim those words or terms.

Originally they weren't even boldface.

But then I changed my mind: without boldface they appeared with _less_
contrast than the rest of the text...

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-27-2005
* Robert Macy:
> Alf,
>
> The program called bad_pointer02.cpp on page 4 [page 6 of pdf] returns
> the proper value, 43. It did not seem to yield undefined behaviour.
>
> I use MSVC line command running on XP.
>
> I can't see where the program is wrong. The function is called, x is
> created, the pointer is set to the location, and then return...oops. I
> get it. But the program still returned the proper value. hmm... Bet
> this happens everytime until you count on it.


Thanks for that comment, it's appreciated.

It just so happens that with the compiler options etc. you're using,
there's nothing that changes the contents of the chunk of memory that
the local variable occupied, between the time the pointer is obtained,
and the time the pointer is used -- Undefined Behavior is allowed to
do anything, including what someone might expect it to do, or not.

One simple way to trash that memory (overwrite it with something else),
is to call some function or, even just evaluating a suitable expression.

E.g., I think something like

std::cout << "Oops!" << std::endl;

after the call to wrongAnswer(), but before displaying the contents of
*p, would do the trick.

I'll probably put in a statement like the "Oops!", to make it even more
clear that this program is really really Undefined Behavior.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Robert Macy
Guest
Posts: n/a
 
      10-27-2005
Alf P. Steinbach wrote:
> E.g., I think something like
>
> std::cout << "Oops!" << std::endl;
>
> after the call to wrongAnswer(), but before displaying the contents of
> *p, would do the trick.


Didn't mess up either. So I was going to try some other things, where
I needed another pointer, like...
int* p, q;
p = wrongAnswer(); // this line generates a compile error
q = p;
std::cout << *q << std::endl;

compiler says : error C2440: cannot convert from 'int' to 'int *'
Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast

didn't like this line either
*p = wrongAnswer();

as expected, this works...
int* q;
int* p = wrongAnswer();
but the first failure makes no sense to me.

ARRRRGGGG!!!!!

instead of
int* p, q; //which looks like it should work to me

must do it this way...
int* p;
int* q;

THEN THE PROGRAM COMPILES. But with a warning !

Always gave a warning, just didn't notice until now.
warning C4172: returning address of local variable or temporary

Why is it not possible to use one line to declare pointers?
int* p, q; // just not allowed?

- Robert -

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-27-2005
* Robert Macy:
> Alf P. Steinbach wrote:
> > E.g., I think something like
> >
> > std::cout << "Oops!" << std::endl;
> >
> > after the call to wrongAnswer(), but before displaying the contents of
> > *p, would do the trick.

>
> Didn't mess up either.


It did when I tried it.


[snip]
> Why is it not possible to use one line to declare pointers?
> int* p, q; // just not allowed?


There is an example & discussion at the start of section 1.1.1 (top of
page 2).

You can use a typedef of the pointer type.

But generally it's not a good idea to do that for the purpose of
declaring more than one pointer at once: instead, declare each pointer
in its own declaration.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Sigurd Stenersen
Guest
Posts: n/a
 
      10-27-2005
Robert Macy wrote:
> Why is it not possible to use one line to declare pointers?
> int* p, q; // just not allowed?


It IS possible. You just need to realize that the * belongs to the
variable, not the type. Hence,
int *p, *q;

Which (mostly) is a good thing, as it allows you to declare type variables
and type pointer variables in the same statement. E.g.
int i, j, *p, *q;
declares two ints and two int pointers.


--

Sigurd
http://utvikling.com


 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      10-27-2005

Robert Macy wrote in message
<(E-Mail Removed) .com>...
>Alf P. Steinbach wrote:
>> E.g., I think something like
>>
>> std::cout << "Oops!" << std::endl;
>>
>> after the call to wrongAnswer(), but before displaying the contents of
>> *p, would do the trick.

>
>Didn't mess up either. So I was going to try some other things, where
>I needed another pointer, like...
> int* p, q;
> p = wrongAnswer(); // this line generates a compile error
> q = p;


[ Let's temporarily assume 'wrongAnswer()' did return a valid
pointer-to-int.]
Ouch!! You got bit by the ol' declaration-on-the-same-line trick.

q = *p; // q is an 'int', NOT a pointer.

That's why I like to 'stick the star onto the var'.
int *p, *q;
And you'll often see experts scream, "one declaration per line.".
int *p;
int *q;

>Why is it not possible to use one line to declare pointers?
> int* p, q; // just not allowed?
>
> - Robert -


It is, BUT, the pointer does not bind to the 'int', but to the var ('p' in
this example). Mr. Eckel, in TiCpp v1 Chap 3, states it like:
(you can say that “* binds more tightly to the identifier”).

That help? (I'm trying *not* to step on Mr. Steinbach's star(pun intended) ,
it's his show. (...and I consider him the 'mentor' and me 'D dummy' <G>) ).
--
Bob R
POVrookie


 
Reply With Quote
 
Robert W Hand
Guest
Posts: n/a
 
      10-28-2005
On Thu, 27 Oct 2005 21:30:01 +0200, "Sigurd Stenersen"
<(E-Mail Removed)> wrote:

> int *p, *q;
>
>Which (mostly) is a good thing, as it allows you to declare type variables
>and type pointer variables in the same statement. E.g.
> int i, j, *p, *q;
>declares two ints and two int pointers.


You are correct that it is allowed. But it can lead to errors either
for the unwary or the overworked and tired. Note that Robert Macy,
who started this thread, made this type of error. I believe that it
is a style issue. I would recommend, in general, having one
declaration per line.
--

Best wishes,

Bob
 
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
Dynamic polymorphism vs. Static polymorphism Krivenok Dmitry C++ 13 06-01-2006 09:49 AM
v2 Pointers and polymorphism explained [preview, PDF, part of my attempted "Correct C++ Tutorial"] Alf P. Steinbach C++ 4 11-28-2005 11:42 AM
Re: Pointers and polymorphism explained [preview, PDF, part of myattempted "Correct C++ Tutorial"] James Dennett C++ 15 11-15-2005 07:14 PM
Re: Pointers and polymorphism explained [preview, PDF, part of my attempted "Correct C++ Tutorial"] Chris ( Val ) C++ 11 11-05-2005 04:54 PM
Re: Pointers and polymorphism explained [preview, PDF, part of my attempted "Correct C++ Tutorial"] Robert Macy C++ 6 10-27-2005 05:46 PM



Advertisments