![](/img/trans.png)
[英]Passing struct-within-pointer-array-within-struct by address in C
[英]Double array within a struct pointer in c
为什么这个双映射数组几乎可以工作,但不能?
我的代码如下:
#include <stdlib.h>
#include <stdio.h>
typedef struct {
double mapping [3][3];
} CoordinateMapperStr;
typedef CoordinateMapperStr * CoordinateMapper;
CoordinateMapper CoordinateMapper_Constructor(void)
{
CoordinateMapper this = (CoordinateMapper) calloc (1, sizeof(CoordinateMapper));
//return this; // <- I was missing this return, but still the rest worked the same
}
void CoordinateMapper_Initialize(CoordinateMapper this, double numb)
{
for (int i=0; i < 3; i=i+1) {
for (int j=0; j < 3; j=j+1) {
this->mapping[i][j] = numb;
printf("mapping(%d, %d) = %f\n", i, j, this->mapping[i][j]);
}
}
}
void CoordinateMapper_Print(CoordinateMapper this)
{
for (int i=0; i < 3; i=i+1) {
for (int j=0; j < 3; j=j+1) {
printf("mapping(%d, %d) = %f\n", i, j, this->mapping[i][j]);
}
}
}
int main()
{
CoordinateMapper mapper_1 = CoordinateMapper_Constructor();
CoordinateMapper_Initialize(mapper_1, 1);
printf("Init 1 done\n");
CoordinateMapper_Print(mapper_1);
printf("Print 1 done\n");
CoordinateMapper mapper_2 = CoordinateMapper_Constructor();
CoordinateMapper_Initialize(mapper_2, 2);
printf("Init 2 done\n");
CoordinateMapper_Print(mapper_1);
printf("Second print 1 done\n");
CoordinateMapper_Print(mapper_2);
printf("Print 2 done\n");
}
// Here is the corresponding output
user:~/path$ gcc src/test_3.c -o test_3
user:~/path$ ./test_3
mapping(0, 0) = 1.000000
mapping(0, 1) = 1.000000
mapping(0, 2) = 1.000000
mapping(1, 0) = 1.000000
mapping(1, 1) = 1.000000
mapping(1, 2) = 1.000000
mapping(2, 0) = 1.000000
mapping(2, 1) = 1.000000
mapping(2, 2) = 1.000000
Init 1 done
mapping(0, 0) = 1.000000
mapping(0, 1) = 1.000000
mapping(0, 2) = 1.000000
mapping(1, 0) = 1.000000
mapping(1, 1) = 0.000000 // This is not correct
mapping(1, 2) = 0.000000 // This is not correct
mapping(2, 0) = 0.000000 // This is not correct
mapping(2, 1) = 1.000000
mapping(2, 2) = 1.000000
Print 1 done
mapping(0, 0) = 2.000000
mapping(0, 1) = 2.000000
mapping(0, 2) = 2.000000
mapping(1, 0) = 2.000000
mapping(1, 1) = 2.000000
mapping(1, 2) = 2.000000
mapping(2, 0) = 2.000000
mapping(2, 1) = 2.000000
mapping(2, 2) = 2.000000
Init 2 done
mapping(0, 0) = 1.000000
mapping(0, 1) = 1.000000
mapping(0, 2) = 1.000000
mapping(1, 0) = 1.000000
mapping(1, 1) = 0.000000 // This is not correct
mapping(1, 2) = 0.000000 // This is not correct
mapping(2, 0) = 0.000000 // This is not correct
mapping(2, 1) = 1.000000
mapping(2, 2) = 1.000000
Second print 1 done
mapping(0, 0) = 2.000000
mapping(0, 1) = 2.000000
mapping(0, 2) = 2.000000
mapping(1, 0) = 2.000000
mapping(1, 1) = 2.000000
mapping(1, 2) = 2.000000
mapping(2, 0) = 2.000000
mapping(2, 1) = 2.000000
mapping(2, 2) = 2.000000
Print 2 done
gcc
编译器标志来帮助我查看此类错误,而缺少的则return this;
在构造函数中?问题是无论你是否return this
,在任何一种情况下你都会得到未定义的行为。
当您不return this
值时,您的非 void function 不会返回值 - 因此您的代码使用了一些垃圾值(可能恰好是calloc
的返回值)。
如果你return this
——你返回sizeof(CoordinateMapper)
的分配,它只是单个指针的大小。 这小于您的 struct sizeof(CoordinateMapperStr)
,并且您的其他代码在分配的 memory 之外读取/写入。 这又是未定义的行为。
@YakovGalka 发现了我的错误。 这里要补充一点,valgrind确实是一个static分析工具,可以检测出这类编程错误。 通过将-Wall
和-g
添加到gcc
作为编译器标志并使用valgrind./compiled_app
运行应用程序,可以轻松检测到这些类型的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.