-
April 22nd, 2013, 09:00 PM
#1
Memory Management : character arrays and = operator
Memory Management : character arrays and = operator
Q. In terms of Memory Management, What error would you have with the following code?
Code:
class String
{
public:
String(const char right[]);
String& operator= (const String& right);
int length() const;
private:
char* buffer;
int len;
};
int String::lenght() const {return len;}
String::String(const char right[])
{
len = 0;
while (right[len] != '\0')
len++;
buffer = new char[len+1];
for (int i = 0; i < len; i++)
buffer[i] = right[i];
buffer[len] = '\0';
}
String& String::operator= (const String& right)
{
int n = right.length();
for (int i = 0; i <= n; i++)
buffer[i] = right.buffer[i];
return *this;
}
Answer.
I have no clue... Could you help me?
The array size seems okay... new operator...
is it because of the dangling pointer because there is no delete operator?
Please let me know.
Thanks,
-
April 22nd, 2013, 09:24 PM
#2
Re: Memory Management : character arrays and = operator
Your class performs manual memory management, but you neglected to define the destructor. You also forgot to define or disable the copy constructor, although you did define copy assignment operator.
-
April 22nd, 2013, 09:28 PM
#3
Re: Memory Management : character arrays and = operator
Originally Posted by weonthewave
Memory Management : character arrays and = operator
Q. In terms of Memory Management, What error would you have with the following code?
In addition to all of that was stated, what happens if new[] fails here?
Code:
buffer = new char[len+1];
If new[] fails, you now have messed up your string contents (and is especially a problem if you coded an assignment operator that did this). This one error almost always gets overlooked by students, and even teachers. Now how do you fix it so that if new[] fails, your string is not corrupted, or that on construction, you don't give me a messed up string? That's your homework assignment from me to you.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; April 22nd, 2013 at 09:31 PM.
-
April 22nd, 2013, 09:33 PM
#4
Re: Memory Management : character arrays and = operator
Code:
String& String::operator= (const String& right)
{
int n = right.length();
for (int i = 0; i <= n; i++)
buffer[i] = right.buffer[i];
return *this;
}
And what if the destination buffer is smaller than the right.buffer?
This assignment operator is wrong. You need to destroy the old string, size it properly, (or resize the existing string, or leave it alone if it is already large enough), and then copy the source string to the destination string.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; April 22nd, 2013 at 09:35 PM.
-
April 22nd, 2013, 09:34 PM
#5
Re: Memory Management : character arrays and = operator
Originally Posted by Paul McKenzie
If new[] fails, you now have messed up your string contents.
Actually, no: that is a constructor, so if new[] fails, the constructor fails. There will be no string contents to speak of.
There does appear to be a potential buffer overflow problem with the copy assignment operator though.
-
April 22nd, 2013, 09:37 PM
#6
Re: Memory Management : character arrays and = operator
Originally Posted by laserlight
Actually, no: that is a constructor, so if new[] fails, the constructor fails. There will be no string contents to speak of.
You're right, but you know that once the assignment operator is "corrected", you'll more than likely see the same error.
Regards,
Paul McKenzie
-
April 23rd, 2013, 04:17 AM
#7
Re: Memory Management : character arrays and = operator
This assignment operator is wrong. You need to destroy the old string, size it properly, (or resize the existing string, or leave it alone if it is already large enough), and then copy the source string to the destination string.
You only destroy the old string and do the copy iff the destination of the copy is not the same as the source.
Code:
String st1;
st1 = st1;
needs to work!
Look up in a good c++ book how to correctly code assignments.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 23rd, 2013, 09:55 AM
#8
Re: Memory Management : character arrays and = operator
or you do what 90% of the apps out there do.
if new/new[] fails, the exe aborts.
:-p
-
April 23rd, 2013, 02:17 PM
#9
Re: Memory Management : character arrays and = operator
I think this thread basically lays out all of the mistakes that beginners make when creating a copy constructor and assignment op, especially for things like a string class.
The problem is that there are many teachers who only cover some or even none of these errors, making the student believe they are writing good or acceptable class interfaces.
Regards,
Paul McKenzie
-
April 23rd, 2013, 02:56 PM
#10
Re: Memory Management : character arrays and = operator
The problem is that there are many teachers who only cover some or even none of these errors, making the student believe they are writing good or acceptable class interfaces.
Unfortunately only too true! Some books don't even cover it correctly either.
qui docet doctores?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 24th, 2013, 09:43 AM
#11
Re: Memory Management : character arrays and = operator
Originally Posted by 2kaud
qui docet doctores?
codeguru ?
Tags for this Thread
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
|