gpt4 book ai didi

c - 关于我的发送的问题 - 动态分配

转载 作者:行者123 更新时间:2023-11-30 18:45:29 25 4
gpt4 key购买 nike

我正在研究有关动态分配的学生项目,我认为我的发送有问题,有人可以帮忙吗?

void buildBoard(int*** mat, int size);
void initMat(int*** mat, int size);

int main()
{
int size;
int** mat;
printf("Please enter a size of the matrix:\n");
scanf("%d", &size);
buildBoard(&mat, size);
initMat(&mat, size);
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}

void buildBoard(int*** mat, int size)
{
*mat = (int**)malloc(size * sizeof(int*));
if (*mat == NULL)
{
printf("Bye\n");
exit(1);
}
for (int i = 0; i < size; ++i)
{
mat[i] = (int*)malloc(size * sizeof(int));
if (mat[i] == NULL)
{
printf("Bye\n");
free(mat[i]);
}
}
}

void initMat(int*** mat, int size)
{
int num;
printf("Please enter a numbers:\n");
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
scanf("%d", &mat[i][j]);
}
}
}

在主函数中,我想检查我的分配,但打印时它总是崩溃。

最佳答案

您错过了多次取消引用mat,我的意思是某些mat必须是(*mat):

void buildBoard(int*** mat, int size)
{
*mat = (int**)malloc(size * sizeof(int*));
if (*mat == NULL)
{
printf("Bye\n");
exit(1);
}
for (int i = 0; i < size; ++i)
{
(*mat)[i] = (int*)malloc(size * sizeof(int));
if ((*mat)[i] == NULL)
{
printf("Bye\n");
exit(1);
}
}
}

void initMat(int*** mat, int size)
{
printf("Please enter a numbers:\n");
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
scanf("%d", &(*mat)[i][j]);
}
}
}

我还删除了无用的变量num

编译和执行:

pi@raspberrypi:/tmp $ gcc -g -pedantic -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
Please enter a size of the matrix:
2
Please enter a numbers:
1
2
3
4
1 2
3 4

valgrind 下:

pi@raspberrypi:/tmp $ valgrind ./a.out
==4232== Memcheck, a memory error detector
==4232== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4232== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4232== Command: ./a.out
==4232==
Please enter a size of the matrix:
2
Please enter a numbers:
1
2
3
4
1 2
3 4
==4232==
==4232== HEAP SUMMARY:
==4232== in use at exit: 24 bytes in 3 blocks
==4232== total heap usage: 5 allocs, 2 frees, 2,072 bytes allocated
==4232==
==4232== LEAK SUMMARY:
==4232== definitely lost: 8 bytes in 1 blocks
==4232== indirectly lost: 16 bytes in 2 blocks
==4232== possibly lost: 0 bytes in 0 blocks
==4232== still reachable: 0 bytes in 0 blocks
==4232== suppressed: 0 bytes in 0 blocks
==4232== Rerun with --leak-check=full to see details of leaked memory
==4232==
==4232== For counts of detected and suppressed errors, rerun with: -v
==4232== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
pi@

当然valgrind表示内存泄漏,因为您没有释放分配的内存

<小时/>

注意,将变量的地址赋予initMat是没有用的,可以是:

void initMat(int** mat, int size)
{
printf("Please enter a numbers:\n");
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
scanf("%d", &mat[i][j]);
}
}
}

当然也改变了它的声明和main中的调用

<小时/>

之前我还删除了您在以下位置的免费:

    if (mat[i] == NULL)
{
printf("Bye\n");
free(mat[i]);
}

因为事实上你释放了 NULL

关于c - 关于我的发送的问题 - 动态分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54532554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com