release free pointer memory error: CRT detected that the application wrote to memory after end of heap buffer

When win32 program debugging, malloc to allocate memory, when the last free release memory error, the original code is as follows:

Here, after calling malloc memory allocation finished, pstr first address to 0x00c5abb0, copy this address to the memory window, track memory, as follows:

pstr assigned byte size is 32 bytes, where 32 bytes are not initialized set 0xcd, 4 th 0xfd behind Microsoft C ++ debugging stack with the value of the flag "no man's land" sentry position, where I understood to be used to identify the boundaries of memory, refer to the information in this article:

http://blog.sina.com.cn/s/blog_95bf1ccc010154uu.html

Then, after commissioning the next step, after _stprintf_s function runs, we discovered the problem:

Here, the red represents the memory changes, 0xfd has also been changed to 0xfe, memory that is out of line; a closer look at the formatted data is written _stprintf_s function strings in Unicode version of the prototype is _swprintf_s, in MSDN on, the unit of the second parameter is the number of characters instead of bytes:

After the clear problem, modify the second parameter passed to _stprintf_s maximum number of characters, as follows:

Published 69 original articles · won praise 10 · views 30000 +

Guess you like

Origin blog.csdn.net/u010096608/article/details/103844067