Thread: Silent cast to unsigned

  1. #16
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Quote Originally Posted by anon View Post
    Perhaps you could initialize the table in a way and reference it through a (static) pointer that would allow you to use negative indices (if the signedness of the char type is implementation defined as I suspect you might set it all up at runtime - or may-be at compile time through some tricks).

    I also wonder why you are determining the case sensitivity within the function. There is already strcmp which should be good for case sensitive comparisons, so you'd only need to implement the case insensitive version. And if you want a function that makes this choice through a third parameter, that would only call either strcmp or your version.

    Also, do you need those ints. If you figure out the correct indexing, it needn't be more complex than
    Code:
    while (*s1 && *s2 && table[*s1] == table[*s2]) {
        ++s1;
        ++s2;
    }
    return something
    I would also suggest fixing the function's prototype. It doesn't change the strings, so they should be const char*'s.
    Yes, it's now only for testing. Later on i will only have case insensitive version. But thanks for other tips...

  2. #17
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    EDIT1: deleted.
    Yes i get those messages, and they show indexing is wrong, right?

    EDIT2:
    But what about if c1 is negative, it gets indexed in table OK, but as TABLE[] is initialized with POSITIVE integers - c1 gets converted to positive and that test fails because of that?

    EDIT3:
    Solved. It gives 0 from "outter space", so it never crashes (as I expected)...
    Last edited by rasta_freak; 08-06-2008 at 06:08 AM.

  3. #18
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Thanks matsp

    EDIT: How can I assure that I get SIGSEGV or anything on such invalid access?
    Last edited by rasta_freak; 08-06-2008 at 05:58 AM.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rasta_freak View Post
    Thanks matsp

    EDIT: How can I assure that I get SIGSEGV or anything on such invalid access?
    Realistically, you can't. The reason being that you can not control where in memory your TABLE ends up, so you can't guarantee that what is before or after it is invalid memory. That is what you have assert and similar functionality for - checking for things that should not matter.

    By the way, if you compile with optimization enabled, I suspect there will be no big difference in the form that casts your char to unsigned char for the table access.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM