Question

I've got troubles loading a file into memory, my compiler warns me about something I do not grasp. What's the matter?

C:\Users\Caroline\Desktop\Prog\literature.cpp:236:15: warning: deleting array 'char chbuffer [(((sizetype)((ssizetype)fsize)) + 1)]' [enabled by default]

function :

bool loadfile(string & buffer, const char fpath[])
{
    ifstream file(fpath, ios::binary);
    if(!file) return false;
    file.seekg(0, ios::end);
    long fsize = file.tellg();
    file.clear();
    file.seekg(0);
    char chbuffer[fsize + 1];
    file.read(chbuffer, fsize);
    buffer = chbuffer;
    delete [] chbuffer;
    return true;
}
Was it helpful?

Solution

You cannot delete an automatically allocated array. Remove the delete [] chbuffer; statement.

In general, delete is only used when paired with new. You could have allocated chbuffer like this:

char *chbuffer = new char[fsize + 1];

in which case you would want to use delete [] chbuffer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top