Troubleshooting Segmentation Faults in C++ Operator Overloading

  • Context: C/C++ 
  • Thread starter Thread starter John O' Meara
  • Start date Start date
  • Tags Tags
    C++ Fault
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting segmentation faults in C++ related to operator overloading, specifically focusing on the implementation of the subtraction operator for a custom class. Participants explore potential causes of the error and suggest debugging strategies.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Experimental/applied

Main Points Raised

  • One participant questions whether the parameters of the + operator must always be objects of a class and discusses the possibility of calling one operator from another.
  • Another participant suggests that the original code may lead to a stack overflow due to infinite recursion, as the subtraction operator calls itself without a termination condition.
  • There is a correction regarding the operator call, with one participant indicating that the subtraction operator should not call itself but instead call the addition operator.
  • A participant expresses a lack of familiarity with the gdb debugger and seeks guidance on how to use it for debugging segmentation faults.
  • Another participant provides instructions on using gdb with g++, emphasizing the importance of compiling with debugging information.
  • One participant mentions using tracer statements to identify the source of the error, indicating a workaround while acknowledging the need for a debugger as the program grows in complexity.

Areas of Agreement / Disagreement

Participants express differing views on the nature of the error (segmentation fault vs. stack overflow) and the appropriate use of operator overloading. There is no consensus on the best approach to resolve the issue, and the discussion remains open-ended.

Contextual Notes

Some participants note limitations in debugging tools available on specific operating systems, which may affect the ability to troubleshoot effectively.

Who May Find This Useful

Programmers working with C++ who are interested in operator overloading, debugging techniques, and those facing similar issues with segmentation faults or recursion in their code.

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::count << "##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 ·
Replies
23
Views
3K
Replies
53
Views
5K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
6K