為什么這個雙映射陣列幾乎可以作業,但不行?
我的代碼如下:
#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;建構式中的缺失?
uj5u.com熱心網友回復:
問題在于,無論您是否return this這樣做,在任何一種情況下都會出現未定義的行為。
當您不這樣做時,return this您的非 void 函式不會回傳值——因此您的代碼使用了一些垃圾值(這可能恰好是 的回傳值calloc)。
如果您return this- 您回傳分配的sizeof(CoordinateMapper),這只是單個指標的大小。這小于您的 struct sizeof(CoordinateMapperStr),并且您的其他代碼讀取/寫入超出分配的記憶體。這又是未定義的行為。
uj5u.com熱心網友回復:
@YakovGalka 發現了我的錯誤。我想在這里補充一點,valgrind 確實是一個可以檢測此類編程錯誤的工具。通過添加-Wall并-g以gcc如編譯器標志和運行與應用程式valgrind ./compiled_app,然后很容易被檢測到這些型別的錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/370432.html
