简体   繁体   English

C程序中的分段错误(核心已转储)

[英]Segmentation fault (core dumped) in C program

I have the following program: 我有以下程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 255

char * decrypt(char *p, int key){
    char *tmp;
    for(int i = 0; p[i] != '\0'; i++){
        tmp[i] = p[i]-key;
    }
    return tmp;
}

int main(void){
    printf("Hallo Welt!");
    printf("%s\n", decrypt("ibmmp", 1));
    return EXIT_SUCCESS;
}

When I compile it with gcc -Wall i get the Warning tmp could get uninitialized in this function [-Wmaybe-uninitialized] tmp[i] = p[i]-key (translated from german) and segmentation fault (core dumped) ./crypto when i run it 当我用gcc -Wall编译它时,我得到警告tmp could get uninitialized in this function [-Wmaybe-uninitialized] tmp[i] = p[i]-key (德语翻译)和分段错误(core dumped)。我运行加密货币

What is causing that error? 是什么导致该错误?

I know this quesion has been asked many times, but i could not fix this warning, because other people had different sourcecodes and i couldn't adapt it to my problem. 我知道这个问题已经被问过很多次了,但是我无法解决这个警告,因为其他人有不同的源代码,并且我无法适应我的问题。

You need to allocate 'tmp' and then, keeping with good 'c' coding, check that the allocation was successful. 您需要分配“ tmp”,然后使用良好的“ c”编码,检查分配是否成功。 I assume you have MAX defined so you can set an upper-bound on the length of your string, so I use that below. 我假设您已定义MAX,因此可以在字符串的长度上设置上限,因此我在下面使用它。 If MAX is intended to be the number of characters without a null, then you need to 'malloc(MAX +1)'. 如果将MAX设置为带null的字符数,则需要'malloc(MAX +1)'。 If it is intended to include NULL, then just leave the code as defined below. 如果打算包含NULL,则只需保留下面定义的代码即可。 You also want to decide what to return on failure of the malloc. 您还想确定在malloc失败时返回什么。 I return NULL, but you may want to do something different depending on your needs. 我返回NULL,但是根据您的需要,您可能需要做一些不同的事情。

Also be aware, that this function is returning allocated memory, so someone needs to free it so you aren't leaking memory. 另请注意,该函数正在返回分配的内存,因此有人需要释放它,以免泄漏内存。

char * decrypt(char *p, int key){
    char *tmp;
    tmp = (char *) malloc(MAX);
    if(!tmp)
        return NULL;
    for(int i = 0; p[i] != '\0'; i++){
        tmp[i] = p[i]-key;
    }
    return tmp;
} 

Allocate memory for tmp before using it. 在使用tmp之前,先为其分配内存。 Make sure you null-terminate the string before returning it. 返回之前,请确保对字符串进行空终止。

// Make the input a const string.
// char * decrypt(char *p, int key){
char * decrypt(char const* p, int key){
    char *tmp = malloc(strlen(p) + 1); // Allocate memory
    int i = 0;
    for( ; p[i] != '\0'; i++){
        tmp[i] = p[i]-key;
    }
    tmp[i] = '\0';                     // null terminate.
    return tmp;
}

Make sure you deallocate the memory. 确保取消分配内存。 Just using 只是使用

printf("%s\n", decrypt("ibmmp", 1));

will result in a memory leak. 会导致内存泄漏。

int main(void){
    printf("Hallo Welt!");
    char* dec = decrypt("ibmmp", 1)
    printf("%s\n", dec);
    free(dec);                        // Deallocate memory.
    return EXIT_SUCCESS;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM