在C中检测到堆损坏内存泄漏

在C中检测到堆损坏内存泄漏,c,C,我得到了一个错误,它说 调试评估失败,检测到热损坏 在我的程序中一切都很好,但我犯了那个错误。内存泄漏在哪里?我必须释放主内存,因为我的函数需要返回指针 我的代码: #include <stdio.h> #include <stdlib.h> #include <string.h> int *dynamic_reader(unsigned int n) { /*making new array and checking if allocation

我得到了一个错误,它说

调试评估失败,检测到热损坏

在我的程序中一切都很好,但我犯了那个错误。内存泄漏在哪里?我必须释放主内存,因为我的函数需要返回指针

我的代码:

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

int *dynamic_reader(unsigned int n) {

    /*making new array and checking if allocation succeeded*/
    int *mem;
    mem = malloc(n * sizeof(int));
    if (!mem) {
        printf("Memory allocation failed\n");
        exit(-1);
    }

    /*letting the user to input the values for the array*/
    int i = 0;
    while (i < n) {
        scanf("\n%d", &mem[i]);
        i++;
    }

    /*printing the array just to make sure everything is good*/
    for (int j = 0; j < n; j++) {
        printf("%d  ", mem[j]);
    }

    return mem;
}
int *insert_into_array(int *arr, unsigned int num, int newval) {

    /*making new bigger array*/
    int *newarr = realloc(arr, (num + 1) * sizeof(int));

    /*adding the integer to this new array */
    newarr[num] = newval;
    printf("\n");

    /*printing to make sure everything is correct*/
    for (int j = 0; j < num + 1; j++) {
        printf("%d  ", newarr[j]);
    }
    return newarr;
}
int main(void) {
    /*In dynamic_reader function I need to make an array which size is given as a parameter*/
    /*In this case I choosed 3*/
    int *arr = dynamic_reader(3);
    int num = 3;
    /*In insert_into_array function I need to add one integer to this array I made in dynamic_reader*/
    /*The parameters are the array, the number of elements in the array already done and the integer I want to add*/
    int *c = insert_into_array(arr, num, 9);

    /*I free the memory here because I need to return the pointers of these arrays in the function so it cant be done there*/
    free(arr);
    free(c);
}
#包括
#包括
#包括
int*动态_读取器(无符号int n){
/*创建新数组并检查分配是否成功*/
int*mem;
mem=malloc(n*sizeof(int));
如果(!mem){
printf(“内存分配失败\n”);
出口(-1);
}
/*允许用户输入数组的值*/
int i=0;
而(i
您正在双重释放内存。检查文档中是否有错误。Realloc将1)扩展传递的缓冲区,或2)分配新缓冲区、复制数据并释放原始缓冲区。当您这样做时:

free(arr);
free(c);
您正在双重释放一个值,该值曾经被realloc释放,或者已经被第一个
free(arr)


此外,您应该检查realloc是否失败(返回NULL),如果失败,请适当处理。

您正在双重释放内存。检查文档中是否有错误。Realloc将1)扩展传递的缓冲区,或2)分配新缓冲区、复制数据并释放原始缓冲区。当您这样做时:

free(arr);
free(c);
您正在双重释放一个值,该值曾经被realloc释放,或者已经被第一个
free(arr)


此外,您应该检查
realloc
是否失败(返回NULL),如果失败,则应适当处理该情况。

首先创建
malloc
数组,将其作为
arr
返回主函数。然后在另一个函数中,
realloc
其中
arr
是realloc的一个参数,但其他一些指针存储结果。根据realloc中发生的情况,您可能会看到
arr
newarr
指向同一位置,或者
newarr
指向有效位置,而
arr
指向已释放的无效位置。无论哪种方式,在最后释放这两个数组都是一个问题。

首先,您需要
malloc
一个数组,将其作为
arr
返回到主函数。然后在另一个函数中,
realloc
其中
arr
是realloc的一个参数,但其他一些指针存储结果。根据realloc中发生的情况,您可能会看到
arr
newarr
指向同一位置,或者
newarr
指向有效位置,而
arr
指向已释放的无效位置。无论哪种方式,在最后释放它们都是一个问题。

无需
释放(arr)
,当您
realloc()
释放它时,就会考虑到这一点。
realloc()
返回的指针将指向包含原始内存的内存,或者在将旧内存的内容复制到新的、更大的内存块后释放旧内存。

无需
释放(arr)
,这在
realloc()
它时会得到处理。
realloc()
返回的指针将指向包含原始内存的内存,或者在将旧内存的内容复制到新的更大内存块后释放旧内存。

您知道是否存在内存泄漏吗?运行程序时是否查看了系统内存?该消息中没有任何内容表明内存泄漏。它说堆腐败。腐败不是泄密。在调用free()之前所做的某些操作使内存无效。使用调试器找出代码的错误所在。
int*newarr=realloc(arr,(num+1)*sizeof(int))
这看起来很糟糕。一旦执行
realloc(arr,
,指针
arr
就不再有效,因此无法释放它。使用realloc()的通常方法是重用同一指针,即
arr=realloc(arr,…
。同样值得一提的是,您通常不希望每次添加另一个元素时都不断地重新分配数组。当元素的数量超过数组的长度时,通常会将数组的大小增加到以前的两倍,从而将昂贵的内存操作的频率降至最低。您知道是否存在emory leak?运行程序时是否查看了系统内存?该消息中没有任何内容表示内存泄漏。它表示堆损坏。损坏不是泄漏。调用free()之前所做的操作使内存无效。使用调试器找出代码的错误。
int*newarr=realloc(arr,(num+1)*sizeof(int));
这看起来很糟糕。一旦你做了
realloc(