PDA

View Full Version : C++ Operator overloading


brad211987
10-25-2009, 05:53 AM
I am working on learning operator overloading in C++ and have managed to do most of it except the >> input operator. Here is my setup:

I have a class named MyString that is essentially a basic implementation of a string class, provides methods such as compare, find, insert, erase etc...

Next class is MyStringDerived, which overloads operators to make the MyString class easier to use. MyStringDerived extends MyString.

My problem comes with overloading the >> operator, here is my current attempt:


istream & operator>>(istream & in, MyStringDerived & myStringDerived)
{
string line;

in >> line;
char* ch;

strcpy(ch, line.c_str());
MyString val = ch;

myStringDerived.setStr(val.getStr());

return in;
}


On this particular iteration, the program simply terminates with no errors or output when it comes to the strcpy.

If anyone could give me a kick in the right direction on how to approach this, I would be incredibly greatful.

oracleguy
10-25-2009, 07:40 AM
That is because the variable ch is an uninitialized pointer. So it is causing a segmentation fault.

You need to change it so:
char* ch = new char[string.length() + 1];
strcpy(ch, line.c_str());
MyString val = ch;

myStringDerived.setStr(val.getStr());
delete ch;
return in;


Though I wonder why you are doing it in such a roundabout way. Why doesn't your setStr function take a const char* as one of the overloads? Then you wouldn't need the MyString.

It has been a long time since I've overloaded that operator so I forget exactly but there should be some way to read the stream directly... hopefully.

brad211987
10-25-2009, 04:18 PM
Can I just hire you until I finish re-learning C++? haha

changed it to look like this:


string line;

in >> line;

char* ch = new char[line.length()];

strcpy(ch, line.c_str());

myStringDerived.setStr(ch);

return in;


and it works great.

Is there a way to read directly into a char* instead of going first to a string and stepping the value into the char*?

oracleguy
10-25-2009, 07:21 PM
Is there a way to read directly into a char* instead of going first to a string and stepping the value into the char*?

You can do that but the issue you can run into is that there is no protection against a buffer overflow. If you want to see what I mean, try this simple little program down below that I wrote this morning.

I'm not sure how the string class implements the operator overloading. You should be able to see the code for it though. I can't even remember the last time I overloaded the extraction operator so I don't know a lot of the neat tricks to it.


#include <iostream>
#include "string_class.h"

using std::cin;
using std::cout;
using std::endl;

int main()
{
char str1[4];
char str2[4];

cout << "Address of str1: " << &str1 << "\nAddress of str2: " << &str2 << endl;

strcpy(str2, "FOO");

cin >> str1;

cout << "str1 is: " << str1 << "\nstr2 is:" << str2 << endl;

system("pause");

return 0;
}


In this case I put the c-strings on the stack instead of the heap so I can ensure they would be in adjacent memory locations. That will make it easier to demonstrate the danger.

If you run the program and type: str and hit enter, you'll should see:

Address of str1: 000000000012FEE0
Address of str2: 000000000012FEE4
str
str1 is: FOO
str2 is:str
Press any key to continue . . .


Notice the address of str1, that is is lower than str2, if on your system the compiler puts str1 after str2, you need to change the program. Change the strcpy to use str1 and the cin to use str2.

In regards to the output, notice how everything is fine. Each array is 4 characters, so str with a null is four. The same with FOO.

Now re-run it and type in: horray

Address of str1: 000000000012FEE0
Address of str2: 000000000012FEE4
horray
str1 is: horray
str2 is:ay
Press any key to continue . . .


Notice how str2 was overwritten? Because the cin doesn't know where to stop. Try typing in: food and you'll notice str2 is blank, that's because the null from the str1 is at str2[0]. Which allows str1 to print out ok because as you know, when printing c-strings, it just keeping going until it finds a null. That can cause some frustrating problems if you don't realize you are going over the array size.

Now type something longer than 8 characters in. Depending on how your program was compiled, it either will work fine or crash perhaps throwing an invalid instruction exception. If that happens, it means the data overwrote part of your program's code in memory.

That is why buffer overflow vulnerabilities can be so dangerous. You can actually alter a program's code while it is running and make it do something malicious.

If you already knew all this, sorry, I didn't mean to tell you stuff you already know but I think it is a interesting aspect of programming that C and C++ has.

brad211987
10-25-2009, 09:00 PM
I was pretty familiar with buffer overflows from computer security classes, which is one of my bigger interests. It is definitely nice to see an easy and clear cut example, especially when I was on the verge of writing one. :thumbsup:

The transition from Java back to doing C++ again has been particularly difficult in the past few weeks, we only really touched on the basics during the C++ class I took in high school. I've been familiar with the concepts and ideas from my own study, but as usual, the practical is much more difficult than the conceptual.

Thanks again for the help, back work for me!