0

I'm writing a parser for a large file and one of my functions responsible for reading from the input file has a char buffer called peek. Basically, as main repeatedly calls this function, peek is eventually getting over-written with some odd values. Here's the function that's being called by main. bufferAsInt:

void bufferAsInt(ifstream &inf, int &i)
{
    char peek[3];
    inf.read(peek, 3);
    i = atoi(peek);

    //I'm not using the >> operator to read an int because the int is just
    //3 chars long in the input file and two consecutive integer values can
    //be written like this: 123456 for 123 and 456.
}

I found that as I wrote these values to an output file, when reading an int value that was only two digits long, the third digit (or some other number) would be left over in the char buffer peek and the value would be written incorrectly to the output file (this only happened after reading a very very large amount of data from the input file.) So after tens of thousands of iterations, when reading a number like 15, the value that would get written to my output file might have been something like 156.

To solve the problem I changed my implementation of bufferAsInt to this:

void bufferAsInt(ifstream &inf, int &i)
{
    char *peek = new char[3];
    inf.read(peek, 3);
    i = atoi(peek);
    delete [] peek;
}

(Of course I was guessing at what the issue was). What I'd like to know is if the fact that my problem was solved is some sort of weird consequence of declaring this char buffer on the heap or if the issue actually was that my program was running out of stack memory.

I have 6GB of RAM in my computer and at the time of running, no other programs would have been using enough memory to cause this issue to the best of my knowledge.

3
  • Possible duplicate
    – eerorika
    Feb 11, 2014 at 16:01
  • Who calls bufferAsInt ? And how do you call the next one?
    – Marco A.
    Feb 11, 2014 at 16:04
  • Even though you have 6GB of RAM the stack is probably only a few MB.
    – Borgleader
    Feb 11, 2014 at 16:07

3 Answers 3

2

You're off by one.

atoi expects a null-terminated string. So a three-digit number needs a char[4] to be properly stored. Also, read doesn't put a null on the end.

Try this:

void bufferAsInt(ifstream &inf, int &i)
{
    char peek[4];
    inf.read(peek, 3);
    peek[3] = 0;
    i = atoi(peek);
}
1
  • Yeah, looks like we all saw it at about the same time. Feb 11, 2014 at 16:11
2

atoi() expects a C 'NUL terminated string' as input, that is, ASCII characters followed by an ASCII zero byte. That's the only way the function knows where to stop converting.

In your first code listing, you read three bytes into a three byte buffer, but you have no control over the byte that follows in memory. I believe that's undefined behavior in C++, so literally anything can happen. Typically, though, if the following byte happens to be a zero or a non-digit, the string will convert properly; if it happens to be a digit, you get a different number when you were expecting.

The proper fix is to use your first example, but:

char peek[4];      // 4 char buffer instead of 3
inf.read(peek, 3);
peek[3] = '\0';    // ensure the 4th char is zero
i = atoi(peek);
0

Most likely the only thing that changed was that the new, with your compiler and options, zeroes the array.

To guarantee that you could write

char *peek = new char[3]();

But the dynamic allocation serves no purpose, so instead do it like this:

char peek[3] = {};

Note: if the file contains 3 digits, then you should instead use four digits array, in order to have room for terminating zero.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.