映射区域的权限错误 [英] Bad permissions for mapped region
本文介绍了映射区域的权限错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试运行以下函数时出现错误:
I get an error when trying to run the following function:
char* reverseInPlace(char* src)
{
//no need to alloc or free memory
int i=0;
int size=mystrlen(src);
for(i=0;i<size;i++)
{
int j=size-i-1;
if(i<j)
{
char temp;
printf("Interchange start %d:%c with %d:%c",i,src[i],j,src[j]);
temp=src[i];
src[i]=src[j];//error occurs here
src[j]=temp;
printf("Interchange complete %d:%c and %d:%c",i,src[i],j,src[j]);
}
}
return src;
}
我这样称呼这段代码:
char* rev2=reverseInPlace("BeforeSunrise");
printf("The reversed string is %s
",rev2);
错误如下所示:
Interchange start 0:B with 12:e
Process terminating with default action of signal 11 (SIGSEGV)
Bad permissions for mapped region at address 0x401165
为什么会出现这个错误?
Why does this error occur?
推荐答案
你正在向你的函数传递一个常量字符串.
You are passing a constant string to your function.
字符串字面量在 C 中属于 char [N + 1]
类型(其中 N
是数组的长度),但修改它们会导致未定义的行为.您的编译器此时应该已经发出警告.
String literals are of type char [N + 1]
(where N
is the length of the array) in C, but modifying them results in undefined behavior. Your compiler should have already issued a warning at that point.
如果你想修改它,那么你必须创建一个副本:
If you wish to modify it then you have to create a copy:
char str[] = "BeforeSunrise";
char* rev2=reverseInPlace(str);
这篇关于映射区域的权限错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文