A pointer problem from a high school programming contest

This is a discussion on A pointer problem from a high school programming contest within the C++ Programming forums, part of the General Programming Boards category; I'm trying to follow this discussion, but I'm having a little difficulty understanding what it is that is "allowed" or ...

  1. #31
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I'm trying to follow this discussion, but I'm having a little difficulty understanding what it is that is "allowed" or "not allowed" by the standard. Are we saying that the following code could actually cause a meltdown?

    Code:
    int *p = (int *)rand();
    Interesting.

    As long as p isn't dereferenced, what would be the logic in disallowing a pointer that points to something invalid as long as it is never used (ie. dereferenced)?

  2. #32
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by laserlight View Post
    That's correct. None of the four functions (main() excluded) has any net effect, assuming that this invalid pointer thing also has no net effect.
    Some compilers will even optimize out things that have no effect, so they may not ever even get called.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #33
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quoting from TC++PL, page 557:
    A reverse_iterator is implemented using an iterator called current. That iterator can (only) point to the elements of its sequence plus its one-past-the-end element. However, the reverse_iterator's one-past-the-end element is the original sequence's (inaccessible) one-before-the-beginning element. Thus, to avoid access violations, current points to the element after the one the reverse_iterator refers to. This implies that * returns the value *(current-1) and that ++ is implemented using -- on current.

  4. #34
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by MacGyver View Post
    As long as p isn't dereferenced, what would be the logic in disallowing a pointer that points to something invalid as long as it is never used (ie. dereferenced)?
    There is no logic in it. It seems like the designers of the C and C++ standards wanted to forge a language that could conceivably run anywhere, including architectures which are deliberately obtusely designed. I refuse to submit to that kind of stupidity.

  5. #35
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by brewbuck View Post
    There is no logic in it. It seems like the designers of the C and C++ standards wanted to forge a language that could conceivably run anywhere, including architectures which are deliberately obtusely designed. I refuse to submit to that kind of stupidity.
    It seems quite silly, especially considering the fact that no memory accesses are actually performed prior to dereferencing a pointer. How would this be enforced? Would the O/S or hardware actually keep records of all pointers inside a program handy in a table? LOL. That would seem to be such a waste of resources that it makes me sick to even think of it.

    You're right when you said that such a system would catch a memory violation when it would occur. That's how a system should be protected imo. I don't understand the motivation to increase the protection anymore.

    Anyway, does anyone know of a system that actually performs such checking?

  6. #36
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > Anyway, does anyone know of a system that actually performs such checking?
    The link I gave earlier

    http://groups.google.com/group/comp....18f6cccd56427d

    mentions some architectures where this might happen, though I don't understand most of that part of the discussion, not being a hardware expert.

  7. #37
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by MacGyver View Post
    Anyway, does anyone know of a system that actually performs such checking?
    If someone can name one (and virtual machines DON'T count) I'll give them $20, if it's verifiable.

  8. #38
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by James Dow Allen
    CBFalconer wrote:
    > /* Making fatal hidden assumptions */
    > /* Paul Hsiehs version of strlen.


    I haven't read your entire treatise, and I surely wouldn't want to
    defend
    Mr. Hsieh (whose code, he's led us to understand, was written by his
    dog)
    but I do have some comments.


    > #define SW (sizeof (int) / sizeof (char))


    The fact that sizeof(char)==1 doesn't make this line bad *if* the
    programmer feels that viewing SW as chars per int is important.
    I don't defend this particular silliness, but using an expression for
    a simple constant is often good if it makes the constant
    self-documenting.


    > Some sneaky hidden assumptions here:
    > 1. p = s - 1 is valid. Not guaranteed. Careless coding.


    Mr. Hsieh immediately does p++ and his code will be correct if then
    p == s. I don't question Chuck's argument, or whether the C standard
    allows the C compiler to trash the hard disk when it sees p=s-1,
    but I'm sincerely curious whether anyone knows of an *actual*
    environment
    where p == s will ever be false after (p = s-1; p++).


    Many of us fell in love with C because, in practice, it is so much
    simpler
    and more deterministic than many languages. Many of the discussions in
    comp.lang.c seem like they'd be better in a new newsgroup:
    comp.lang.i'd_rather_be_a_lawyer


    :-) :-)


    James Dow Allen
    Thanks for the link.

    ROFL, I love this post.

  9. #39
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by MacGyver View Post
    Thanks for the link.

    ROFL, I love this post.
    Paul Hsieh, by the way, is the author of pstdint.h, a reasonably portable version (as portable as such a thing can be) of the C99 header stdint.h, for platforms which don't have it.

  10. #40
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Another interesting tidbit is that pointer subtraction is done using the type ptrdiff_t, which is NOT guaranteed by the standard to be able to hold the result (!). In fact, it's only guaranteed by the Standard to have a range of between plus and minus 65-thousand something. If the result doesn't fit in a ptrdiff_t, the behavior is undefined. Fortunately, most code involving pointer subtraction can be easily modified to avoid it.

    http://www.thescripts.com/forum/thread460624.html

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 11:42 AM
  4. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 03:50 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21