我的程序有问题。我正在用一个虚拟的alfabeth做一个简单的codifying-prog,但是当试图访问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;
}
答案 0 :(得分:3)
您将mess
声明为:
char mess[]="";
使其大小等于1
,以保存NUL字符。接下来,您将扫描该数组中的输入:
scanf("%s", mess);
因为没有足够的空间而无法使用。
要解决此问题,您需要声明mess
的大小正确:比您打算在其中存储的最大字符数的长度多一个。
答案 1 :(得分:3)
我确实认为问题是
char mess[]="";
没有为要扫描的字符串分配内存。
将其替换为
char mess[MAX_LENGTH];
mess[0] = 0;
另外请注意,使用scanf时不会限制输入的长度,请参阅How to prevent scanf causing a buffer overflow in C?
答案 2 :(得分:3)
当我在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()
处出现了一个错误:
size_t len=strlen(mess);
char *code=malloc(sizeof(char)*len);
你需要加1来为null分配足够的空间。
警告:一次运行,我输入'马铃薯'并获得'编纂'输出马铃薯;这不是一个很好的加密。