【问题标题】:reallocation of a Matrix in c [closed]c中矩阵的重新分配[关闭]
【发布时间】:2016-04-07 10:16:02
【问题描述】:

可能这是一个非常愚蠢的问题,但我看不透,也许你能帮忙?

我的问题是矩阵的重新分配,向它添加 1 列和 1 行,然后用 `INFINITY' 填充新元素。

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

int main(){
    int i, j, size = 3;
    float **mat;

    //Initial allocation
    mat = malloc(size* sizeof(float *));
    for(i = 0; i < size; i++) {
        mat[i] = malloc(size * sizeof(float));
        for (j = 0; j < size; j++)
            mat[i][j] = 1;
    }

    //Print initial matrix
    for(i=0; i<size; i++) {
        for (j = 0; j < size; j++)
            printf("%f ", mat[i][j]);
        puts("\n");
    }

    //I'm going to add a row and a column
    void *pointer = realloc(mat, (size+1)*sizeof(float*));
    if(pointer != NULL) {
        mat = pointer;
        mat[size] = malloc((size+1)*sizeof(float));
    }else
        fprintf(stderr, "Error: allocation");

    for(i = 0; i <= size; i++) {
        mat[i][size] = 0;
        mat[size][i] = 0;
    }

    //Print altered matrix
    for(i=0; i<=size; i++) {
        for (j = 0; j <= size; j++)
            printf("%f ", mat[i][j]);
        puts("\n");
    }

    //Here comes the free
    for (i = 0; i < size+1; i++){
        free(mat[i]);  // <-- Debug says SIGTRAP
    }
    free(mat);
    return 0;
}

提前感谢您的帮助。

编辑:我注意到只有在调试时才会出现该错误,而不是在正常运行时。我的 IDE 是 Clion。

【问题讨论】:

  • 您没有向任何内容添加列。您正在添加一行,并使该行的一个元素比其他行长(假设它们的长度为size)。
  • 您还没有添加额外的列。
  • mat 是如何声明的?
  • float **mat, @WeatherVane @n.m.我应该如何添加列?

标签: c memory-management matrix malloc realloc


【解决方案1】:

假设您最初有一个 2x2 数组。

x x
x x

在调用 realloc 和 malloc 之后,您创建了一个如下所示的对象:

x x
x x
x x x

要解决此问题,您还需要在每一行上调用 realloc。


一个完整的解决方案可能如下所示:

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

int main(){
    int i, j, size = 3;
    float **mat;

    // Initial allocation
    mat = malloc(size * sizeof(float *));
    for(i = 0; i < size; i++)
        mat[i] = malloc(size * sizeof(float));

    // Initial values
    for(i = 0; i < size; i++)
        for (j = 0; j < size; j++)
            mat[i][j] = 1;

    // Print initial matrix
    for(i = 0; i < size; i++) {
        for (j = 0; j < size; j++)
            printf("%f ", mat[i][j]);
        printf("\n");
    }
    printf("\n");

    // I'm going to add a row and a column
    mat = realloc(mat, (size+1)*sizeof(float*));
    for(i = 0; i < size; i++)
        mat[i] = realloc(mat[i], (size+1)*sizeof(float));
    mat[size] = malloc((size+1) * sizeof(float));

    for(i = 0; i <= size; i++) {
        mat[i][size] = 0;
        mat[size][i] = 0;
    }

    //Print altered matrix
    for(i = 0; i <= size; i++) {
        for (j = 0; j <= size; j++)
            printf("%f ", mat[i][j]);
        printf("\n");
    }

    //Here comes the free
    for (i = 0; i <= size; i++)
        free(mat[i]);
    free(mat);
}

【讨论】:

  • 那么,删除mat[size] = malloc((size+1)*sizeof(float));,应该是这样吗? for(i = 0; i &lt;= size; i++) { mat[i] = realloc(mat[i], sizeof(float)); mat[size][i] = 0; }
  • @Aster:查看我的编辑。
  • 非常感谢!我明白我现在做错了什么!
猜你喜欢
  • 1970-01-01
  • 2013-12-05
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
相关资源
最近更新 更多