C++中一个0xC0000005访问冲突问题

C++中一个0xC0000005访问冲突问题

 

冯.诺依曼结构CPU(如i386,ARM A8,A9)的保护模式或者哈佛结构(如8051, ARM M0,.. M3)的CPU下,C++编译器将放置常量的内存设置为只读模式或者放入只读内存中,如果出现往常量内存中写入数据就会产生访问冲突异常。

举例,反转字符串:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "stdio.h"
#include "string.h"
 
//反转字符串
char* ReverseStr(char* str, int len)
{
char* header= str;
char* tail= str + len -1;
char temp;//中间变量反转字符
while(header<tail)
{
  temp = *header;
  *header= *tail;
  *tail= temp; 
  ++header;
  --tailer;
}
return str;
}
 
int main()
{
char* s = "abcde";
printf("反转后为:%s ",Reverse_Str(s, strlen(s)));
getchar();
return 0;
}

上面的程序看似没有错误,

运行时

在*header=*tail; 以及 *tail=temp 行会抛出异常://xxxx.exe 中的 0xxxxxxxxx 处未处理的异常: 0xC0000005: 写入位置 0xxxxxxxx 时发生访问冲突

问题出在变量声明: char *s="abcde";

这样定义字符串变量s,s指针是保存在堆上,但字符串“abcde”是一个常量,它是保存在常量区的,被写保护了,在反转函数中,*header= *tail,是更改常量内存的值,被写保护,所以会报内存写错误

如果把"abcde"定义到栈或者全局变量就不存在此问题了。

char s[20];  strcpy(s, "abcde");
原文地址:https://www.cnblogs.com/zys871841072/p/3387589.html