【问题标题】:Why does accessing my matrix give me a seg fault? [closed]为什么访问我的矩阵会给我一个段错误? [关闭]
【发布时间】:2016-08-27 22:45:08
【问题描述】:

当我尝试访问我的矩阵 Line*** 时,我遇到了段错误。 像(matrix[i][j]->vbit == 00),{int vbit} 这样的简单操作会给我一个分段错误。我假设它在构造函数中,但我似乎找不到问题。有人看吗?

Line ***getSets(int width, int height){
int i;
int j;
Line *temp;
Line line;

printf("Make %d sets with %d lines\n",height,width);

Line*** matrix = (Line***)calloc(height,sizeof(Line**));


for(i=0;i < height;i++){
    matrix[i] = (Line**)calloc(width,sizeof(Line*));
}    

/// Set all vbits to 0
for(i = 0; i < height;i++){
    for(j = 0;j <width; j++){
        temp = matrix[i][j];
        temp = malloc(sizeof(Line));
        temp->vbit = 0;        
        temp->tag = 0;
        temp->lastUsed = 0;
    }
}
return matrix;}

【问题讨论】:

  • 一个有 seg 错误问题的 3 星程序员。想象一下!
  • 不要将您的代码作为图片发布。请将您的代码作为文本直接发布在您的问题中。
  • 您没有将分配的缓冲区分配给matrix[i][j],它未初始化。
  • 不要发布代码图片,发布实际代码。话虽如此,您的问题很明显:在您的内部循环中,您从矩阵条目分配给temp,然后立即重新分配给temp,使第一个分配无用。相反,您需要在为矩阵条目分配存储空间后将 from temp to 分配给它。
  • temp = matrix[i][j];temp = malloc(sizeof(Line)); --> temp = matrix[i][j] = malloc(sizeof(Line));

标签: c arrays pointers matrix segmentation-fault


【解决方案1】:

您只分配给 temp,而不是您的实际矩阵元素:

temp = matrix[i][j];
temp = malloc(sizeof(Line));

改为这样做:

matrix[i][j] = malloc(sizeof(Line));
temp = matrix[i][j];

或者

for(j=0; j<width; j++) {
    temp = malloc(sizeof(Line));
    memset(temp, 0, sizeof(Line));
    matrix[i][j] = temp;
}

另外,你真的应该检查 calloc 和 malloc 的结果。

【讨论】:

    【解决方案2】:

    根据经验:每当您认为需要超过 2 层间接性时,总是意味着您的程序设计从根本上被破坏了。

    在这种情况下,您提出了 3 级间接性,只是因为您没有正确分配多维数组。你应该这样做like this

    话虽如此,即使您正确分配动态内存,问题的根源仍然是程序设计。考虑做一些完全不同的事情。例如,您的程序说它正在制作 x 组 y 行。那为什么不呢?创建一个集合类。下面是一个替代设计示例,您可以使用更多功能对其进行扩展:

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    // assuming: 1 set has several lines, 1 line has several coordinates
    
    typedef struct
    {
      // whatever you want in here, doesn't matter
      int x;
      int y;
    } line_t;
    
    typedef struct
    {
      size_t  lines_n;
      line_t* line;
    } set_t;
    
    
    bool get_sets (size_t height, size_t width, set_t set[height])
    {
      for(size_t i=0; i<height; i++)
      {
        set[i].lines_n = width;
        set[i].line = malloc( sizeof(line_t[width]) );
        if(set[i].line == NULL)
        {
          return false;
        }
    
        for(size_t j=0; j<width; j++)
        {
         // initialize all members:
          set[i].line[j].x = 0;
          set[i].line[j].y = 0;
        }
      }
    
      return true;
    }
    
    int main (void)
    {
      int height = 3;
      int width = 5;
      set_t set [height];
    
      printf("Make %d sets with %d lines\n", height, width);
      bool result = get_sets(height, width, set);
      if(result == false)
      {
        // out of memory error
      }  
    
      return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 2013-11-14
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多