Thread: tertiary comparison

  1. #31
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I can't think of any strictly 'bad' value for a char
    That's because you really won't see it on a two's complement architecture. Imagine a machine that uses a binary representation where there are two distinct representations for zero. When viewed as signed char, you can't tell the difference between the bit patterns of negative zero and positive zero. One of those zeros could be a trap representation. The solution on such an architecture would be to use unsigned char where the bit patterns are easily distinguished.

    >I feel like I'm in school, and the teacher just made a mistake that she's trying to cover up
    Yet you aren't really sure if that's the case or the teacher is just good at playing you. In truth, I wasn't expecting you to notice. But I was hoping you would so that I could give the speech about striving for excellence that I had been keeping in reserve for just such an occasion.
    My best code is written with the delete key.

  2. #32
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>you really won't see it on a two's complement architecture
    I've heard of one's complement before...

    >>you can't tell the difference between the bit patterns of negative zero and positive zero. One of those zeros could be a trap representation.
    *lightbulb goes on* So how would trap representations be used? Or are they just an unwanted sideeffect?

    >>Yet you aren't really sure if that's the case or the teacher is just good at playing you.
    Of course not. If I knew she was playing me, that would take all the satisfaction out of it

    >>the speech about striving for excellence that I had been keeping in reserve for just such an occasion.
    Lesson #2: Always have a backup plan. I'll remember that too
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #33
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    They aren't represented the same. For instance, you can't convert a function pointer to a pointer to void.
    Yes you can. C++ would complain about doing that, but C wouldnt.

  4. #34
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by bithub
    Yes you can. C++ would complain about doing that, but C wouldnt.
    No you can't:
    C: A Reference Manual, 5th Edition
    Page 137

    5.3.1 Generic Pointers
    , pharagraph two:

    Standard C introduces the type void * as a "generic pointer." It has the same repre-
    sentation as type char * for compatibility twith older implementations, but the language
    treats it differently. Generic poitners cannot be dereferenced with the * or subscripting
    operators, nor can they be operands of addition or subtraction operators. Any pointer to an
    object or incomplete type (but not to a function type) can be converted to type void * and
    back without change. Type void * is considered to be neither an object pointer nor a func-
    tion pointer.
    I'm sure Prelude or someone will be along with the direct quote from the standard,
    but this works for me.

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

  5. #35
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Internally they are represented the same (Just a pointer to a location in memory). If you want, just look at the ASM generated by your compiler.

    In fact, gcc let me assign a void* to a function pointer and vice versa. You can even dereference the function pointer with a simple cast.

    Generic poitners cannot be dereferenced with the * or subscripting
    operators, nor can they be operands of addition or subtraction operators.
    If that's true does GCC not follow the standards then? GCC lets you add pointers to void with no problems. GCC does want you to cast the void to something else before dereferencing it though.

    Code:
    void *voidptr;
    char buff[] = "some buffer";
    
    voidptr = buff;
    voidptr = voidptr + sizeof(char);
    printf("voidptr is pointing to a %c\n",*(char*)voidptr);

  6. #36
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Don't confuse "guaranteed by standard" with "my compiler does this"

    Compiling your code like this, gets a warning
    Code:
    $ gcc -W -Wall -ansi -pedantic hello.c
    hello.c: In function `main':
    hello.c:11: warning: pointer of type `void *' used in arithmetic
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #37
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Internally they are represented the same (Just a pointer to a location in memory).
    I challenge you to prove that this is the case on every possible system that supports C.

    >If that's true does GCC not follow the standards then?
    It depends on what switches you use to compile.
    My best code is written with the delete key.

  8. #38
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I challenge you to prove that this is the case on every possible system that supports C.
    Obviously that is impossible to do without access to every system on the planet (plus there is always the exception which disproves the rule). I have programmed in C/ASM on over 15 different system types (this includes a lot of embedded systems), and they all follow the same basic scheme. Calling a function is just pushing the next instruction address on the stack, then pushing the function parameters on to the stack, then jumping to the function address.

    It depends on what switches you use to compile.
    I did not know that. Just out of curiousity, why doesnt gcc use the C standards by default? Is it for backwards compatability reasons (so older code compiles fine with the default settings)? Ive seen a lot of makefiles for gcc, but I cant recall any of them using the ansi switch (maybe I just wasnt paying close enough attention).

  9. #39
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >plus there is always the exception which disproves the rule
    Well, technically the rule is that function pointers aren't required to have the same representation as object pointers. But I think you get the point, so I'll lay off it.

    >Just out of curiousity, why doesnt gcc use the C standards by default?
    I have no idea. I would assume that it's because GCC tries to be a lot of things to a lot of people. And the majority of programs make good use of non-standard extensions. Of course there's the risk of somebody being raised to believe that the extensions are blessed by the language standard, and that's where problems arise.
    My best code is written with the delete key.

  10. #40
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well, technically the rule is that function pointers aren't required to have the same representation as object pointers. But I think you get the point, so I'll lay off it.
    Fair enough, I can live by the argument that just because something is the way it is now, doesn't mean it will be that way in the future.

    Of course there's the risk of somebody being raised to believe that the extensions are blessed by the language standard, and that's where problems arise.
    I've been guilty of that before

    EDIT:
    because I cant seem to line up my quote tags properly

  11. #41
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I've been guilty of that before
    Me too. That's one reason why I'm so hard on people who use non-portable constructs out of habit. I've been there, got better, and realized how much easier programming is when you adhere to the standard as much as possible.
    My best code is written with the delete key.

  12. #42
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    GCC has a lot of extensions to the C standard. They are not portable, but if you're an OSS dev it's very likely that your apps won't be compiled on anything but GCC.

    This is a list. Nested functions are quite nice to make code cleaner - and if the program needs to be compiled on a different compiler this is easy to change. The typeof() statement might be harder to replace on a non-GCC compiler :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  3. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  4. Binary comparison
    By tao in forum Windows Programming
    Replies: 0
    Last Post: 06-28-2006, 12:10 PM
  5. comparison between pointer and integer
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 01:15 PM