对 2D 数组调用 free() 会产生 seg。过错

发布于 2024-12-25 13:17:05 字数 1362 浏览 1 评论 0原文

我正在尝试学习 C,并且已经达到了这样的地步:我想尝试一些比将文本打印到控制台、以非常简单的方式使用指针等小而简单的示例更高级的东西。

问题是我的程序在调用 free 时崩溃了,我确定是指针在某种程度上困扰着我......

所以,我创建了一个像这样的二维数组:

bool InitializeGrid(struct Grid *g, int rows, int columns)
{
    g->rows = rows;
    g->columns = columns;
    g->grid = malloc(g->rows * sizeof(int *));

    if(g->grid == NULL)
    {
        printf("Could not allocate memory for grid rows.\n");
        return false;
    }

    for (int i = 0; i < g->rows; i++)
    {
        g->grid[i] = malloc(g->columns * sizeof(int));

        if(g->grid[i] == NULL)
        {
            printf("Could not allocate memory for grid columns.\n");
            return false;
        }
    }

    / ... /

    return true;
}

结构 Grid 看起来像这样:

struct Grid
{
    int rows;
    int columns;
    int **grid;
};

然后我在整个程序中成功地使用了网格,一切都运行良好。当程序即将退出时,我需要/想要释放我分配的内存。我就是这样做的:

void CleanupGrid(struct Grid *g)
{
    for(int i = 0; i < g->rows; i++)
        free(g->grid[i]);

    free(g->grid);
}

但是在第一个 free(g->grid[i]) 上,我收到了分段错误。

为什么这不起作用?我该如何解决这个问题?

编辑:

stdbool.h 已包含在程序中以使用 bool/true/false。

我觉得盲目地盯着 free() 语句真的很愚蠢。当我将程序缩小到绝对最小值时,它与 free() 语句配合得很好。当我访问并写入网格时,free() 会向我显示分段错误。

接受用户1083265的回答。

I'm trying to learn C and I've gotten to a point where I felt like trying something more advanced than small and easy examples of printing text to the console, using pointers in a very simple way and so on.

The problem is that my program crashes on my call to free and I'm sure it is the pointer(s) somehow haunting me...

So, I have created a two-dimensional array like this:

bool InitializeGrid(struct Grid *g, int rows, int columns)
{
    g->rows = rows;
    g->columns = columns;
    g->grid = malloc(g->rows * sizeof(int *));

    if(g->grid == NULL)
    {
        printf("Could not allocate memory for grid rows.\n");
        return false;
    }

    for (int i = 0; i < g->rows; i++)
    {
        g->grid[i] = malloc(g->columns * sizeof(int));

        if(g->grid[i] == NULL)
        {
            printf("Could not allocate memory for grid columns.\n");
            return false;
        }
    }

    / ... /

    return true;
}

The structure Grid looks like this:

struct Grid
{
    int rows;
    int columns;
    int **grid;
};

I then successfully use the grid throughout the program and everything works great. When the program is about to exit, I need/want to free the memory I allocated. This is how I do that:

void CleanupGrid(struct Grid *g)
{
    for(int i = 0; i < g->rows; i++)
        free(g->grid[i]);

    free(g->grid);
}

But on the first free(g->grid[i]) I receive a segmentation fault.

Why does this not work? How can I fix this?

Edit:

stdbool.h has been included in the program for use of bool/true/false.

I feel really stupid for staring blind at the free() statement. When I scale down the program to the absolute minimum, it works great with the free() statements. It is when I access and write to the grid at one point that the free() greets me with a segmentation fault.

Accepting user1083265's answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2025-01-01 13:17:05

我编译你的代码(只是分配/释放)并且它不会崩溃。

可能,当使用分配的网格时,您可能会损坏分配的片段上方的一些内存。 Glibc 使用金丝雀值来检测此类损坏- 因此在释放损坏的内存时会出现段错误。

I compile your code (just alloc/dealloc) and it doesn't crash.

Possibly, when using allocated grid, you corrupt some memory above allocated fragments. Glibc uses canary values to detect such corruption - so you get segfault when freeing corrupted memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文