Home > Back-end >  Help to see the delete abnormal problems
Help to see the delete abnormal problems

Time:09-17

The code is as follows:
 # include & lt; Iostream> 
#include

using namespace std;

Int main () {
Char * a=new char [200];

Memcpy (a - 1, "12345678", 8).

The delete [] a;

return 0;
}

Execute to delete statement will be submitted to the "double free or corruption (out) :" error
A - 1 where is wrong, want to know why the delete a will there is a problem, the principle of what kind of?

CodePudding user response:

Memcpy (a - 1, "12345678", 8).
Why is a minus 1??????

CodePudding user response:

reference 1st floor xdayong response:
here memcpy (a - 1, "12345678", 8).
Why is a minus 1??????


I know that there is a problem, only deliberately make a mistake, I want to know why this case is the delete a complains,

CodePudding user response:

Did you operating memory allocated to you

CodePudding user response:

Error because of one line of code that digging a hole on the

CodePudding user response:

reference 3 floor truth is right or wrong response:
did you operations assigned to your memory

That how not memcpy there an error

CodePudding user response:

reference 4 floor met the goddess response:
error because of one line of code that digging a hole on the


I know there is problem, is don't understand why when I was in the delete a quote this fault, a pointer is not moving, or point to the new memory before,

CodePudding user response:

New - & gt; The delete
New [] - & gt; The delete []
,
Delete some of the things, to do or,
Delete and delete [] do or may be not the same,

Normal operation is not an error, but luck eight fault,
An error on the recognition or recognition

CodePudding user response:

reference response: 7th floor thousand dream life
new - & gt; The delete
New [] - & gt; The delete []
,
Delete some of the things, to do or,
Delete and delete [] do or may be not the same,

Normal operation is not an error, but luck eight fault,
An error on the recognition or recognize

As for your normal delete this method, and I thought for a while

CodePudding user response:

reference response: 7th floor thousand dream life
new - & gt; The delete
New [] - & gt; The delete []
,
Delete some of the things, to do or,
Delete and delete [] do or may be not the same,

Normal operation is not an error, but luck eight fault,
An error on the recognition or recognize


refer to the eighth floor response: thousand dreams life
Quote: reference response: 7th floor thousand dream life

New - & gt; The delete
New [] - & gt; The delete []
,
Delete some of the things, to do or,
Delete and delete [] do or may be not the same,

Normal operation is not an error, but luck eight fault,
An error on the recognition or recognize

As for your normal delete this method, and I thought for a while


Ah, made a mistake, when I was in front of the fart,

CodePudding user response:

reference 5 floor Heyddo reply:
Quote: refer to the third floor truth is right or wrong response:

Did you operations assigned to your memory

What about how not memcpy there error

Write unallocated address, which belongs to the undefined behavior, specific how to operate, it is Microsoft's own definition, you can understand, so write without detection, and deleted when tested, so he called the exception, had better not use undefined write code, unless you can accurately know him.

CodePudding user response:

refer to 6th floor Heyddo response:
Quote: refer to 4th floor met the goddess response:

Error because of one line of code that digging a hole on the


I know there is problem, just don't quite understand why when I was in the delete a quote this fault, a pointer is not moving, or point to new memory, before the
stack overflow error,

CodePudding user response:

reference 11 floor met the goddess response:
Quote: refer to the sixth floor Heyddo response:
Quote: refer to 4th floor met the goddess response:

Error because of one line of code that digging a hole on the


I know there is problem, just don't quite understand why when I was in the delete a report of the fault, a pointer is not moving, or point to new memory, before the
stack overflow error,

Excuse me delete failure principle is what? I understand that I did not move a pointer

CodePudding user response:

Because after the new, the actual distribution of memory is larger than you require allocation of memory,
How to delete, it can know exactly how much memory to delete off?
That's because the new, he will leave at first that a memory, used to store about is assigned to you to use the memory information
And return to you is the first period of memory memory address,
That you have the memory of a - 1 copy function leads to the storage memory information memory is covered by other information, result in unable to delete or delete error,
The address of a storage memory information because content covered by you didn't, so delete would be an error

CodePudding user response:

When using p=malloc allocation of memory, the memory manager need to record the memory information, most of the memory manager to put these information stored in several bytes in front of the p, memcpy destroyed these information, cause the failure of the subsequent free,

CodePudding user response:

Don't delete, can try free

CodePudding user response:

Stack overflow problem

CodePudding user response:

A - 1,,, not your memory you also in, also released, the collapse of one of the good

CodePudding user response:

To test error, the original poster is of enough

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related