gcc 使用指针,没有使用'free()',没有内存泄漏

hfyxw5xn  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(142)

在我的程序中,我使用了malloc()和指针,但我没有使用free()来释放这些内存,我用-fsanitize=address标志编译,但它说没有内存泄漏。据我所知,如果我分配内存,我也必须在程序结束时释放内存,否则会有内存泄漏。

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

void print2D(int rowSize, int **cols_size, int **arr) {

    for(int i = 0; i < rowSize; i++) {
        for(int j = 0; j < (*cols_size)[i]; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
}

int** make2D(int rowSize, int **colSize) {

    int** arr = (int**) malloc(sizeof(int*) * rowSize);

    for(int i=0; i < rowSize; i++) {
        arr[i] = (int*) malloc(sizeof(int) * 2);
    }

    *colSize = (int*) malloc(sizeof(int*) * 2);

    (*colSize)[0] = 2;
    (*colSize)[1] = 2;

    arr[0][0] = 1;
    arr[0][1] = 2;

    arr[1][0] = 3;
    arr[1][1] = 4;

    return arr;
}

int main() {

    int **colSize;
    int rowSize = 2;

    int** arr = make2D(rowSize, colSize);

    print2D(rowSize, colSize, arr);

    return 0;
}

我用gcc -o wow -fsanitize=address wow.c && ./wow编译并运行它
怎么会没有内存泄漏?我是不是漏掉了什么?

更新

我第一次使用wsl (debian 10)fsanitize=address进行编译和运行时,未能检测到内存泄漏
但是当我用Arch 5.19编译和运行时,它检测到了DEADLYSIGNAL

hsvhsicv

hsvhsicv1#

编译器不仅报告泄漏(如果程序设法避免SIGSEGV),而且报告程序表现出未定义的行为。

$ gcc -Wall -Wextra -pedantic -fsanitize=address a.c -o a && ./a
a.c: In function ‘main’:
a.c:42:17: warning: ‘colSize’ is used uninitialized in this function [-Wuninitialized]
   42 |     int** arr = make2D(rowSize, colSize);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~
1 2
3 4

=================================================================
==187==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7fda63d72808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
    #1 0x5594afb0c407 in make2D (/tmp/ikegami/a/a+0x1407)
    #2 0x5594afb0c6d6 in main (/tmp/ikegami/a/a+0x16d6)
    #3 0x7fda63a97082 in __libc_start_main ../csu/libc-start.c:308

Indirect leak of 16 byte(s) in 2 object(s) allocated from:
    #0 0x7fda63d72808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
    #1 0x5594afb0c433 in make2D (/tmp/ikegami/a/a+0x1433)
    #2 0x5594afb0c6d6 in main (/tmp/ikegami/a/a+0x16d6)
    #3 0x7fda63a97082 in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: 32 byte(s) leaked in 3 allocation(s).

要修复未定义的行为,请将

int **colSize;
int rowSize = 2;

int** arr = make2D(rowSize, colSize);

print2D(rowSize, colSize, arr);

int *colSize;
int rowSize = 2;

int** arr = make2D(rowSize, &colSize);

print2D(rowSize, &colSize, arr);

那你就

$ gcc -Wall -Wextra -pedantic -fsanitize=address a.c -o a && ./a
1 2
3 4

=================================================================
==249==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7fd485c86808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
    #1 0x55e3c49a8447 in make2D (/tmp/ikegami/a/a+0x1447)
    #2 0x55e3c49a8797 in main (/tmp/ikegami/a/a+0x1797)
    #3 0x7fd4859ab082 in __libc_start_main ../csu/libc-start.c:308

Direct leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7fd485c86808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
    #1 0x55e3c49a84af in make2D (/tmp/ikegami/a/a+0x14af)
    #2 0x55e3c49a8797 in main (/tmp/ikegami/a/a+0x1797)
    #3 0x7fd4859ab082 in __libc_start_main ../csu/libc-start.c:308

Indirect leak of 16 byte(s) in 2 object(s) allocated from:
    #0 0x7fd485c86808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
    #1 0x55e3c49a8473 in make2D (/tmp/ikegami/a/a+0x1473)
    #2 0x55e3c49a8797 in main (/tmp/ikegami/a/a+0x1797)
    #3 0x7fd4859ab082 in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: 48 byte(s) leaked in 4 allocation(s).

您应该将print2D更改为接受const int *cols_size而不是int **cols_size。不需要间接寻址。

相关问题