c - C中的动态数组push()导致Valgrind错误

标签 c dynamic memory-leaks valgrind dynamic-memory-allocation

我正在尝试用 C 语言创建一个具有三个输入参数的函数。动态数组如果是整数,则其长度和整数个数。该函数将动态数组的大小增加一个元素,并将整数(参数)作为新元素放在其末尾。有一个非常简单的代码,可以工作,但会在 Valgrind 中生成内存泄漏错误。

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

void pushINT(int*arr,int*size,int data) {
    int*tmp=realloc(arr,(*size+1)*sizeof(int));
    if(tmp==NULL)exit(100);
    else {
        arr[*size]=data;
        (*size)=*size+1;
    }
}

int main() {
    int* array=malloc(0);
    int arraySIZ=0;

    for(int i=0;i<10;i++) pushINT(array,&arraySIZ,i);
    for(int i=0;i<arraySIZ;i++) printf("%d",array[i]);
    printf("\n");

    free(array);
    return 0;
}

Valgrind 输出:

==19581== Memcheck, a memory error detector
==19581== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==19581== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==19581== Command: /home/filip/Documents/b3b36PRG/HW05/main.c
==19581== 
/home/filip/Documents/b3b36PRG/HW05/main.c: line 4: syntax error near unexpected token `('
/home/filip/Documents/b3b36PRG/HW05/main.c: line 4: `void pushINT(int*arr,int*size,int data) {'
==19581== 
==19581== HEAP SUMMARY:
==19581==     in use at exit: 83,410 bytes in 2,151 blocks
==19581==   total heap usage: 3,812 allocs, 1,661 frees, 145,949 bytes allocated
==19581== 
==19581== 109 (32 direct, 77 indirect) bytes in 1 blocks are definitely lost in loss record 619 of 690
==19581==    at 0x483880B: malloc (vg_replace_malloc.c:309)
==19581==    by 0x192341: xmalloc (in /usr/bin/bash)
==19581==    by 0x146EEE: make_bare_simple_command (in /usr/bin/bash)
==19581==    by 0x146FC5: make_simple_command (in /usr/bin/bash)
==19581==    by 0x141C5B: yyparse (in /usr/bin/bash)
==19581==    by 0x1380C9: parse_command (in /usr/bin/bash)
==19581==    by 0x1381D7: read_command (in /usr/bin/bash)
==19581==    by 0x13845F: reader_loop (in /usr/bin/bash)
==19581==    by 0x136B68: main (in /usr/bin/bash)
==19581== 
==19581== LEAK SUMMARY:
==19581==    definitely lost: 32 bytes in 1 blocks
==19581==    indirectly lost: 77 bytes in 5 blocks
==19581==      possibly lost: 0 bytes in 0 blocks
==19581==    still reachable: 83,301 bytes in 2,145 blocks
==19581==         suppressed: 0 bytes in 0 blocks
==19581== Reachable blocks (those to which a pointer was found) are not shown.
==19581== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==19581== 
==19581== For counts of detected and suppressed errors, rerun with: -v
==19581== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

什么原因导致这个问题?我怀疑 realloc()。

最佳答案

该函数应该是:

void pushINT(int **arr,int *size,int data) {
    int *tmp=realloc(*arr,(*size+1)*sizeof(int));
    if (tmp==NULL) exit(100);
    *arr= tmp;
    tmp[*size]=data;
    (*size)=*size+1;
}

请注意,它现在接收一个双指针,并记住 realloc 可以更改内存位置。因此有必要更新调用者的指针,这就是它需要双指针的原因。

<小时/> 或者正如 Jonathan Leffler 所建议的那样:

int *pushINT(int *arr,int *size,int data) {
    int *tmp=realloc(arr,(*size+1)*sizeof(int));
    if (tmp==NULL) exit(100);
    tmp[*size]=data;
    (*size)=*size+1;
    return tmp;
}

关于c - C中的动态数组push()导致Valgrind错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54886214/

相关文章:

javascript - 处理 svg <image> 时的内存管理

python - 重复 os.path.isdir 调用中的巨大内存泄漏?

c - Geany 没有给出 c 语言输出

c - 发出中断时不会调用中断处理程序

c - 识别gtk中滚动条的向上/向下移动

将字符串从 argv 转换为无符号字节数组

c - 为什么你会想要在堆上有一个数组?

memory - iOS 6,崩溃并出现内存不足警告

c++ - 动态数组 C++ 多项式类

javascript - 我在创建动态表时遇到问题?