【问题标题】:Heap has been corrupted after a certain size malloc In C在 C 中一定大小的 malloc 后堆已损坏
【发布时间】:2021-09-23 14:50:20
【问题描述】:

我试图用 C 语言编写一个非常简单的压缩器,但在我的代码中遇到了错误“堆已损坏”。我调查了一下,错误似乎是因为这行:

ptr = (char*)malloc(count * sizeof(char));

当我将大小从 count 更改为 1000 时,它可以工作,我尝试调试并查看某处是否存在差异但我找不到它,我知道可能存在某种溢出但我不明白为什么以及是什么解决方案,而不是仅仅写一个大数字来修复它

这是我现在的代码:

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

errno_t err;
int count =0;

struct node {
    int data;
    struct node* left;
    struct node* right;
};

struct node* newNode(int data) {
        struct node* node = (struct node*)malloc(sizeof(struct node));

        node->data = data;

        node->left = NULL;
        node->right = NULL;
        return (node);
    };

int* frequency(char* str) {

    FILE* fptr;
    err = fopen_s(&fptr, str, "r");
    if (err != 0)
    {
        printf("The file wasn't opened\n");
        exit(0);
    }

    int* ptr;
    ptr = (int*)malloc(95 * sizeof(int));

    if (ptr == NULL) {
        printf("Error! memory not allocated.");
        exit(0);
    }

    for (int i = 0; i < 95; i++) {
        *(ptr + i) = 0;
    }
    char ch;
    int index;

    while ((ch = fgetc(fptr)) != EOF) {
        index = (int)ch - 32;
        (*(ptr+index))++;
    }

    err = fclose(fptr);
    if (err != 0)
    {
        printf("The file wasn't closed\n");
        exit(0);
    }

    for (int i = 0; i < 95; i++) {
        printf("%d ", *(ptr+i));
    }
    return ptr;
}

void* letFrequency(int* occLet) {
    for (int i = 0; i < 95; i++) // counts how many actually occur 
    {
        if (*(occLet+i) > 0)
        {
            count++;
        }
    }

    int* ptr;
    ptr = (char*)malloc(count * sizeof(char));
    if (ptr == NULL) {
        printf("Error! memory not allocated.");
        exit(0);
    }

    int max = 0;
    int placement = 0;

    for (int j = 0; j < count; j++) {
        for (int i = 0; i < 95; i++) {
            if (*(occLet+i) >= max)
            {
                max = *(occLet+i);
                placement = i;
            }
        }
        *(ptr+j) = (char)(placement + 32);
        printf("%c", *(ptr +j));
        *(occLet+placement) = 0;
        max = 1;
    }
    return ptr;
}

void binaryMap(char* letFrq) {
    struct node* rootzero = newNode(1);
    struct node* rootone = newNode(0);

    int leaveszero = 0;
    int leavesone = 0;

    if (count % 2 == 0) {
        leavesone = count / 2;
        leaveszero = count / 2;
    }
    else
    {
        leavesone = count / 2;
        leaveszero = count / 2 + 1;
        printf("%d", leaveszero);
    }

}


int main() {
    char str[70];
    printf("Enter the name of the text file you want to compress: ");
    scanf_s("%s", str, sizeof(str));
    int* ptrFr;
    char* ptrLetFr;
    ptrFr = frequency(str);
    ptrLetFr = letFrequency(ptrFr);
    free(ptrFr);
    binaryMap(ptrLetFr);
}

【问题讨论】:

  • 为什么 int*ptr 不是 char*ptr
  • 在 C 中不要强制转换 malloc() 的返回值是 void* 所以没有必要。 sizeof(char) 定义为 1 - 所以这也是不必要的。但是ptr = malloc( count * sizeof(*ptr) ) ; 是个好主意,因为它允许prt 的类型更改最少的维护——避免类型错误。
  • 而你读取的文件只包含值> 32和
  • 是的,尤其是因为 ey 使用的是 *(ptr+j),它将跳转 4 个字节而不是 1 个字节,因为它是 int*
  • @MohamedAkram 谢谢!我什至没有注意到,它解决了这个问题:)

标签: c heap-memory heap-corruption


【解决方案1】:

线条;

    int* ptr;
    ptr = (char*)malloc( count * sizeof(char));

显然是错误的。如果您打算使用count 整数,那么sizeof(char) 将分配太小的块。我建议作为一个习惯

  • 不要投射void* - 只需分配即可。
  • 使用sizeof指向的对象,而不是显式类型。
  • 不要留下不必要的悬空指针。

为此:

    int* ptr = malloc(count * sizeof(*ptr) ) ;

或者如果它是_打算是char*,那么:

    char* ptr = malloc(count * sizeof(*ptr) ) ;

请注意更改是多么微小 - 您不必在三个不同的地方进行更正。

【讨论】:

  • 它应该是一个 char* ,但感谢您的提示!代码有效:)
猜你喜欢
  • 2019-01-22
  • 2020-01-15
  • 1970-01-01
  • 1970-01-01
  • 2018-01-23
  • 2020-08-02
  • 1970-01-01
  • 2020-10-26
  • 2011-06-04
相关资源
最近更新 更多