0

我正在尝试在 C 中完成对稀疏矩阵的分配。我有一个作为值和坐标列表的稀疏矩阵,并将其转换为耶鲁格式。

我遇到了一个以前似乎没有人见过的奇怪的内存分配问题。我的代码是:

yale* convertMatrix(matrix_list* input){
int matrix_elements = input->elements;
int matrix_rows = input->m;

yale* yale = (struct y*)calloc(1, sizeof(yale));

int* A = (int*)calloc(matrix_elements, sizeof(int));
int* IA = (int*)calloc(matrix_rows + 1, sizeof(int));    
int* JA = (int*)calloc(matrix_elements, sizeof(int));

printf("%d elements\n",matrix_elements);

yale->A = A;      // Value
yale->IA = IA;          // Row (X)
yale->JA = JA;     // Column (Y)
yale->elements = matrix_elements;
yale->m = matrix_rows;
yale->n = input->n;

list* tmp_list = input->first;

for(int i = 0, j = 0, tmp_y = 0; i < matrix_elements && tmp_list!=NULL; i++){
    printf("Input Value: %d \n",tmp_list->point.value);
    A[i] = tmp_list->point.value;
    // Initialise the first row
    if(i == 0) IA[0] = tmp_list->point.x;
    else{
        // Add a new row index
        if(tmp_y != tmp_list->point.x){
            j++;
            IA[j] = i;
            tmp_y = tmp_list->point.x;
        }
    }
    JA[i] = tmp_list->point.y;
    tmp_list = tmp_list->next;
}

for(int i = 0; i < matrix_elements; i++)
    printf("%d,",yale->A[i]);
printf("\n");
for(int i = 0; i < matrix_rows + 1; i++)
    printf("%d,",yale->IA[i]);
printf("\n");
for(int i = 0; i < matrix_elements; i++)
    printf("%d,",yale->JA[i]);

return yale;
}

这是耶鲁的结构:

typedef struct y{
int n;
int m;
int elements;
int *IA;
int *JA;
int *A;
} yale;

但是程序在循环的第一次迭代中在第一个相关的 printf 处出现段错误。

      printf("%d,",yale->A[i]);

我是肯定的:matrix_elements 是一个整数(在我的测试用例中为 9) matrix_rows 是一个整数 A / IA / JA 都填充了正确的值(如果你在 printf 中将 yale->A 换成 A,它工作正常)。直接将数组调用到结构指针不会影响结果。Mallocing,callocing,而不是类型转换,都没有效果。

感谢 Xcode 和 gdb,我还可以在段错误的地方看到这一点。结构指针似乎指向数组

4

2 回答 2

0

我建议你在Valgrind下运行你的代码。这应该报告缓冲区溢出错误。(缓冲区溢出是您写入数组末尾的地方)。

我还建议您为代码编写一些单元测试。它们对检测错误非常有帮助。特别是,我建议您使用 3x3 输入矩阵编写一个测试,每个位置都有一个值。检查你得到的值是你所期望的。

于 2012-01-29T00:52:53.310 回答
0

要编译它,我需​​要将它添加到代码段中:

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

typedef struct y{
        int n;
        int m;
        int elements;
        int *IA;
        int *JA;
        int *A;
        } yale;

typedef struct list {
        struct list *next;
        struct point { int x,y,value; } point;
        } list;

typedef struct matrix_list {
        int elements;
        int m;
        int n;
        struct list *first;
        int *point;
        } matrix_list;

更新:我将程序转换为更易读的东西(恕我直言)。我不知道 IA 和 JA 应该做什么,但下面的片段应该等同于 OP。

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

struct y {
        unsigned int n;
        unsigned int m;
        unsigned int elements;
        unsigned int *IA;
        unsigned int *JA;
        int *A;
        } ;

struct list {
        struct list *next;
        struct point { unsigned int x,y; int value; } point;
        } ;

struct matrix_list {
        unsigned int elements;
        unsigned int m;
        unsigned int n;
        struct list *first;
        } ;

struct y *convertMatrix(struct matrix_list* input)
{
unsigned int matrix_elements = input->elements;
unsigned int matrix_rows = input->m;
unsigned int ii,jj,tmp_y;

struct y *yale ;
struct list *tmp_list ;

yale = calloc(1, sizeof *yale);
assert (yale != NULL);

printf("%u elements\n",matrix_elements);

yale->A = calloc(matrix_elements, sizeof *yale->A);
assert (yale->A != NULL);
yale->IA = calloc(matrix_rows + 1, sizeof *yale->IA);
assert (yale->IA != NULL);
yale->JA = calloc(matrix_elements, sizeof *yale->JA);
assert (yale->JA != NULL);

yale->elements = matrix_elements;
yale->m = matrix_rows;
yale->n = input->n;

    // Initialise the first row, set start condition
        // FIXME: this ignores the empty list or size=0 cases
yale->IA[0] = tmp_y = input->first->point.x;
ii = jj = 0;
for(tmp_list = input->first ;tmp_list; tmp_list = tmp_list->next) {
    printf("Input Value: %d \n",tmp_list->point.value);
    yale->A[ii] = tmp_list->point.value;
        // Add a new row index
    if(tmp_y != tmp_list->point.x){
        jj++;
        yale->IA[jj] = ii;
        tmp_y = tmp_list->point.x;
    }
    yale->JA[ii] = tmp_list->point.y;
    if (++ii >= matrix_elements ) break;
}

for(int i = 0; i < matrix_elements; i++)
    printf("%d,",yale->A[i]);
printf("\n");
for(int i = 0; i < matrix_rows + 1; i++)
    printf("%u,",yale->IA[i]);
printf("\n");
for(int i = 0; i < matrix_elements; i++)
    printf("%u,",yale->JA[i]);

return yale;
}

注意:我将 (ii == 0) {} 条件移出循环,并将单字母索引替换为两字母等价物。另外:所有索引都是无符号的(应该是)

于 2012-01-25T13:55:10.597 回答