What does this error mean? double free or corruption
Hey ~ everyone~
What does this error mean? *** glibc detected *** double free or corruption (!prev): It occurs when i tried to write to a file like this : Write2DArrayInt(tcumulus, ncolumns,nrows, out); Write2DArrayInt is a function like this: void Write2DArrayInt(short int **Array, int Columns, int Rows, FILE *fp) { int i; for(i=0; i<Rows; i++){ fwrite(Array[i], sizeof(short int),Columns, fp); } fclose(fp); } And tcumulus is defined as : short **tcumulus |
You provide a FILE* to Write2DArrayInt and it closes the file, but does your code try to close the same file somewhere else? That's generally how that error happens in your situation.
ta0kira |
You can disable this by disabling malloc check. use this command:
$export MALLOC_CHECK_=0 And then run your program. But you may receive some strange bugs. try to fix the code. |
Though the error is annoying, disabling the message isn't a good solution. As far as I know, this never comes up when it isn't true.
ta0kira |
Using code tags in the forum will help us (and you) read these sorts of things more easily.
Since compilers differ widely in their initialization of declared variables, it is always good practice to initialize them, and to always check and/or assign them when they are used. For example: Code:
FILE *fp = NULL; -- when you declare a variable, initialize it (or, at least, initialize it as early as possible) -- before you use a potentially NULL or otherwise uninitialized variable, check its state -- when you perform an action that may leave the variable in an undetermined state, initialize it to a known state This is always good programming practice and will save you aggravation and confusion. |
Quote:
1) You are calling fclose two times in a row. Closing file in a function where it was passed as parameter isn't a good idea. You should close files in functions where they were opened. 2) You are freeing previously allocated memory several times. For example: Code:
//in C++ Code:
/*in C*/ Code:
//in C++ Code:
/*in C*/ |
Thank u very much , guys~
|
All times are GMT -5. The time now is 12:53 PM. |