Thread: Pointer pointing to itself

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, they don't. But in C, you can bet that a lot of them mean that. Which is quite annoying, really. Things that cause undefined behavior should be errors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Jul 2010
    Posts
    33
    Is this particular case undefined behaviour though? I don't see that it is, the way the compiler deals with it seems logical, to me at least ( but I don't think that will count for anything ). Implicit type coercion happens all the time when dealing with void pointers, are we to say that all operations on void pointers will produce undefined behaviour?

  3. #18
    Registered User
    Join Date
    Jul 2010
    Posts
    33
    Quote Originally Posted by Bayint Naung View Post
    Btw, not all warnings mean 'undefined behaviour'.
    This is what OP is doing exactly(idea).
    Except it has(should has?) defined behaviour?
    Code:
    void *p = NULL;
    p = &p;
    if( p == *(void**)p ) {
      printf("WOW\n");
    }
    In a way, yes but obviously you wouldn't be performing these operations with void pointers

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CSaw View Post
    Is this particular case undefined behaviour though? I don't see that it is, the way the compiler deals with it seems logical, to me at least ( but I don't think that will count for anything ). Implicit type coercion happens all the time when dealing with void pointers, are we to say that all operations on void pointers will produce undefined behaviour?
    Operations on void* pointers are undefined behavior. You must convert them to a proper type before doing any operations to them.
    And the fact that you are converting a pointer to a non-pointer by storing a pointer to pointer in a pointer also causes undefined behavior.
    Fine example: On x64 Windows, sizeof(int) = 4, sizeof(int*) = 8. Hence, you truncate the address when converted to an int. Therefore undefined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Looking at the C standard:
    Quote Originally Posted by C99 Section 6.3.2.3 Paragraph 7
    A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. If the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined. Otherwise, when converted back again, the result shall compare equal to the original pointer. When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object.
    Consequently, it seems that this line might not result in undefined behaviour, but it also might:
    Code:
    int *numPtr =(&numPtr);
    Quote Originally Posted by C99 Section 6.5.9 Equality operators, Paragraph 2
    One of the following shall hold:
    — both operands have arithmetic type;
    — both operands are pointers to qualified or unqualified versions of compatible types;
    — one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void; or
    — one operand is a pointer and the other is a null pointer constant.
    The above list of constraints is exhaustive, so it is clear that this results in undefined behaviour:
    Code:
    if(numPtr == *numPtr)
    Assuming that converting from void* to void** leads to a correctly aligned pointer, Bayint Naung's example looks well defined, since void* is compatible with void*.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Jul 2010
    Posts
    33
    Thanks for clearing that up laserlight. I've never read the standard before, I've been told it isn't a fun experience...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM