• Good Point (unregistered)

    My favorite example of someone not understanding C strings was:

    sprintf(buff, "i = %d\0", i);
    The coder told me that with sprintf() you needed to put a \0 at the end of the format string or else the output buffer would not be properly terminated. Only sprintf() works in this manner.
  • Stringer (unregistered) in reply to Good Point

    You know, we've been seeing a lot of these misuse-of-string WTFs lately. Don't you just wish that someone would write a wrapper for all of the nastyness of manipulating them, so inept programmers could just use it instead of having to try (and fail) to reinvent the wheel?

  • (cs) in reply to Stringer
    Stringer:
    You know, we've been seeing a lot of these misuse-of-string WTFs lately. Don't you just wish that someone would write a wrapper for all of the nastyness of manipulating them, so inept programmers could just use it instead of having to try (and fail) to reinvent the wheel?

    Where's the fun in just using the wheel? We want to invent it! :)

    At least, that's the prevailing attitude in newb programmers that have not yet seen the light.

  • Stephen Touset (unregistered) in reply to Erzengel

    The fun thing is that nobody's asked HOW the person expected strcat(buffer, '\0') to work if the terminating NULL wasn't already there. strcat needs the end of the string in order to append something to it.

  • Andrew (unregistered) in reply to Jon Skeet
    Jon Skeet:
    RON:
    xeron:
    I think the first one was actually attempting to make each item point to a seperate instance of "\nDEFAULT", and went about it in quite a bizarre way.

    It looks like VB, which is on .NET now, which means that all strings are interned. So any two references to the string "\nDEFAULT", no matter how they were constructed, will point to the same exact string objects in the string store.

    I don't believe that VB.NET interns all strings. C# certainly doesn't. For instance:

    string x = "hello"; string y = "hel"+"lo"; string tmp = "hel"; string z = tmp+"lo";

    x and y now refer to the same strings. z refers to a different string.

    Jon

    So what? x and y can point to the same string at compile-time. z is not resolved until run-time since tmp is a variable, not a constant.

    Yes, its true that z can be optimized at compile-time. But that's only really useful in languages where variables are pre-declared (C or Pascal). C#, C++, & Java can declare string types after executable statements.

  • (cs) in reply to iMalc
    iMalc:
    btw, your strlen is broken. I've never seen anyone use chars 1 thru 10 to terminate a string before.

    What?! I do it all the time!

    Crap, guess I got kind of overeager with the incrementing numbers... should have looked it over before posting it. There is some big irony in that somewhere...

  • (cs) in reply to Stringer
    Stringer:
    You know, we've been seeing a lot of these misuse-of-string WTFs lately. Don't you just wish that someone would write a wrapper for all of the nastyness of manipulating them, so inept programmers could just use it instead of having to try (and fail) to reinvent the wheel?
    Why? so they can use your wrapper incorrectly?
  • BillyBob (unregistered) in reply to EvanED

    The "Next>>" link is still broken :-/

    EvanED:
    size_t strlen(const char* s)
    {
       if( s[0] == 0 ) return 0;
       if( s[1] == 1 ) return 1;
    

    [snip]

    if( s[9] == 9 ) return 9; if( s[10] == 10 ) return 10;

    return ERR_STRING_TOO_LONG; }

    Assuming you fixed up the comparisons in the if statements, this function is only useful if ERR_STRING_TOO_LONG is defined as 42.

  • espinafre (unregistered) in reply to snoofle
    snoofle:
    using Java:
     final static Character A = new Character((char)0x42) /* letter */;
     final static Character B = new Character((char)0x41) /* letter */;
     // ...
     final static Character CR= new Character((char)0x13) /* letter */;
     
     // WTF-way to create a string
     public String makeString(Collection letters, 
                              Boolean    mode) {
       if (!mode != !!false) {
          String s = "";
          for (String theLetter : letters) {
               s += (""+theLetter);
          }
          return s.toString();
       } else if (!mode != !false) {
          String data[] = { CR,D,E,F,A,U,L,T };
          return makeString(new ArrayList(Arrays.asList(data));
       } else {
          Characterdata[] = {F,I,L,E, N,O,T, F,O,U,N,D};
          return makeString(new ArrayListArrays.asList(data));
       }
    }
    
    

    I think there should be some XML somewhere in that code, or else it won't be enterprisey enough.

  • JayeAeotiv (unregistered)
    void dumpToConsole(String msg, int code, String details, String stack) {
    
    	StringBuffer sb = new StringBuffer();
    
    	sb.append(msg + " Code: " + String.valueOf(code) + " Details: " + details + "\r\n" + stack + "\r\n");
    
    	System.out.println(sb.toString());
    
    }
    

    captcha: vern

  • dkf (unregistered) in reply to Stringer
    Stringer:
    You know, we've been seeing a lot of these misuse-of-string WTFs lately. Don't you just wish that someone would write a wrapper for all of the nastyness of manipulating them, so inept programmers could just use it instead of having to try (and fail) to reinvent the wheel?
    Such libraries exist; they're called scripting language implementations.
  • Old Wolf (unregistered) in reply to Stephen Touset
    Stephen Touset:
    The fun thing is that nobody's asked HOW the person expected strcat(buffer, '\0') to work if the terminating NULL wasn't already there. strcat needs the end of the string in order to append something to it.

    Fancy seeing you here ;)

  • ajk (unregistered)

    actually in windoz sometimes an extra null is required for some api calls e.g. OpenFileDialog

  • (cs) in reply to ajk
    ajk:
    actually in windoz sometimes an extra null is required for some api calls e.g. OpenFileDialog

    Ah yes, that's a series of null-seperated strings terminated by a double-null, isn't it? (The Windows API is fun!)

  • FIA (unregistered) in reply to Stilgar
    Stilgar:
    if true what is the point? what is more = new String("\nDEFAULT"); would have accomplished that... I think

    <sigh> You know it's time to finish for the day when the first thing you think when reading this is 'You don't need the "if (true)"'

  • PC Paul (unregistered) in reply to Stephen Touset
    Stephen Touset:
    The fun thing is that nobody's asked HOW the person expected strcat(buffer, '\0') to work if the terminating NULL wasn't already there. strcat needs the end of the string in order to append something to it.
    strcat() removes the NULL at the end of the first string before adding the second. That's why they used "\0" to cat *two* NULLs onto the end.

    Quite clever really...

    (for any n00bs - wrap this with <JOKE> tags liberally.)

  • (cs) in reply to newfweiler
    newfweiler:
    char * buffer = malloc(strlen(string));
    strcpy(buffer, string);
    
    And don't forget to free it when done!
    delete buffer;
    // Sometimes that doesn't work and you 
    // have to delete it the other way
    if (buffer)  // still allocated?
       delete [] buffer;  // or {} or whatever
    

    Uh ... didn't ANYONE notice that the malloc made the buffer one character too small, causing a guaranteed buffer overrun?

    At least someone noticed the mixing of malloc and delete, and the confusion between "delete" and "delete []".

    (Edit: wtf? the 'didn't ANYONE' line is after the close quote tag. Why is it in the box? Look at the page source.)

  • Andrew (unregistered) in reply to Misha

    Come on, extra nulls in memory can help. This reminds me of programming back in school. Sometimes you could get a program to run after the fith or sixth attempt at executing it. That was because I filled RAM up with enough mis-placed \0s that I eventually got one in the right place :)

  • Nelle (unregistered) in reply to ChadN
    ChadN:
    bathe:
    You must be relatively new to the art of computer programming if you believe somebody might be joking when they say there are broken implementations of strcpy.

    [...]

    But yes, especially with "optimizations", I suppose I can imagine a broken strcpy(). But the canonical, unoptimized (arguably) K&R versions are small and easy to carry around with you, until you prove the library versions correct. Working with broken C string functions is like trying to write a story without consonants or vowels.

    UNOPTIMISED.isPossible(EMBEDDED).DefaultValue = false;

Leave a comment on “Strings in Hiding”

Log In or post as a guest

Replying to comment #:

« Return to Article