对于映射区域糟糕权限 [英] Bad permissions for mapped region

查看:137
本文介绍了对于映射区域糟糕权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试运行以下功能时的错误:

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; 
}

我把这code是这样的:

I call this code like this:

char* rev2=reverseInPlace("BeforeSunrise");
printf("The reversed string is %s\n",rev2);

错误看起来是这样的:

The error looks like this:

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.

字符串文字是类型的char [N + 1] (其中 N 是数组的长度)在C,但修改它们在不确定的行为。你的编译器应该在这一点上已经发出了警告。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆