[英]Using a pointer and did not use `free()` and no memory leaks
在我的程序中,我使用了malloc()
和指针,但我没有使用free()
来释放那些 memory,我已经使用-fsanitize=address
标志进行编译,但它说没有 memory 泄漏。 据我所知,如果我分配 memory,我还必须在程序结束时释放 memory,否则会出现 memory 泄漏。
#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
编译并运行了它
怎么没有 memory 泄漏? 我错过了什么吗?
我第一次使用wsl (debian 10)
和fsanitize=address
编译并运行时未能检测到 memory 泄漏
但是当我用Arch 5.19
编译和运行时,它检测到DEADLYSIGNAL
。 为什么会这样?
编译器不仅报告泄漏(如果程序设法避免 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
。 没有必要间接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.