I keep getting a glibc detected: double free or corruption error. It runs through the whole thing and then error at the break statement. I think it's cause i'm trying to delete something that I already deleted? Not really sure, what do you think?
case 3:{
double (*wspd10)[lay][ROW][COL]=newdouble [TSTEP][lay][ROW][COL];
int timeIndex=0;
//get variables from netcdf file
NcVar *wspd10Var;
if (!(wspd10Var=dataFile.get_var("WSPD10")))
{cout<<"Could not get pointer to NcVar wind speed at 10m requested."
<<" Common problem: file parameter does not point to a METCRO2D file."<<endl;}
if (!wspd10Var->get(&wspd10[0][0][0][0],TSTEP,lay,ROW,COL))
{cout<<"Could not set netcdf wind speed at 10 m variable to class constructor"<<endl;}
//create data for wind
for (int tstep=0;tstep<TSTEP;tstep++){
if (hour_array[tstep]==12){
for (int lon=0;lon<ROW;lon++){
for (int lat=0;lat<COL;lat++){
data[timeIndex][lon][lat]=wspd10[tstep][0][lon][lat]*36;
}
}
timeIndex++;
}
}
delete [] wspd10;
loaded=true;
}
break;
I posted this not because I want advice on how you (the generic you) would write this code based on one clip of it- I have been doing this awhile now and I'm doing this the most memory efficient way possible (I will eventually have a terabyte of data streaming through this). What I would like help on is the error. Have you gotten it before? what did it mean for you? Also, if you can see why this code is possibly faulting (i.e. trying to delete something that has already been deleted beyond a scope or something along those lines), please let me know.
I did find somewhere that this error means: "the C++ library thinks you did a double free (that is, you freed the same thing twice, which is of course a bug) or that you corrupted its data structures, such as by writing beyond the end of a buffer you allocated."
no- it (this clip of code) is part of a modification member function, hence the modification to data which is created and initialised in the constructor.
Solution: used gdb debugger and found a class object (multidimensional array) was being deleted twice.
@Athar- thanks for suggesting something useful.
@kbw- comments like that are particularly unhelpful. In this case, I don't mind because you really have only seen 20 lines of several thousand & I have enough experience to shrug it off. However, to an inexperienced programmer, remarks like that are condescending and unproductive- may I suggest not saying anything at all if you don't have anything constructive to say?