Thread: String::assign problems

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    String::assign problems

    Code:
    cstr[ getLastCharIndex(cstr) + 1 ] = '\0';
    str.assign( cstr );
    Code:
    str.assign( cstr, getLastCharIndex(cstr) + 1 );
    Why does the latter inject garbage into my strings?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The latter assigns one more (uninitialized?, null?) character to the string than cstr has. Null-terminator is not part of std::strings.

    Also, how does getLastCharIndex work if the string is not null-terminated?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    cstr is not null terminated (the length of the string is stored instead). The latter gets the index i of the last char and tells assign to copy i+1 characters starting from cstr.
    I've verified getLastCharIndex returns proper indices. It seems that assign doesn't like non-null-terminated char arrays??

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If getLastCharIndex returns the same value as strlen (if the string was null-terminated) are you not assigning one more character than there is in the second version?

    In the first version, yes the string obviously has to be null-terminated, because otherwise assign wouldn't know how long it is.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    getLastCharIndex returns the index of the last character, which would be equivalent to (strlen - 1) (keep in mind, no null-char here). In the first example, I allocated one extra byte for cstr to be able to assign the '\0'.
    Basically, if the string is "bar", then getLastCharIndex returns 2 (the index offset of 'r') and so assigning getLastCharIndex()+1 characters should copy the entire string and nothing more, yet I have no idea where the garbage is coming from?? I can verify the index is correct because the first example writes the null character to the correct address.
    Last edited by @nthony; 11-06-2008 at 02:16 AM.

  6. #6
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Actually I think I just found some larger problems in the ADT surrounding getLastCharIndex that may be causing this. I'll investigate some more. Thanks! and sorry for wasting your time.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    str.assign( cstr, getLastCharIndex(cstr) + 1 );
    What happens if you remove the +1?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM