Thread: Is my C++ compiler broken?

  1. #16
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    What I don't understand is that if NULL, a constant pointer expression (be it what it may) is explicit-cast to integer, is that not my "final word". All the dangers of foot-shooting aside, if I tell the compiler I want to convert this constant pointer expression (an integral-like type) to an integer, should that not essentially "shut it up". Basically, wouldn't the expression:
    (int)ANY_POINTER_TYPE become of type integer?

    I mean I can see the warning from an implicit cast (from argument to parameter type), however an explicit cast should always negate this, what's more puzzling is how the compiler gets the bright idea that "(long)NULL" and "(char)NULL" are fine where "(int)NULL is not"...

    (also, yes as mentioned the compiler is mingw/gcc)

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you provide the smallest and simplest program that demonstrates that problem.
    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

  3. #18
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Code:
    #include <cstring>
    using namespace std;
    int main()
    {
        int x;
        memset( &x, (int)NULL, sizeof(x)); // generates warning
        memset( &x, (long)NULL, sizeof(x)); // fine
        memset( &x, (char)NULL, sizeof(x)); // fine
        memset( &x, (int)(long)NULL, sizeof(x)); // fine
        memset( &x, (int)(void *)NULL, sizeof(x)); // fine
    }
    I don't use the word often, but this is definitely "lulzwtfcompiler"...

  4. #19
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I actually get an error here (GCC 4.2.3 on Linux)
    Code:
    a.cpp: In function ‘int main()’:
    a.cpp:10: error: cast from ‘void*’ to ‘int’ loses precision
    Line 10 is this line
    Code:
    memset( &x, (int)(void *)NULL, sizeof(x)); // fine
    If I take it out there is no warnings/errors even with "-Wall -ansi -pedantic"

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I don't use the word often, but this is definitely "lulzwtfcompiler"...
    No, it's "lulzwtfprogrammer". Why the hell do you insist on using NULL, which has always been meant for pointers, implementation constraints in C++ aside, as an integer? Why do you care at all?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm definitely with CornedBee here - NULL is not ZERO - if it was it would be called ZERO. It may well have the VALUE zero, but it is not the same. It is three characters longer to write NULL than 0, and if what you want to do is to fill an array with zero, then you should use 0.

    If you want to fill an array of pointers, then you should use:
    Code:
    for(size_t i = 0; i < sizeof(array)/sizoef(array[0]); i++)
    {
        array[i] = NULL;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matsp
    NULL is not ZERO - if it was it would be called ZERO. It may well have the VALUE zero, but it is not the same. It is three characters longer to write NULL than 0
    Stroustrup disagrees, and I think that one reason is because NULL is a macro, so after preprocessing it really is not (necessarily) three characters longer than 0, even if it is three characters longer to write than 0. I wish the idea of nullptr had come up much earlier.

    Quote Originally Posted by matsp
    if what you want to do is to fill an array with zero, then you should use 0.
    I agree, because as CornedBee pointed out, the intention of NULL is for it to be used as a null pointer constant, not an integer constant, even if that is what it is.

    Quote Originally Posted by matsp
    If you want to fill an array of pointers, then you should use:
    Alternatively:
    Code:
    std::fill(array, array + (sizeof(array) / sizeof(array[0])), static_cast<T*>(0));
    This also illustrates that NULL is misleading as a null pointer constant - attempting to use:
    Code:
    std::fill(array, array + (sizeof(array) / sizeof(array[0])), NULL);
    would not work since the type of NULL is an integer type, not a pointer type, so there is a type mismatch.
    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

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Stroustrup disagrees, and I think that one reason is because NULL is a macro, so after preprocessing it really is not (necessarily) three characters longer than 0, even if it is three characters longer to write than 0. I wish the idea of nullptr had come up much earlier.
    Just to clarify, I'm perfectly happy with:
    Code:
    T* ptr = 0;
    , using 0 instead of NULL. And I would also say that memset(array, 0, sizoef(array)) would be what I'd write - no, it's not fully portable, but I'd expect it to work on all architectures I've so far seen/used.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #24
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am more comfortable with T* ptr = 0; too. As laserlight pointed out it is less typing. Also, the statement is concisely doing something very understandable. Sometimes using NULL can make matters a bit more confusing. The nullptr thing is a welcomed addition to the language.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't see the problem.
    Use NULL for pointers and nowhere else.
    It's the same to type
    T* ptr = NULL;
    As it would be to write
    T* ptr = nullptr;
    They seem to do the same.
    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.

  11. #26
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    But my pinky got tired during the typing of NULL and nullptr looks too similar to a variable name (I am not being serious. I am just going back to the fact that 0 is less physical typing with a guaranteed understanding)

    0 is a unique value in programming. It is understood in areas where other constants would not be understood.

    Example:
    Code:
    T* ptr = 1;
    Is erroneous, and won't compile without a cast. Plus looking at it, it makes no logical sense.

    Example:
    Code:
    T* (*ptr)[](T (*)[], T *) = 0;
    Obviously is a nullptr. Indeed you can see it is a null pointer without necessarily knowing what it points to.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    T* (*ptr)[](T (*)[], T *) = 0;
    Code:
    T* (*ptr)[](T (*)[], T *) = NULL;
    Code:
    T* (*ptr)[](T (*)[], T *) = nullptr;
    They all look the same to me, but the two later are better, of course.
    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.

  13. #28
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Duly noted. I don't think I disagreed with you, by the way. I even pointed out where there were hints of sarcasm. Lets not be all bitter and mean to one another. Its Friday, afterall

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure, it's not that I disagree, but I don't why people are against NULL used on pointers.
    Using NULL on pointers would be exactly the same thing as using nullptr...
    I don't really see nullptr as a good addition, I only see it as good because if we start using it instead of NULL, we can't abuse it where we want to type 0.
    And really... a pointer shouldn't be initialized to 0, it should be initialized to NULL.
    Well, that's my opinion. It makes more sense to me. I'm a strict type kind of individual.
    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.

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I don't see the problem.
    Use NULL for pointers and nowhere else.
    Recall your argument against C89 allowing functions to be used without being declared. Would you agree that that rule in C is perfectly fine, since we can impose the coding standard rule that says "declare functions before use"?

    It's the same to type
    T* ptr = NULL;
    As it would be to write
    T* ptr = nullptr;
    They seem to do the same.
    It is also the same to type:
    Code:
    int* ptr = NULL;
    As it would be to write
    Code:
    int num = NULL;
    However, it is not the same to type:
    Code:
    int* ptr = nullptr;
    As it would be to write
    Code:
    int num = nullptr;
    Another problem involves overload resolution.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler Paths...
    By Cobra in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2006, 04:04 AM
  2. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 11:07 AM
  3. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 06:42 AM
  4. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 11:28 AM
  5. Help With finding a compiler
    By macman in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 04-15-2005, 08:15 AM