Seg fault on overloaded << operator

Mar 25, 2015 at 1:10am
I'm getting a segmentation fault on overloaded '<<' operator, however, if I run my program with a command line option (reading a file specified on the command line), the program works just fine... However, if I enter the file manually it will seg fault.

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
#include <iostream>
#include <fstream>
using namespace std;

class Cell {
private:
	bool readOnly;
	int value;
public:
	Cell(int value, bool readOnly) {
		this->value = value;
		this->readOnly = readOnly;
	}

	bool isReadOnly() {return readOnly;}
	int getValue() const {return value;}
	void setValue(int value) {if (!readOnly) this->value = value;}
//	ostream& operator << (ostream& out, const Cell &rhs);
};

ostream& operator << (ostream &out, const Cell &rhs) {
	out << " " << rhs.getValue() << " ";
	
	return out;
}

void getFilename(char* filename) {
	cout << "Enter source file: ";
	cin >> filename;
}

int main(int argc, char **argv) {
	char* filename;
        if (argc == 2)
            filename == argv[1];
        else
            getFilename(filename);
	
	//Board *board = new Board(filename);

	Cell cell(0, true);
	cout << cell << endl;
	//delete board;
	return 0;
}
Mar 25, 2015 at 1:13am
which line? or all of them?
Mar 25, 2015 at 1:24am
Specifically the "cout << cell << endl;"
Mar 25, 2015 at 1:26am
can you paste the exact error message here?
Mar 25, 2015 at 1:27am
ill be back tomorrow ill help you then.
Mar 25, 2015 at 1:28am
I've told you already what the exact error message is. I run the program, I enter a file name in, it pops out "Segmentation Fault" and the program exits. Using the debugger isn't useful here in this case as it's not really telling me what's going on other than a seg fault occured and it exited.
Mar 25, 2015 at 1:29am
char* is a pointer, it is not a string.

A pointer has to point to something.

On line 33, you create a pointer named filename. You do not initialize it, so it points to random garbage memory. On line 37, you pass that pointer to getFilename, which then attempts to read string data to that address, effectively corrupting the heap and likely causing a program crash.



The easy solution here: don't use pointers until you understand what they are and what they do. If you want to use strings, use std::string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    string filename;
    if(argc == 2)
        filename = argv[1];  // <- note: = assignment, not == comparison
    else
        filename = getFilename();  // <- output by return value

//....

string getFilename()
{
    cout << "Enter source file: ";
    string filename;
    cin >> filename;
    return filename;
}
Mar 25, 2015 at 1:43am
What you said makes sense Disch, and I thought I had a decent understanding of pointers (although they can be screwy at times). I didn't realize I had done what I did there, but it makes sense. So it wasn't my extraction operator, it was a function using essentially an uninitialized variable (I dynamically allocated it, it should be good to go now). Thanks!
Mar 25, 2015 at 1:54am
(I dynamically allocated it, it should be good to go now).


I still strongly recommend you use strings instead of dynamically allocating your own char arrays.

There is little reason to use char arrays in C++. Especially with iostream.

EDIT:

In fact, I'm almost certain that you made some other kind of program-breaking mistake with dynamic allocation. I can already see some pitfalls you may have missed with how your existing code is written.
Last edited on Mar 25, 2015 at 1:56am
Topic archived. No new replies allowed.