O
O
Oleg Tsilyurik2015-12-17 03:50:07
C++ / C#
Oleg Tsilyurik, 2015-12-17 03:50:07

What's wrong with free, delete?

Something like this:

char *sval = (char*)calloc( zn, sizeof( char ) );
for( ... ) {
   // здесь что-то заполняется  эту строку
}
free( (void*)sval );

Mistake:
*** Error in `./period1': free(): invalid next size (fast): 0x09413040 ***
Аварийный останов

Just don't write schoolboy nonsense about "freeing unallocated memory" and so on.
Quite a lot is written about such things (by search): free(): invalid next size (fast) and many more. etc. ... and all after the middle of 2012. and beyond, and C and C++.
Compiler GCC 4.8.4
What can this error message mean?
PS And what is surprising: if there is a void * conversion in free, then sometimes it fails (not always), without conversion - always an error.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Oleg Tsilyurik, 2015-12-17
@Olej

An error was found: in the loop, it climbed (by writing 0) in the string 1 byte further than the length zn, while the length descriptor of the next memory area is overwritten, and when freed, it is impossible to restore (combine) free regions.
And that's why, at different optimization levels (-O0, ... -O3), the error messages are completely different ... which can drive you crazy.

M
Maxim Moseychuk, 2015-12-17
@fshp

// здесь что-то заполняется эту строку
More details here. You could change the pointer inside the loopsval

V
Vladislav Yaroslavlev, 2015-12-18
@vladon

It's best to do this:
And then you will not make a mistake in the loop (it will be found at the compilation stage).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question