CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2001
    Location
    germany
    Posts
    772

    CString concatenation

    Hi GUys,

    I am having a CString and append other CString to it by using the
    += operator. Normally that's ok but sometime the app crashes.
    MSDN says:
    "You should be aware that memory exceptions may occur whenever you use this concatenation operator because new storage may be allocated for characters added to this CString object. "

    Ok, right. So how can I append a CString to another safely?

    thanks
    Please rate if it helps!

    * The second mouse gets the cheese.

    * Birthdays are good for you. The more you have, the longer you live.

    * Always keep your words soft and sweet, just in case you have to eat them.

    * Always read stuff that will make you look good if you die in the middle of it.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: CString concatenation

    The chances that a CString += operation causing a crash are pretty close to zero. Unless your string are millions of characters long, I would suspect that if you're having problems the cause is somewhere else.

  3. #3
    Join Date
    Aug 2001
    Location
    germany
    Posts
    772

    Re: CString concatenation

    Yes it used to work fine. Now, when I'm debugging the app, sometimes I get an error at that location.
    it is something like: (the aStr has the sufficient length so the delete is not the problem.
    Code:
     
    			aStr.Delete(0,8);	
    			formatted += "     Bank:          ";
    			formatted += aStr.Left(2);
    			formatted += '\n'; 
     
    			//bank
    			aStr.Delete(0,2);
    			formatted += "     Address:      ";
    			formatted += aStr.Left(4);
    			formatted += '\n';
    
    			//length
    			aStr.Delete(0,4);
    			formatted += "     Length:        ";
    			formatted += aStr.Left(4);
    			formatted += '\n';
    Please rate if it helps!

    * The second mouse gets the cheese.

    * Birthdays are good for you. The more you have, the longer you live.

    * Always keep your words soft and sweet, just in case you have to eat them.

    * Always read stuff that will make you look good if you die in the middle of it.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: CString concatenation

    Looking at that code, the most like problem is that it's actually Left blowing up if aStr is too short.

    Have you tried the debugger?

  5. #5
    Join Date
    Aug 2001
    Location
    germany
    Posts
    772

    Re: CString concatenation

    Thanks! I'll take a look at that.
    Please rate if it helps!

    * The second mouse gets the cheese.

    * Birthdays are good for you. The more you have, the longer you live.

    * Always keep your words soft and sweet, just in case you have to eat them.

    * Always read stuff that will make you look good if you die in the middle of it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured