宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

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

如果在DOS的实模式的编译器,如Turbo C/Borland C++下,可能就不会报错。

举例,反转字符串:

#include "stdio.h"
#include "string.h"

//反转字符串
char* ReverseStrchar* str, int len) 
{
 char* header= str;
 char* tail= str + len -1;
 char temp;//中间变量反转字符
 whileheader<tail)
 {
  temp = *header;
  *header= *tail;
  *tail= temp;  
  ++header;
  --tailer;
 }
 return str;
}

int main)
{
 char* s = "abcde";
 printf"反转后为:%s
",Reverse_Strs, strlens)));
 getchar);
 return 0;
}

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

运行时

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

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

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

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

char s[20]; 
strcpys, "abcde");