Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning deleting array, in load file function

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;
}
like image 354
nomatchland Avatar asked Jan 25 '13 00:01

nomatchland


1 Answers

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.

like image 166
Greg Hewgill Avatar answered Sep 23 '22 10:09

Greg Hewgill