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



这是我的代码:

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 %fn", struct1->a[0][0], struct1->a[0][1], struct1->a[1][1]);
}

int main(){
my_struct struct1;
update(&struct1);
printf("%f %f %fn", struct1.a[0][0], struct1.a[0][1], struct1.a[1][1]);

return 0;
}

所以基本上我想做的是有一个带有矩阵的结构(我真的很想将矩阵的定义与 [r][c] 一起使用,从而使用我进行分配的方式(。

如您所见,我有两个 printf 来查看正在发生的事情。

在命令行中打印的内容如下:

5.000000 10.000000 3.000000
5.000000 10.000000 0.000000

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

我可以看到两个问题。

1(malloc尺寸不正确

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

2( 索引超出范围

您的代码(在更正 malloc 大小后(正在创建一个 2x2 矩阵。所以这个访问struct1 -> a[1][2]超出了范围

添加必要的包含<stdio.h><stdlib.h>后,我们可以在 Valgrind 下运行代码看看出了什么问题:

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参数的取消引用。 它应该是

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

我们为两个指针分配了空间,以增加双数组的空间,而不是为两个双数组分配空间,每个数组有两个元素。

顺便说一句,在实际代码中,不要忘记在尝试使用它之前检查从malloc()返回的指针,并在完成后free()它。

相关内容

  • 没有找到相关文章

最新更新