Misoperation: releasing the space on the stack
Error:
Cause:
From the content in the box, we can see that expression: _ block_type_is_valid (phead-> nblockuse) indicates that the memory block type is incorrect. Under what circumstances will this problem occur? For more information, see the following.Code:
Void test (vector <int *> * vecptr) {If (vecptr! = NULL) {int nsize = vecptr-> size (); For (INT nindex = 0; nindex <nsize; ++ nindex) {Delete vecptr-> at (nindex ); vecptr-> at (nindex) = NULL;} Delete vecptr; vecptr = NULL ;}}
Call code:
Vector <int *> VEC; int * PTR = NULL; For (INT nindex = 0; nindex <5; nindex ++) {PTR = new int; * PTR = nindex; VEC. push_back (PTR); PTR = NULL;} test (& VEC );
We can see that the VEC variable is applied on the stack and is temporary. When the test function is passed in, the pointer in the vector is released in sequence. This is no problem, but in the last step, we deleted the vector, and this error occurs. the reason is obvious: delete can only delete the memory on the stack, and the memory on the stack does not need to be manually released. Therefore,ProgramThe memory block type is incorrect.