Troubleshooting Segmentation Faults in C++ Operator Overloading

  • C/C++
  • Thread starter John O' Meara
  • Start date
  • Tags
    C++ Fault
In summary, the code is trying to call a function that does not exist, and as a result, a segmentation fault occurs.
  • #1
John O' Meara
330
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
  • #2
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;
 
  • #3
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.
 
  • #4
temp = operator - (op1, op2); is a mistake it should read; temp = operator + (op1,op2). That is the '-' operator calls the + operator, sorry about that.
 
  • #5
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.
 
  • #6
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.
 
  • #7
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.
 
  • #8
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.
 
  • #9
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.
 

What is a Segmentation Fault in C++?

A segmentation fault, also known as a segfault, is an error that occurs when a program tries to access a memory address that is outside of its allocated space. This can happen due to a bug in the code or attempting to access memory that has been freed.

What causes a Segmentation Fault in C++?

A segmentation fault can be caused by a variety of reasons, such as trying to access a null pointer, dereferencing a dangling pointer, or writing to read-only memory. It can also occur when there is a stack overflow or a buffer overflow.

How do I debug a Segmentation Fault in C++?

Debugging a segmentation fault can be challenging as it often does not provide specific information about the error. However, some techniques to debug it include using a debugger, printing out the values of variables, and using a tool like Valgrind to detect memory errors.

How can I prevent Segmentation Faults in my C++ code?

To prevent segmentation faults, it is essential to write clean and error-free code. This includes properly allocating and freeing memory, checking for null or dangling pointers, and avoiding buffer overflows. Using a debugger and testing your code thoroughly can also help catch potential issues.

Can a Segmentation Fault be fixed?

Yes, a segmentation fault can be fixed by identifying and fixing the underlying cause of the error. This may involve rewriting parts of the code, using debugging techniques, or implementing better memory management practices. In some cases, a segfault may also be caused by a hardware issue, in which case it cannot be fixed by the programmer.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top