-2

I am getting "crt detected that the application wrote to memory after end of heap buffer" when deleting my char source[]

Size and array value are read from file, size_orig is initialized before creating source[] even if file input is wrong

I deleted from code anything that is not connected with initializing and deleting source[]

int main()
{
    
    int size_new = 26;

    std::ifstream in;
    std::string OrigFile;
    std::cout << "FILENAME\n";
    std::cin >> OrigFile;

    in.open(OrigFile);

    if (!in)
    {
        std::cout << "FILE COULDN'T BE OPENED\n";
        exit(-1);
    }

    int size_orig = 0;
    in >> size_orig;

    if (!in)
    {
        std::cout << "SIZE IS NOT A NUMBER";
        in.close();
        exit(-1);
    }

    char* source = new char[size_orig];


    while (!in.eof())
    {
        in >> source;
        for (int i = 0; i < size_orig; i++)
        {
            if( !(isalpha(source[i])))
            {
                std::cout << "ERROR_TEXT";
                delete[] source;
                in.close();
                exit(-1);
            }
        }

    }
    
    delete[] source;
    return 0;
}

After researching I initialized size_orig before reading its value from file, didn't help

Max
  • 3
  • 2
  • 3
    in.eof() is wrong – Neil Butterworth Nov 04 '22 at 22:46
  • https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – pm100 Nov 04 '22 at 22:48
  • Aside: strongly suggest writing error messages to `std::cerr` rather than `std::cout`. – Chris Nov 04 '22 at 22:52
  • `size_orig` probably doesn't account for NUL terminator. What are the contents of the file? – Igor Tandetnik Nov 04 '22 at 22:52
  • Use `in.read(source, size_orig);` instread of the `>>` operator. This way you can be sure the amount of data read does not exceed the array size. There may be content in the file that the size stored in the input file doesn't take into account. – fabian Nov 04 '22 at 22:53
  • if you are interested in the mysteries of eof, here is something i wrote earlier: https://latedev.wordpress.com/2012/12/04/all-about-eof/ – Neil Butterworth Nov 04 '22 at 22:57

1 Answers1

0
in >> size_orig;

This reads some integer value from the file.

char* source = new char[size_orig];

And this value determines how much memory is allocated for source.

in >> source;

And then the next string gets read from the file into source. If the size of the string, including the null terminator that >> adds to it, does not exceed size_orig, you're home free.

But, obviously, if it doesn't, you're in deep trouble, you'll end up corrupting memory, resulting in undefined behavior. And the error message that you are reporting is one common manifestation of this undefined behavior, and a crash.

C++'s >> operator, here, will read whatever's in the file, no matter how big the next string in the file is. If size_orig was 5, and the next word in the file was abracadabra, well, you're up the creek without a paddle.

This is the explanation for your crash.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148