1

我的程序有问题。我用一个虚拟的 alfabeth 制作了一个简单的编码程序,但是在尝试访问 char 数组时它无法访问该位置,怎么会?我不知道它是否与 size_t 类型而不是 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;
}
4

3 回答 3

3

您声明mess为:

char mess[]="";

这使得它的大小等于1, 来保存 NUL 字符。接下来,您正在扫描该数组中的输入:

scanf("%s", mess);

这不起作用,因为没有足够的空间。

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

于 2012-11-21T05:09:40.100 回答
3

我相信问题是

char mess[]="";

没有为要扫描的字符串分配内存。

将其替换为

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

另请注意,您使用的 scanf 不会限制输入的长度,请参阅如何防止 scanf 在 C 中导致缓冲区溢出?

于 2012-11-21T05:10:44.790 回答
3

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

$ 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()

size_t len=strlen(mess);
char *code=malloc(sizeof(char)*len);

您还需要添加 1 来为 null 分配足够的空间。

警告:一次运行,我输入了“土豆”并得到了“编码”输出 POTATO;这不是一个很好的加密。

于 2012-11-21T05:16:45.817 回答