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,
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.
Re: Memory Management : character arrays and = operator
Quote:
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
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
Re: Memory Management : character arrays and = operator
Quote:
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.
Re: Memory Management : character arrays and = operator
Quote:
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
Re: Memory Management : character arrays and = operator
Quote:
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.
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
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
Re: Memory Management : character arrays and = operator
Quote:
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?
Re: Memory Management : character arrays and = operator
Quote:
Originally Posted by
2kaud
qui docet doctores?
codeguru ? :)