为什么EXC\u访问错误,无法访问内存?

为什么EXC\u访问错误,无法访问内存?,c,C,我的程序有问题。我正在用一个伪alfabeth做一个简单的编码程序,但是当试图访问char数组时,它无法访问位置,为什么?我不知道这是否与大小类型而不是int类型有关 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <ctype.h> static char * createAlfa(void); char *

我的程序有问题。我正在用一个伪alfabeth做一个简单的编码程序,但是当试图访问char数组时,它无法访问位置,为什么?我不知道这是否与大小类型而不是int类型有关

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

static char * createAlfa(void);
char * codify(char *mess, char *alfa);

int
main(void)
{
    char *alfa=createAlfa();
    printf("write your mess to codify: \n");
    char mess[]="";
    scanf("%s", mess);
    char *code=codify(mess, alfa);
    printf("your codified message is: %s\n", code);
    return 0;
}

static char *
createAlfa(void)
{
    char *codealfa=malloc(sizeof(char)*27);
    srand((unsigned int)time(NULL));
    int i, ran;
    for(i=0; i<26; i++)
    {
        codealfa[i]='A'+i;
        if((ran=rand()%26)<i)
        {
            codealfa[i]=codealfa[ran];
            codealfa[ran]='A'+i;
        }
    }
    codealfa[26]=0;
    return codealfa;
}

char *
codify(char *mess, char *alfa)
{
    size_t len=strlen(mess);
    char *code=malloc(sizeof(char)*len);
    int i;
    for(i=0; i<len; i++)
    {
        int pos=(int)(toupper(mess[i])-'A');  //pos is behaving correctly, first loop is 
                                              //it is 15 when i write "potato"
        code[i]=alfa[pos];      //EXC_BAD_ACCESS, Could not access memory
    }
    code[i]=0;
    return code;
}
#包括
#包括
#包括
#包括
#包括
静态字符*createAlfa(void);
char*编码(char*mess,char*alfa);
int
主(空)
{
char*alfa=createAlfa();
printf(“将您的信息写入代码:\n”);
char mess[]=“”;
scanf(“%s”,mess);
字符*代码=编码(mess,alfa);
printf(“您的编码消息是:%s\n”,代码);
返回0;
}
静态字符*
createAlfa(无效)
{
char*codealfa=malloc(sizeof(char)*27);
srand((无符号整数)时间(NULL));
int i,ran;

对于(i=0;i您将
mess
声明为:

char mess[]="";
使其大小等于
1
,以容纳NUL字符。接下来,您将扫描该数组中的输入:

scanf("%s", mess);
因为没有足够的空间,这将不起作用


要解决此问题,您需要声明大小正确的
mess
:比您要存储在其中的最大字符数长度多一个。

您将
mess
声明为:

char mess[]="";
使其大小等于
1
,以容纳NUL字符。接下来,您将扫描该数组中的输入:

scanf("%s", mess);
因为没有足够的空间,这将不起作用


要解决此问题,您需要声明大小正确的
mess
:比要存储在其中的最大字符数长度多一个。

我确实认为问题是

char mess[]="";
没有为要扫描的字符串分配内存

换成

char mess[MAX_LENGTH];
mess[0] = 0;

还要注意的是,使用scanf时不会限制输入的长度,请参见

我确实认为问题在于

char mess[]="";
没有为要扫描的字符串分配内存

换成

char mess[MAX_LENGTH];
mess[0] = 0;

还要注意的是,使用scanf时不会限制输入的长度,请参见

当我在Mac OS X上的
valgrind
下运行代码时,我会得到以下输出:

$ valgrind excbadacc
==80786== Memcheck, a memory error detector
==80786== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==80786== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==80786== Command: excbadacc
==80786== 
write your mess to codify: 
absintheabelones
==80786== Invalid write of size 1
==80786==    at 0x100000D1B: codify (excbadacc.c:53)
==80786==  Address 0x100007210 is 0 bytes after a block of size 16 alloc'd
==80786==    at 0xB823: malloc (vg_replace_malloc.c:266)
==80786==    by 0x100000CC5: codify (excbadacc.c:45)
==80786== 
==80786== Invalid read of size 1
==80786==    at 0xC894: strlen (mc_replace_strmem.c:398)
==80786==    by 0x1748C2: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==80786==    by 0x17318D: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==80786==    by 0x17C2CF: printf (in /usr/lib/system/libsystem_c.dylib)
==80786==    by 0x100000DFA: main (excbadacc.c:18)
==80786==  Address 0x100007210 is 0 bytes after a block of size 16 alloc'd
==80786==    at 0xB823: malloc (vg_replace_malloc.c:266)
==80786==    by 0x100000CC5: codify (excbadacc.c:45)
==80786== 
your codified message is: AQBRSKHDAQDPZSDB
==80786== 
==80786== HEAP SUMMARY:
==80786==     in use at exit: 10,330 bytes in 36 blocks
==80786==   total heap usage: 36 allocs, 0 frees, 10,330 bytes allocated
==80786== 
==80786== LEAK SUMMARY:
==80786==    definitely lost: 43 bytes in 2 blocks
==80786==    indirectly lost: 0 bytes in 0 blocks
==80786==      possibly lost: 0 bytes in 0 blocks
==80786==    still reachable: 10,287 bytes in 34 blocks
==80786==         suppressed: 0 bytes in 0 blocks
==80786== Rerun with --leak-check=full to see details of leaked memory
==80786== 
==80786== For counts of detected and suppressed errors, rerun with: -v
==80786== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 1 from 1)
$
一个问题是您在
codify()
中出现了一个off by One错误:

size_t len=strlen(mess);
char *code=malloc(sizeof(char)*len);
您还需要添加1来为null分配足够的空间


警告:在一次运行中,我输入了“马铃薯”并获得了“编码”输出马铃薯;这不是一个很好的加密。

当我在Mac OS X上的
valgrind
下运行代码时,我得到了以下输出:

$ valgrind excbadacc
==80786== Memcheck, a memory error detector
==80786== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==80786== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==80786== Command: excbadacc
==80786== 
write your mess to codify: 
absintheabelones
==80786== Invalid write of size 1
==80786==    at 0x100000D1B: codify (excbadacc.c:53)
==80786==  Address 0x100007210 is 0 bytes after a block of size 16 alloc'd
==80786==    at 0xB823: malloc (vg_replace_malloc.c:266)
==80786==    by 0x100000CC5: codify (excbadacc.c:45)
==80786== 
==80786== Invalid read of size 1
==80786==    at 0xC894: strlen (mc_replace_strmem.c:398)
==80786==    by 0x1748C2: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==80786==    by 0x17318D: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==80786==    by 0x17C2CF: printf (in /usr/lib/system/libsystem_c.dylib)
==80786==    by 0x100000DFA: main (excbadacc.c:18)
==80786==  Address 0x100007210 is 0 bytes after a block of size 16 alloc'd
==80786==    at 0xB823: malloc (vg_replace_malloc.c:266)
==80786==    by 0x100000CC5: codify (excbadacc.c:45)
==80786== 
your codified message is: AQBRSKHDAQDPZSDB
==80786== 
==80786== HEAP SUMMARY:
==80786==     in use at exit: 10,330 bytes in 36 blocks
==80786==   total heap usage: 36 allocs, 0 frees, 10,330 bytes allocated
==80786== 
==80786== LEAK SUMMARY:
==80786==    definitely lost: 43 bytes in 2 blocks
==80786==    indirectly lost: 0 bytes in 0 blocks
==80786==      possibly lost: 0 bytes in 0 blocks
==80786==    still reachable: 10,287 bytes in 34 blocks
==80786==         suppressed: 0 bytes in 0 blocks
==80786== Rerun with --leak-check=full to see details of leaked memory
==80786== 
==80786== For counts of detected and suppressed errors, rerun with: -v
==80786== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 1 from 1)
$
一个问题是您在
codify()
中出现了一个off by One错误:

size_t len=strlen(mess);
char *code=malloc(sizeof(char)*len);
您还需要添加1来为null分配足够的空间


警告:在一次运行中,我输入了“potato”并得到了“coded”输出potato;这不是一个很好的加密。

发生错误时,您是否写出了I和pos?提示:当循环退出时,
I
的值是多少?是否在您使用
malloc
创建的数组的范围内?您在:
s处有一个off by-by-one错误ize_t len=strlen(mess);char*code=malloc(sizeof(char)*len);
in
codify()
。您也需要添加1来为空值分配足够的空间。警告:我输入了
potato
,并获得了一次“编码”输出potato;这不是一个很好的加密。发生错误时您是否写出了I和pos?提示:当循环退出时
I
的值是多少?这是否在您要处理的数组的范围内使用
malloc进行de
?在
codify()中有一个off-by-one错误:
size\t len=strlen(mess);char*code=malloc(sizeof(char)*len);
。您也需要添加1来为空值分配足够的空间。警告:我输入了
potato
,有一次得到了“编码”输出的potato;这不是一个很好的加密。+1,但这不是唯一的bug…+1,但这不是唯一的bug…而且我发现
mass
的大小也有问题。这是有限制的甚至
valgrind
都能帮你做些什么。@JonathanLeffler我已经解决了这个问题,但还是要感谢你的意见,我只能投赞成票。.我还没有学会valgrind,但我用C语言编程的越多,了解这个工具就越重要。.我发现
混乱的大小也有问题。即使是
valgrind
对你的帮助也是有限的。@JonathanLeffler我已经解决了这个问题,但还是要感谢你的意见,我只能投票赞成..我还没有学会valgrind,但是我用C编程越多,了解这个工具就越重要。。