How to deal with segmentation faults?

Hi Guys, I am having some problem with segmetation fault for like 10th times and again not able to solve it by myself. It drives me crazy I know, I am a dumb. Anyway, I am trying to create a Big Integer class for both examining OOP and it will be useful for Euler projects. I am having segmentation fault in line:

std::cout << *it << std::endl;

(in text editor Code tags were not working I do not know why. Maybe you are having the same problem.)

I need to know why this is happening. Actually, it will be good if I know how to handle segmentation fault and why it occurs. Thank you already.


The Header File ( BigInt.h ):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// BigInt Class Header
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>

//using namespace std;

class BigInt
{
	//
	public:
		//
		//
		BigInt(std::string integer);
		void TrimString(std::string Integer);
		void SetBigIntVector();
		
	private:
		std::vector< std::vector<int> >  BigIntVector;
		int row;
		std::string integer;
		int ** intMatrix;
		
		
};







The cpp file (BigInt.cpp):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include "BigInt.h"

BigInt::BigInt(std::string integer)
{
	//
	
	row = 0;
	for(int i=0; i<integer.size() && integer[i] <= '9' && integer[i] >= 0; i++)
	{
		//
		if( i % 8 == 0 )
		{
			//
			row+=1;
		}
		BigInt::BigIntVector.push_back( { row - 1 , integer[i] } );
		//if string is bigger than 8 steps, than we divide the string 
		//into small 8 int parces and do calculations with them seperately
	}
	
	BigInt::TrimString(integer);
}

void BigInt::SetBigIntVector()
{
	//
	int ** intMatrix = new int*[ BigInt::BigIntVector.size() ];
	for(int i = 0; i < BigIntVector.size(); i++ )
	{
		//
		intMatrix[i] = new int [2];
		intMatrix[i][0] = BigIntVector[i][0];
		intMatrix[i][1] = BigIntVector[i][1];
		
		std::cout << intMatrix[i][0] << "," << intMatrix[i][1] << std::endl;
		
	}
	
}

void BigInt::TrimString(std::string integer)
{
	for(std::string::iterator it = integer.begin(); it != integer.end(); ++it) 
	{
              //do_things_with(*it);
              std::cout << *it << std::endl;
	}
}

Last edited on
We'd expect the constuctor to intitalise all members of the class. You're not doing that, so you're bound to have problems.
That's not much, but I'd think that line 31 (BigInt.cpp) may be the culprit: You set the local variable not the member variable (line 23 ( BigInt.h ))
Ok, I had the code simplified like this:

BigInt.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>

class BigInt
{
	//
	public:
		//
		BigInt(std::string integer);
		void TrimString(std::string Integer);
		void SetBigIntVector();
		
	private:
		//
		std::vector<int>  BigIntVector;
		std::string integer;
	
};



BigInt.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include "BigInt.h"

BigInt::BigInt(std::string integer)
{
	//
	this->integer = integer;
	
	for(std::string::iterator it = this->integer.begin(); it != this->integer.end(); ++it) 
	{
    std::cout << *it << std::endl;
    this->BigIntVector.push_back(*it);
	}	
}


And it still gives me the sama error in the same line:

std::cout << *it << std::endl;

Am I not allowed to use std library in methods? I am really confused right now. How will I see what I am doing?
There is nothing wrong with the code snippet. Post code where the problem can be reproduced. You may have an undefined behavior somewhere else (like out of bounds access).

How will I see what I am doing?
Use the debugger.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include "BigInt.h"

int main(int argc, char** argv) {
	
	BigInt num1("12345678987654321");
	
	return 0;
}


if I construct num1 the fault occurs if I remove the line and run it does not. All the codes are these. So you say there is nothing wrong. Is it a linker issue then?
istemihan wrote:
All the codes are these.
No, you clearly have omitted code. @coder777 says there is nothing wrong with the code that you showed - meaning, the problem is in the code you have not shown.
If that is really all you have then there might be a problem with your compiler environment. Try to reinstall it.
Man I am gonna share whole codes- I mean whole files and codes I used- with the fault I get in order.

1- BigInt.h File:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>

class BigInt
{
	//
	public:
		//
		BigInt(std::string integer);
		void ShowBigIntVector();
		
	private:
		//
		std::vector<int>  BigIntVector;
		std::string integer;
	
};



2- BigInt.cpp File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include "BigInt.h"

BigInt::BigInt(std::string integer)
{
	//
	this->integer = integer;
	
	for(std::string::iterator it = this->integer.begin(); it != this->integer.end(); ++it) 
	{
    //std::cout << *it << std::endl;
    this->BigIntVector.push_back(*it);
    //std::cout << this->BigIntVector[0];
	}	
}

void BigInt::ShowBigIntVector()
{
	//
	for( int i = 0; i < this->BigIntVector.size(); i++ )
	{
		//
		std::cout << this->BigIntVector[i];
	}
	
}



3- main.cpp File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include "BigInt.h"
//#include "BigInt.cpp"

int main(int argc, char** argv) {
	
	BigInt num1("12345678987654321");
	num1.ShowBigIntVector();
	
	return 0;
}


And the errors I get in debug mode with tese codes:

link: http://i58.tinypic.com/dnj12g.png

link: http://i58.tinypic.com/2ywe3qr.png

link: http://i57.tinypic.com/fdbqrs.jpg
Last edited on
You have this declaration:
 
		std::vector< std::vector<int> >  BigIntVector;


Then you have this code:
1
2
3
4
5
6
7
8
9
10
11
BigInt::BigInt(std::string integer)
{
	//
	this->integer = integer;

	for(std::string::iterator it = this->integer.begin(); it != this->integer.end(); ++it)
	{
		std::cout << *it << std::endl;
		this->BigIntVector.push_back(*it);
	}
}
I can't see how a *it (a char) can be converted to a std::vector<int>. That code just doesn't make sense to me.

It seems to me that you're just changing code in a panic without thinking about your design. Just stop, remove all those members and implement one part at a time. I mean, why should a BigInt class have a string representation and a std:vector<int>?
@kbw

I mean, why should a BigInt class have a string representation and a std:vector<int>?


I don't know man, I thought I would use BigInt enormousInt("123456798765432345678876543234567876543345678876543234567")

instead of long ong int enormousInt = 123456798765432345678876543234567876543345678876543234567;

Because, you can not hold such number in long long int or any other data type. Right? So most useful thing I came up with was strings.

I can't see how a *it (a char) can be converted to a std::vector<int>. That code just doesn't make sense to me.


You are right, this is an obvious mistake but it is not the source of the error. I changed it to:

std::vector< char > BigIntVector;

And the same mistake still exist. Thank you for reply.

The code you provided compiles and runs without error. If this crashes it must be a problem with your compiler/linker.
@coder777

Oh man, this is the saddest problem that I ve been avoiding to hear it. Why can't it just be normal problem. Anyway thank you all guys!
I'm not clear on what code you have that is crashing as there have been a number of changes to what you started out with.

Can you post your current class definition of BigInt and the constructors please.

Thanks.
The current code leads to crash is this:

header file(BigInt.h):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>

class BigInt
{
	//
	public:
		//
		BigInt(std::string integer);
		void ShowBigIntVector();
		
	private:
		//
		std::vector< char >  BigIntVector;
		std::string integer;
	
};



cpp File(BigInt.cpp):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include "BigInt.h"

BigInt::BigInt(std::string integer)
{
	//
	this->integer = integer;
	
	for(std::string::iterator it = this->integer.begin(); it != this->integer.end(); ++it) 
	{
    //std::cout << *it << std::endl;
    this->BigIntVector.push_back(*it);
    //std::cout << this->BigIntVector[0];
	}	
}

void BigInt::ShowBigIntVector()
{
	//
	for( int i = 0; i < this->BigIntVector.size(); i++ )
	{
		//
		std::cout << this->BigIntVector[i];
	}
	
}



main File(main.cpp):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include "BigInt.h"
//#include "BigInt.cpp"

int main(int argc, char** argv) {
	
	BigInt num1("12345678987654321");
	num1.ShowBigIntVector();
	
	return 0;
}



Really all the codes are above, I swear :)
What compiler/IDE are you using? If it is not from within the last 5 years you should update it.

I can't reproduce any crash: https://ideone.com/9ZTfPE
Last edited on
Oh man, that was a bummer. I am using DevC++ 5.11. Which compiler do you use/suggest?
Odd, that version is less than a year old. Still, it's clearly not working for you for some reason.

On Windows I recommend Visual Studio 2015 - the community edition is free. Make sure you choose to install all the C++ tools - the option for that is unticked by default.

There's also Code::Blocks, but I've never used it.
In Code::Blocks works very fine guys. I think DevC++ needs to be developed to be better. See you..
I wouldn't write the code like that myself, but I don't see anything there to cause a crash.

Do you have a debugger in your environment? If so, run the program in the debugger and let it crash. We can then example why it crashed in your environment.
Topic archived. No new replies allowed.