View Full Version : istream::getline segmentation fault (x86-64)
Dr. Kylstein
December 20th, 2008, 09:39 PM
The following code consistently results in a segfault. Removing the getline statement removes the error.
#include <iostream>
#include <fstream>
int main(int argc, char **argv) {
std::ifstream stream;
stream.open("ayb2.txt");
char* line;
stream.getline(line, 256);
stream.close();
return 0;
}
lensman3
December 21st, 2008, 11:05 PM
The following code consistently results in a segfault. Removing the getline statement removes the error.
#include <iostream>
#include <fstream>
int main(int argc, char **argv) {
std::ifstream stream;
stream.open("ayb2.txt");
// char* line;
You need to allocate memory for line. char* line; just indicates that it will be pointer to the line but no memory is allocated for the data. Since the next line reads 256 bytes of char memory, then you need to allocate at least 257 bytes (+1 for end of string /0).
stream.getline(line, 256);
stream.close();
return 0;
}
You need to allocate memory for line. char* line; just indicates that it will be pointer to the line but no memory is allocated for the data. Since the next line reads 256 bytes of char memory, then you need to allocate at least 257 bytes (+1 for end of string /0).
Dr. Kylstein
December 22nd, 2008, 01:57 PM
I didn't realize I was that out of touch. I'm glad it it's me and not the libraries.
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.