0xC0000005: Solution for access violation when writing location 0x00000000 (memory alignment)

0xC0000005: Solution for access violation when writing location 0x00000000 (memory alignment)

Incidentally, I will summarize the related memory problems :
1) The write location conflicts (caused by memory alignment)
2) This->0xFFFFFFFF memory problem caused by delete links
other words, because the project test at the time forgot to take a screenshot, I can only dictate the memory conflict problem encountered Up.
3) The problem of memcpy_s.

//1)p1没有开辟内存直接memcpy_s或者memcpy_s拷贝后再new
char* p1=NULL;
memcpy_s(p1,size1,p2,size2);
p1 = new char[100000];
//2)p1,p2都有内存,但是在p1的size1大小小于p2的。或者在循环new和delete的时候,不经意把size1和size2改变导致的越界
memcpy_s(p1,size1,p2,size2);

4) The first point of 3 is actually an example of the pointer p1 not being initialized; the second point is an example of a number out of bounds. If you encounter this memory problem in the future, just think about these four points or three points, and you can definitely find out if you are careful .

Foreword: The
memory byte alignment problem of Xiaobai can be seen in this article of mine, which basically solves 99.9% of the interview questions, so that you can better understand memory alignment.
01 Offset Knowledge
02 Memory Alignment
You don’t need to read the first one, of course, it’s best if you have time to strengthen your knowledge.

The following is the situation on the Internet. This is the most basic error condition. You can compare with his according to your own situation:
1):

char *p; 
p = new char[2]; 
delete [] p; 
// always using p.... 
p = xxx; // error

2):

char *p; 
memcpy(p, xxx, number); // error

3):

char *p; 
p = new char[number]; 
delete [] p; 
......... 
delete [] p; // error

In the C++ environment, most of the situation is similar in thinking:
and what I want to say is that I am different from them, and I caused it indirectly. When I encapsulated the C++ class, when the member data has been initialized and assigned, this happens when the code is running. Look at the picture:
Insert picture description here
Insert picture description here
As a result, I have been doing it for almost a day, and I kept printing information. It turned out that when I had initialized, the code was running, and the printed member data was a large negative number. At this time, I thought of affirmation. It is the wrong data fetching, access to the defined memory, or you can understand that the array is out of bounds. As a result of this situation, I immediately had a relative byte alignment problem, and immediately changed the byte alignment to 1, and there was no problem immediately!

#pragma pack(1)

Of course, I also reported the following error when I ran the code. Some people said it was a problem with project properties, some people said it was a problem with new and delete of different modules. I have a memory alignment problem here. After solving it, I haven't reported it. Also say the following, to reduce the distress of everyone encountering this problem! ! !

Invalid address specified to RtlValidateHeap

Guess you like

Origin blog.csdn.net/weixin_44517656/article/details/108672686