无法使用c将值存储在矩阵中

时间:2018-10-03 11:37:26

标签: c matrix multidimensional-array struct malloc

这是我的代码:

typedef struct{

double (*a)[2];
} my_struct;


void update(my_struct* struct1){

struct1 -> a = malloc ( 2*sizeof(struct1->a) );

struct1 -> a[0][0] = 5.0;
struct1 -> a[0][1] = 10.0;
struct1 -> a[1][0] = 3.0;
struct1 -> a[1][1] = 4.0;

printf("%f %f %f\n", struct1->a[0][0], struct1->a[0][1], struct1->a[1][1]);

}


int main(){

my_struct struct1;

update(&struct1);

printf("%f %f %f\n", struct1.a[0][0], struct1.a[0][1], struct1.a[1][1]);


return 0;
}

所以基本上我想做的是拥有一个带有矩阵的结构(我真的很想在[r] [c]中使用这个矩阵定义,从而确定分配方式)。

如您所见,我有两个printfs可以查看正在发生的情况。

在命令行中,输出如下:

5.000000 10.000000 3.000000
5.000000 10.000000 0.000000

那么为什么在main()中打印的值之一为零?

2 个答案:

答案 0 :(得分:0)

我可以看到两个问题。

1)malloc大小不正确

struct1->a是指针,所以sizeof(struct1->a)是指针的大小。您想要的是第一个元素的大小。因此,请使用sizeof(struct1->a[0])(或sizeof(*struct1->a)

2)索引超出范围

您的代码(校正malloc大小后)正在创建2x2矩阵。因此,此访问权限struct1 -> a[1][2]超出范围

答案 1 :(得分:0)

添加必要的包含library(Statamarkdown)<stdio.h>之后,我们可以在Valgrind下运行代码以查看问题所在:

<stdlib.h>

这向我们表明分配给valgrind -q --leak-check=full ./52626203 ==1409== Invalid write of size 8 ==1409== at 0x10919F: update (52626203.c:16) ==1409== by 0x109209: main (52626203.c:28) ==1409== Address 0x4a39050 is 0 bytes after a block of size 16 alloc'd ==1409== at 0x48357BF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==1409== by 0x10915A: update (52626203.c:12) ==1409== by 0x109209: main (52626203.c:28) ==1409== ==1409== Invalid write of size 8 ==1409== at 0x1091B6: update (52626203.c:17) ==1409== by 0x109209: main (52626203.c:28) ==1409== Address 0x4a39058 is 8 bytes after a block of size 16 alloc'd ==1409== at 0x48357BF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==1409== by 0x10915A: update (52626203.c:12) ==1409== by 0x109209: main (52626203.c:28) ==1409== ==1409== Invalid read of size 8 ==1409== at 0x1091C6: update (52626203.c:19) ==1409== by 0x109209: main (52626203.c:28) ==1409== Address 0x4a39058 is 8 bytes after a block of size 16 alloc'd ==1409== at 0x48357BF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==1409== by 0x10915A: update (52626203.c:12) ==1409== by 0x109209: main (52626203.c:28) ==1409== 5.000000 10.000000 4.000000 ==1409== Invalid read of size 8 ==1409== at 0x109212: main (52626203.c:30) ==1409== Address 0x4a39058 is 8 bytes after a block of size 16 alloc'd ==1409== at 0x48357BF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==1409== by 0x10915A: update (52626203.c:12) ==1409== by 0x109209: main (52626203.c:28) ==1409== 5.000000 10.000000 4.000000 ==1409== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==1409== at 0x48357BF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==1409== by 0x10915A: update (52626203.c:12) ==1409== by 0x109209: main (52626203.c:28) ==1409== 的尝试有误,这表明我们仅为struct1->a[1][0]分配了足够的空间,而没有为a[0]分配。

这使我们直接进入了分配行,在该行我们看到我们错过了对a[1]的参数的取消引用。应该是

sizeof

我们正在为两个指向double数组的指针分配空间,而不是为每个都有两个元素的两个double数组分配足够的空间。

在实际代码中,顺便说一句,别忘了在尝试使用struct1->a = malloc(2 * sizeof *struct1->a); // ^ 返回的指针之前,并在完成后malloc()对其进行检查。