我做了一个简单的程序来找出用户输入的md5校验和(输入[1; 6]) 问题是这些for循环似乎占用了我所有的RAM + Swap资源,导致pc挂起。我做错了什么?管理系统内存的更好方法是什么?
/*
C Example w/o mpi
mpicc md5.c -o md5 -lcrypto -lssl
./md5
Single process on c2d laptop
String finding matchin md5 of string "hello" tooks
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>
#if defined(__APPLE__)
# define COMMON_DIGEST_FOR_OPENSSL
# include <CommonCrypto/CommonDigest.h>
# define SHA1 CC_SHA1
#else
# include <openssl/md5.h>
#endif
char *str2md5(const char *str, int length) {
int n;
MD5_CTX c;
unsigned char digest[16];
char *out = (char*)malloc(33);
MD5_Init(&c);
while (length > 0) {
if (length > 512) {
MD5_Update(&c, str, 512);
} else {
MD5_Update(&c, str, length);
}
length -= 512;
str += 512;
}
MD5_Final(digest, &c);
for (n = 0; n < 16; ++n) {
snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int)digest[n]);
}
return out;
}
int main (int argc, char* argv[])
{
#ifdef COUNT // Very bad name, not long enough, too general, etc..
static int const count = COUNT;
#else
static int const count = 6; // default value
#endif
clock_t begin, end;
double time_spent;
begin = clock();
int bflag = 0;
int sflag = 0;
int index;
int c;
char input[count];
char action[2]; // char + \n
char *inputResult = (char*)malloc(33);
char *tmpResult = (char*)malloc(33);
char inputGuess[6];
int i,j,k,l,m,n;
char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
printf("Please enter string to guess (max 6 char - latin letters and numbers): ");
scanf("%s", input);
inputResult = str2md5(input, strlen(input));
printf("Md5 to find = %s \n", inputResult);
printf("Continue? (Y/n) ");
scanf("%s", action);
if(action == "n\n"){
return 0;
}
/* for 1 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
char guess1[] = {letters[i], '\0'};
/* printf("%s\n", guess1);*/
if(strcmp(str2md5(guess1, strlen(guess1)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess1);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
/* for 2 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
for(j=0; j<sizeof(letters); j++){
char guess2[] = {letters[i], letters[j], '\0'};
if(strcmp(str2md5(guess2, strlen(guess2)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess2);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}
/* for 3 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
for(j=0; j<sizeof(letters); j++){
for(k=0; k<sizeof(letters); k++){
char guess3[] = {letters[i], letters[j], letters[k], '\0'};
/* printf("%s\n", guess3);*/
if(strcmp(str2md5(guess3, strlen(guess3)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess3);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}
}
/* for 4 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
for(j=0; j<sizeof(letters); j++){
for(k=0; k<sizeof(letters); k++){
for(m=0; m<sizeof(letters); m++){
char guess4[] = {letters[i], letters[j], letters[k], letters[m], '\0'};
/* printf("%s\n", guess4);*/
if(strcmp(str2md5(guess4, strlen(guess4)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess4);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}
}
}
/* for 5 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
for(j=0; j<sizeof(letters); j++){
for(k=0; k<sizeof(letters); k++){
for(m=0; m<sizeof(letters); m++){
for(n=0; n<sizeof(letters); n++){
char guess5[] = {letters[i], letters[j], letters[k], letters[m], letters[n], '\0'};
/* printf("%s\n", guess5);*/
if(strcmp(str2md5(guess5, strlen(guess5)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess5);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}
}
}
}
/* for 6 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
for(j=0; j<sizeof(letters); j++){
for(k=0; k<sizeof(letters); k++){
for(m=0; m<sizeof(letters); m++){
for(n=0; n<sizeof(letters); n++){
for(l=0; l<sizeof(letters); l++){
char guess6[] = {letters[i], letters[j], letters[k], letters[m], letters[n], letters[l], '\0'};
/* printf("%s\n", guess6);*/
if(strcmp(str2md5(guess6, strlen(guess6)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess6);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}
}
}
}
}
return 0;
}
答案 0 :(得分:2)
不检查整个代码:您至少有一个malloc
没有对应的free
。
str2md5
为返回的out
分配33个字节。 strcmp(str2md5(guess3, strlen(guess3)),inputResult)
形式有几行,其中malloc
out
产生的指针是临时的,丢失的,因此永远不会再被释放。
考虑到您在多个嵌套循环中调用这些行,这可能会导致您的问题。
通过将str2md5
的结果分配给本地指针并在执行strcmp
后释放它来避免此问题。这里是前面提到的一行的例子:
char* str2md5_ret = str2md5(guess3, strlen(guess3));
int cmp = strcmp(str2md5_ret, inputResult);
free(str2md5_ret);
if(cmp == 0)
//...
再看一下代码,我注意到其他一些问题:
您在声明时分配inputResult
,但随后您将str2md5
的新数据块分配给它。所以又有33个字节泄露。然后你分配tmpResult
,它没有被释放,实际上从未再次使用过。这又是33个字节泄露。如果没有循环中断,则不会释放inputResult
。
正如其他人所指出的那样,深层嵌套的循环是糟糕的风格。实际上,您可以使用单个循环在单个递归函数中编写所有这些函数。这可能更具可读性,可调试性,可维护性和灵活性。
您还可以通过为所有malloc
调用的输出保留一个长度为33个字节的char*
并将其作为参数传递而不是返回它来删除几乎所有str2md5