本地指针可能指向释放的内存

问题描述 投票:0回答:1
#include <stdio.h>
#include <malloc.h>

int wordLen(char *c_str) {
    int len = 0;
    for (int i = 0; c_str[i] != '\0'; i++) {
        len++;
    }
    printf("%s has a total of %d characters!\n", c_str, len);
    return len;
}

char *allocWord(char *c_str) {
    int size_needed = wordLen(c_str) + 1;
    char *heap_str = malloc(sizeof(char) * size_needed);

    heap_str[(size_needed - 1)] = '\0';

    for (int i = 0; i < (size_needed - 1); i++) {
        heap_str[i] = c_str[i];
    }
    printf("Allocated space for the word %s!", heap_str);
    return heap_str;
}

char *reverseWord(char *src_str) {
    char *reverse = allocWord(src_str);
    int str_len = wordLen(reverse);

    int right = str_len - 1;

    for (int left = 0; left < right; left++) {
        char tmp = reverse[left];
        reverse[left] = reverse[right];
        reverse[right] = tmp;
        right--;
    }
    printf("The reverse of %s, is %s\n", src_str, reverse);
    return reverse;
}

void wordCompare(char *first_str, char *second_str) {
    int len = wordLen(first_str);
    if (len != wordLen(second_str)) {
        printf("Word lengths don't match!\n");
        return;
    }

    for (int i = 0; i < len; i++) {
        if (first_str[i] != second_str[i]) {
            printf("Strings don't match starting from the character %d which is %c\n",
                   i + 1, first_str[i]);
            return;
        }
    }
    printf("Strings are identical\n");
}

int main() {
    char *str1 = allocWord("Palindrome");
    char *reverse = reverseWord(str1);

    char *str2 = allocWord("racecar");
    char *reverse2 = reverseWord(str2);

    wordCompare(str1, reverse);
    printf("\n\n\n");
    wordCompare(str2, reverse2);

    free(str2);
    free(reverse2);
    printf("HERE: %s\n", reverse);
    free(str1);
    free(reverse);
    
    return 0;
}

代码很简单,我模仿了

strlen
函数来查找字长,还有另一个函数为字符串分配一些内存并返回堆上的指针。

然后我有一个反转字符串的函数和一个模仿

strcmp
的函数来检查单词本身和反转是否相同。代码工作正常。

问题是我的 IDE 警告我,在我解除分配

reverse2
变量后,这可能意味着
reverse
变量也无法访问。我不明白为什么会这样,因为我分别为每个方法调用方法并将它们保存在不同的变量中。

问题

Local variable 'reverse' may point to deallocated memory 
显示在第75行是
printf("HERE: %s\n", reverse);

c malloc dynamic-memory-allocation clion
1个回答
0
投票

代码中存在一些问题,但没有一个应该产生警告

Local variable 'reverse' may point to deallocated memory
。您可能正在编译该程序的旧版本吗?

尽管如此,你应该解决这些问题:

  • 头文件

    <malloc.h>
    是非标准的。使用
    <stdlib.h>
    代替。

  • wordLen
    allocWord
    wordCompare
    的字符串参数应定义为
    const char *
    以记录这些字符串未被函数修改,并允许在更严格的警告级别下传递字符串文字而不发出警告。

  • 您可能想使用

    size_t
    作为字符串长度并测试潜在的内存分配失败。

这里是修改版:

#include <stdio.h>
#include <stdlib.h>

size_t wordLen(const char *c_str) {
    size_t len = 0;
    for (int i = 0; c_str[i] != '\0'; i++) {
        len++;
    }
    printf("%s has a total of %zu characters!\n", c_str, len);
    return len;
}

char *allocWord(const char *c_str) {
    size_t len = wordLen(c_str);
    char *heap_str = malloc(sizeof(char) * (len + 1));

    if (heap_str == NULL) {
        fprintf(stderr, "cannot allocate memory for %s\n", c_str);
        exit(1);
    }
    for (size_t i = 0; i < len; i++) {
        heap_str[i] = c_str[i];
    }
    heap_str[len] = '\0';

    printf("Allocated space for the word %s\n", heap_str);
    return heap_str;
}

char *reverseWord(const char *src_str) {
    char *reverse = allocWord(src_str);
    size_t str_len = wordLen(reverse);
    size_t right = str_len;

    for (size_t left = 0; left < right; left++) {
        char tmp = reverse[left];
        reverse[left] = reverse[--right];
        reverse[right] = tmp;
    }
    printf("The reverse of %s is %s\n", src_str, reverse);
    return reverse;
}

int wordCompare(const char *first_str, const char *second_str) {
    size_t len = wordLen(first_str);
    if (len != wordLen(second_str)) {
        printf("Word lengths don't match!\n");
        return 0;
    }

    for (size_t i = 0; i < len; i++) {
        if (first_str[i] != second_str[i]) {
            printf("Strings don't match starting from the character %zu which is %c <-> %c\n",
                   i + 1, first_str[i], second_str[i]);
            return 0;
        }
    }
    printf("Strings are identical\n");
    return 1;
}

int main() {
    char *str1 = allocWord("Palindrome");
    char *reverse1 = reverseWord(str1);
    char *str2 = allocWord("racecar");
    char *reverse2 = reverseWord(str2);

    wordCompare(str1, reverse1);
    wordCompare(str2, reverse2);

    free(str2);
    free(reverse2);
    printf("HERE: %s\n", reverse1);
    free(str1);
    free(reverse1);

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.