C/C++ Troubleshooting Segmentation Faults in C++ Operator Overloading

  • Thread starter Thread starter John O' Meara
  • Start date Start date
  • Tags Tags
    C++ Fault
AI Thread Summary
The discussion centers on writing a C++ program that manipulates numbers as C-style strings and addresses specific coding issues related to operator overloading. The main questions involve whether operator parameters must always be class objects and the feasibility of calling one operator from another. A segmentation fault is reported in the provided code snippet, which is attributed to infinite recursion caused by the operator- function calling itself without a termination condition. Participants suggest that the user likely intended to call the operator+ instead. Debugging advice is offered, including using gdb with the g++ compiler to identify errors, although the user mentions limitations on their RISC OS machine. The user ultimately resolves the issue through tracer statements and expresses interest in adding more functionality to the program, indicating a need for a debugger as the project expands.
John O' Meara
Messages
325
Reaction score
0
I'm trying to write a program in C++ that reads, writes, adds and substracts numbers entered as C-style strings. I have two questions. Is it necessary that the parameters of the + operator only be always objects of a class? Can an operator call another operator from within it, e.g.
my_float operator - (my_float &op1, my_float &op2)
{
my_float temp;

if (op2.num[0] == '+')
op2.num[0] = '-';

temp = operator-(op1, op2);

return temp;
}

The above piece of code causes a segmentation fault, why? Are the arguments in the code of the operator - wrong? I do not know what else to place there. Thanks very much.
 
Technology news on Phys.org
To use the operator you should write

Code:
temp = opA-opB;

but looks to me that would generate stack overflow.

I guess you really mean something else, like

Code:
temp = op1.some_value - op2.some_value;
 
Are you sure it's a segmentation fault, could it be a stack overflow?

This looks like you will enter an infinite recursion loop. operator- calls operator-, which will call operator-, which will call operator-... when you use recursion you have to have a "termination condition" so that the recursion can eventually stop.

I don't really understand why you're making a recursive call at all here. It looks like what you really meant to do was call operator+.

What are you using to compile this? Do you understand how to use gdb or another debugger? This can help in identifying exactly where or why errors occur.
 
temp = operator - (op1, op2); is a mistake it should read; temp = operator + (op1,op2). That is the '-' operator calls the + operator, sorry about that.
 
I don't know how to use the gdb debugger or how to get my hands on it. I am using g++ as my compiler. Thanks.
 
If you are using g++ you already have gdb. Compile your application with:

g++ -g filename.cpp

Instead of g++ by itself, then run:

gdb exename

Then type "run".

When your application hits the segmentation fault, it will automatically drop you back into gdb and show you the exact line the problem is happening on. You can easily find gdb tutorials if you look around to show you how to do more stuff, but just knowing the place where the crash happens is a big step.
 
Thanks very much Coin. I am actually using g++ on a little known RISC OS machine and as far as I know there may be no debugger made portable for the RISC OS even though gcc and g++ are ported all-right.
 
One thing you could try then is running your program (or some subset thereof that only tests the my_float class) on your local pc, just to debug it. Assuming you are running Windows you could get access to g++ and gdb by installing cygwin or mingw, or install visual studio express.
 
Thanks, once again Coin. I will follow up your suggestions in post 7 and other posts here. I actually found the offending code using what I call tracer statements; by putting in enough of std::cout << "##Left the sub() function" << std::endl; etc. I tracked it down, and then commented out the offending code, it worked, fingers crossed, because what is happening with the offending code, I have yet to figure out.
A debugger would very handy espically as my program gets bigger, now I think I shall add more functionality to the program. I am using a windows laptop for here, I could as you suggest use it with gnu compiler collection. Thanks.
 

Similar threads

Replies
23
Views
2K
Replies
89
Views
5K
Replies
5
Views
3K
Replies
31
Views
3K
Replies
4
Views
3K
Replies
4
Views
2K
Back
Top