程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在 C 中一定大小的 malloc 后堆已损坏大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在 C 中一定大小的 malloc 后堆已损坏?

开发过程中遇到在 C 中一定大小的 malloc 后堆已损坏的问题如何解决?下面主要结合日常开发的经验,给出你关于在 C 中一定大小的 malloc 后堆已损坏的解决方法建议,希望对你解决在 C 中一定大小的 malloc 后堆已损坏有所启发或帮助;

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

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

当我将大小从计数更改为 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",sizeof(str));
    int* ptrFr;
    char* ptrLetFr;
    ptrFr = frequency(str);
    ptrLetFr = letFrequency(ptrFr);
    free(ptrFr);
    binarymap(ptrLetFr);
}

解决方法

线条;

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

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

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

为此:

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

或者如果 _intended 为 char* 那么:

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

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

大佬总结

以上是大佬教程为你收集整理的在 C 中一定大小的 malloc 后堆已损坏全部内容,希望文章能够帮你解决在 C 中一定大小的 malloc 后堆已损坏所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。