Nathan Porter Nathan Porter - 24 days ago 6
C++ Question

Function is causing a segmentation fault only when you press enter or it reaches the end of a file

This function is a work in progress and is designed to read data from stdin for a simple integer calculator. The function works as intended when entering an operator and two operands: + 2 2. It also correctly reads from a file with similar formatting piped in on the command line. Despite this it will segfualt if the user hits enter on the command line or it reaches the end of a file. I'm not sure where I've gone wrong? What could be causing this behavior?

void process_input ()
{

while(!std::cin.eof())
{
std::string line;
std::string n;


getline(std::cin, line);


if (std::cin.fail())
break;
else if (line == "quit")
return;

std::istringstream stream(line);
std::vector<std::string> input_cpy(0);
while (stream >> n)
input_cpy.push_back(n);

//Clear global_input_cpy on each iteration and load the new line into it
global_input_cpy.clear();
for (int i = 0; i < input_cpy.size(); ++i)
global_input_cpy.push_back(input_cpy[i]);



if(input_cpy[0] == "+")
sum(std::stol(input_cpy[1]), std::stol(input_cpy[2]));
else if (input_cpy[0] == "*")
multiply(std::stol(input_cpy[1]), std::stol(input_cpy[2]));
else if (input_cpy[0] == "^")
exponent(std::stol(input_cpy[1]), std::stol(input_cpy[2]));
else
std::cout << "input '" << input_cpy[0] << "'" << " unrecognized. skipping." << "\n";

input_cpy.clear();
}
}

Answer Source

If no strings are read and therefore input_cpy is empty, you still try to access input_cpy[0] and this produces undefined behavior.

You need an assertion somewhere that input_cpy.size() >= 2 in order for these statements to have defined behavior:

if(input_cpy[0] == "+")
    sum(std::stol(input_cpy[1]), std::stol(input_cpy[2]));
else if (input_cpy[0] == "*")
    multiply(std::stol(input_cpy[1]), std::stol(input_cpy[2]));
else if (input_cpy[0] == "^")
    exponent(std::stol(input_cpy[1]), std::stol(input_cpy[2]));