[英]Double array within a struct pointer in c
Why does this double mapping array almost work, but doesn't?为什么这个双映射数组几乎可以工作,但不能?
My code is as follows:我的代码如下:
#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
compiler flags could I use to help me see this kind of error and the missing return this;
我可以使用哪些gcc
编译器标志来帮助我查看此类错误,而缺少的则return this;
in the constructor?在构造函数中?The problem is that whether you return this
or not, you get undefined behavior in either case.问题是无论你是否return this
,在任何一种情况下你都会得到未定义的行为。
When you don't return this
your non-void function doesn't return a value -- thus your code uses some garbage value (which might happen to be the return value from calloc
).当您不return this
值时,您的非 void function 不会返回值 - 因此您的代码使用了一些垃圾值(可能恰好是calloc
的返回值)。
If you return this
-- you return allocation of sizeof(CoordinateMapper)
, which is just a size of a single pointer.如果你return this
——你返回sizeof(CoordinateMapper)
的分配,它只是单个指针的大小。 This is less than your struct sizeof(CoordinateMapperStr)
, and your other code reads/writes beyond the allocated memory.这小于您的 struct sizeof(CoordinateMapperStr)
,并且您的其他代码在分配的 memory 之外读取/写入。 This is, again, undefined behavior.这又是未定义的行为。
@YakovGalka spotted my error. @YakovGalka 发现了我的错误。 I want to add here that valgrind is indeed a static analysis tools that can detect these kinds of programming errors.这里要补充一点,valgrind确实是一个static分析工具,可以检测出这类编程错误。 By adding -Wall
and -g
to gcc
as compiler flags and running the application with valgrind./compiled_app
then these kinds of errors are easily detected.通过将-Wall
和-g
添加到gcc
作为编译器标志并使用valgrind./compiled_app
运行应用程序,可以轻松检测到这些类型的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.