Embedded_Dude
Embedded_Dude

Reputation: 227

String, debugging, and Segmentation Error goes hand in hand

Hi I am doing this simple program

 #include <iostream>
 #include <string>

 using namespace std;

 int main (){

      string hi("Hi how are you");

      for(int i = 0;i<4;i++)
           cout<<hi<<endl;

      return 0;
 }

When I compiled it and run it, there were no issues, but when I try to debug it, every time the IDE program (Code::Block 16.01) steps in or out of string hi("hi how are you") it gives me a Segmentation Fault.

I know SF is when the program tries to access a space of memory that it is not supposed to access, and I know string class is a C-Sytle string that allocates memory dynamically, and deletes them automatically when the program is finished, hence there should be no issue in memory management, so this code shouldn't be a problem.

BUT in this code I don't understand why do I get a SF when I debug it. When I tried debugging it for the first time and stepped out of hi, there were no errors, but when I tried watching hi, it gave me a SF, and when I try to debug it again, and I steped into string hi I get S.F.

Screenshot of the error FYI enter image description here

Upvotes: 2

Views: 35

Answers (1)

BartekPL
BartekPL

Reputation: 2420

When I was search information about this issue I found entry in Code::Blocks forum, but it is quite old.

However, there is possibility of bug in GDB for MiniGW. If you want to be sure, you should look for this issue and its fix.

I know that this isn't full answer, but you should go to this posts and read they, there is some solution:

Code::Blocks forum's posts:

1. Watching std::string in debugger causes segfault?!?

2. Still having seg fault while watching a string....

Upvotes: 1

Related Questions