Program crash during dubug

Hello there!
Been breaking my head for couple of hours,the program i´m writing for a school asigment crashes every time when i try to compile it,and i have no clue why..
Hope someone out there can point out the error!

here is the relevant code:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Graph
{
private:
int nroftowns;
int row;
int col;
string*Towns;
int**graph;

public:
Graph();
Graph(string cities[], int n);
void readFile();


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
53
54
55
56
#include "Graph.h"
Graph::Graph()
{
	this->Towns=new string[nroftowns];
	this->graph=new int*[nroftowns];
	for(int i=0;i<nroftowns;i++)
	{
		this->graph[i]=new int[nroftowns];
	}
	
	for(int i=0;i<nroftowns;i++)
	{
		for(int j=0;j<nroftowns;i++)
		{
			graph[i][j]=-1;
		}
	}
}
Graph::Graph(string cities[], int n)
{
	this->Towns=new string[nroftowns];
	this->graph=new int*[nroftowns];
	for(int i=0;i<nroftowns;i++)
	{
		this->graph[i]=new int[nroftowns];
	}
	
	for(int i=0;i<nroftowns;i++)
	{
		for(int j=0;j<nroftowns;i++)
		{
			graph[i][j]=-1;
		}
	}
}

void Graph::readFile()
{
	int n=0;
	string city="";
	ifstream in;
	in.open("citys.txt");

	in>>n;
	this->nroftowns=n;
	cout<<nroftowns;
	this->Towns=new string[nroftowns];
	for(int i=0;i<n;i++)
	{
		getline(in,city);		
		Towns[i]=city;	
	}
	in.close();


}


trying to initilize the objekt in the tesfile by typing

Graph g1;

g1.readFile();

Seems to be some error alocating memory..

initialize these before using them.

1
2
3
int nroftowns;
int row;
int col;


A grabage value for nroftowns will crash this line: this->Towns=new string[nroftowns]; in the constructor.

There is memory leak on line 47.
Yes,you were right,cheers!
Changed the code abit,program runs then crashes.
moved the line to the constructors this->Towns=new string[nroftowns]; från the fileread function,have initialized every member declared under private,have no clue whats causing it,something with memory alocation is my guess.

header:
1
2
3
4
5
6
private:
int nroftowns;
int row;
int col;
string*Towns;
int**graph;


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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Graph::Graph()
{
	this->nroftowns=0;
	this->row=0;
	this->col=0;
	this->Towns=new string[nroftowns];
	this->graph=new int*[nroftowns];
	for(int i=0;i<nroftowns;i++)
	{
		this->graph[i]=new int[nroftowns];
	}
	
	for(int i=0;i<nroftowns;i++)
	{
		for(int j=0;j<nroftowns;i++)
		{
			graph[i][j]=-1;
		}
	}
}
Graph::Graph(string cities[], int n)
{
	this->nroftowns=0;
	this->row=0;
	this->col=0;
	this->Towns=new string[nroftowns];
	this->graph=new int*[nroftowns];
	for(int i=0;i<nroftowns;i++)
	{
		this->graph[i]=new int[nroftowns];
	}
	
	for(int i=0;i<nroftowns;i++)
	{
		for(int j=0;j<nroftowns;i++)
		{
			graph[i][j]=-1;
		}
	}
}

void Graph::readFile()
{

	int n=0;
	int distance=0;
	string city="";
	string from="";
	string to="";
	ifstream in;
	in.open("citys.txt");

	in>>n;
	this->nroftowns=n;    // is this line correct? have tried to put a value greater than 0 for the variable in the constructor,no luck
	for(int i=0;i<nroftowns;i++)
	{
		cout<<"----"<<endl;
		in>>city;
		cout<<city<<endl;
		this->Towns[i]=city;	
	}
	in.close();


	ifstream in2;
	in2.open("citys2.txt");
	for(int i=0;i<nroftowns;i++)
	{
		in>>from;
		in>>to;
		in>>distance;
		this->addEdge(from,to,distance);
	}
	in2.close();
	cout<<"END OF FUNCTION"<<endl;
}
Last edited on

This is going to crash. In your constructor, your nroftowns=0, which means no memory is allocated. Now in readFile(), you are doing:

this->Towns[i]=city;

as there is no memory allocated to Towns, it is going to crash. I suggest, read the correct values in the constructor only, make proper initialization of nroftowns, and than allocate correct memory and than work with Towns and other pointers.

Also, whenever you work with pointers, first check them if they are null before dereferencing them. With pointers you should always be sure that the index you are trying to access is actually available.

Edit:

so it should be something like this in constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
ifstream in;
in.open("citys.txt");

in>>n;
this->nroftowns=n;  //see if you are getting correct value for n
//close the file here if its not going to be used further

//rest of the constructor
this->Towns=new string[nroftowns];
	this->graph=new int*[nroftowns];
	for(int i=0;i<nroftowns;i++)
	{
		this->graph[i]=new int[nroftowns];
	}
	
	for(int i=0;i<nroftowns;i++)
	{
		for(int j=0;j<nroftowns;i++)
		{
			graph[i][j]=-1;
		}
	}


Last edited on
Topic archived. No new replies allowed.