|
-
January 22nd, 2010, 04:29 PM
#4
Re: What is the problem with the code?
Thanks for your reponse! You said " When the original string object is deleted, your “copied” one will contain a dead pointer". I wander when that the original string object is deleted happens? It looks like no variables are out of scope. Thanks for your inputs.
 Originally Posted by VladimirF
One obvious problem is that secure version of strcat() requires the available size of destination string to prevent buffer overrun; you are passing the “desired” size, which no one allocates.
In your main function, the constructor:
will allocate exactly 3 chars, with no room for concatenation. So this line:
Code:
s2 = s1 + " there";
will write outside of allocated memory (undefined behavior; likely – crush).
Also, don’t you need to implement an assignment operator for your string? The one generated by compiler will copy your member variables (char* m_str). When the original string object is deleted, your “copied” one will contain a dead pointer.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|