c - 在结构数组中导航并存储无法正常工作的值
问题描述
我的代码非常混乱,并且有两个结构,它们的定义和初始化都相同。但是对于tall
结构,我可以将变量存储在结构中tall[radius]
而不会出现任何问题。但是,当我复制xx
struct 的过程时,它不会打印错误的值。我似乎无法弄清楚结构使用有什么问题。
我需要一个结构数组来每次动态存储不同大小的数组,然后为每个结构存储其他元素。
我也对新方法持开放态度。为什么这两个结构的性能不同?
#include <stdio.h>
#include <stdlib.h>
#define ar_length 18 //this is the radius max
#define WIDTH 10 //xx width
struct ttall
{
int *pnt;
};
struct xall
{
int *pnt;
};
int main()
{
int i;
int m;
int *x1;
int *x2;
int *x3;
int *ttThis;
int rad_size;
//beginning of the loop
int radius = 1;
int size = (2 * radius + 1);
int size_sqrd = size * size * size;
struct ttall **tall = malloc(sizeof(struct ttall *));
struct ttall *struct_pnt = malloc( 3*sizeof(struct ttall));
struct xall **xx = malloc(sizeof(struct xall *));
struct xall *xpnt = malloc( 3*sizeof(struct xall));
for (int radius = 0; radius < 3; radius++)
{
//need to increment the pointer to the struct everytime
xx[radius] = xpnt+radius;
tall[radius] = struct_pnt+radius;
int z = -1;
int t = -radius-2;
printf("****T*** %d\n",t);
int rad;
int meshCount;
meshCount = (2 * (radius+1) + 1);
rad_size = (2 * (radius+1) + 1) * (2 * (radius+1) + 1) * (2 * (radius+1) + 1);
printf("radius size : %d\n",rad_size);
printf("mesh size : %d\n",meshCount);
tall[radius]->pnt = malloc(rad_size * sizeof(int));
xx[radius]->pnt = malloc(rad_size * WIDTH * sizeof(int));
x1 = malloc(rad_size * sizeof(int));
x2 = malloc(rad_size * sizeof(int));
x3 = malloc(rad_size * sizeof(int));
for (i = 0; i < meshCount; i++)
{
t++;
z++;
for (m = 0; m < meshCount*meshCount; m++)
{
x1[z * (meshCount*meshCount) + m] = t;
}
}
//x2 computations
i = 0;
m = 0;
z = 0;
t = -radius-2;
int x;
for (x = 0; x < meshCount; x++)
{
t++;
for (m = 0; m < meshCount; m++)
{
for(int h = 0;h<meshCount;h++){
int index = meshCount*x+(meshCount*meshCount)*h +m ;
x2[index] = t;
}
}
}
//x3 computations
i = 0;
m = 0;
z = 0;
t = -radius-2;
for (x = 0; x < meshCount; x++)
{
// t++;
t = -radius-2;
for (m = 0; m < meshCount; m++)
{
t++;
for(int h = 0;h<meshCount;h++){
int index = meshCount*x+(meshCount*meshCount)*h +m ;
x3[index] = t;
}
}
}
// structure initializations and memalocation
//works fine with expanding radius
for (m = 0; m < rad_size; m++)
{
tall[radius]->pnt[m] = (x1[m] * x1[m]) + (x2[m] * x2[m]) + (x3[m] * x3[m]);
}
// doesnt work here
m = 0;
for (i = 0; i < rad_size; i++)
{
xx[radius]->pnt[i * WIDTH ] = 1;
xx[radius]->pnt[i * WIDTH + 1] = x1[i];
xx[radius]->pnt[i * WIDTH + 2] = x2[i];
xx[radius]->pnt[i * WIDTH + 3] = x3[i];
xx[radius]->pnt[i * WIDTH + 4] = x1[i] * x1[i];
xx[radius]->pnt[i * WIDTH + 5] = x1[i] * x2[i];
xx[radius]->pnt[i * WIDTH + 6] = x1[i] * x3[i];
xx[radius]->pnt[i * WIDTH + 7] = x2[i] * x2[i];
xx[radius]->pnt[i * WIDTH + 8] = x2[i] * x3[i];
xx[radius]->pnt[i * WIDTH + 9] = x3[i] * x3[i];
}
}
//free(x1);
//free(x2);
//free(x3);
//***testing sum***
// the sum when radius = 1 of xx should be 171
int k = 0;
int sum = 0;
//can replace 27 with rad_size
for (k = 0; k < 27*WIDTH; k++)
{
sum = sum + abs(xx[0]->pnt[k]);
//printf("%d\n",abs(xx[0]->pnt[k]));
}
printf(" sum xx : %d\n", sum);
//******Testing****
for (int c = 0; c < 27; c++)
{
//printf("X2 : %d\n",x3[c]);
printf("tall : %d\n", tall[1]->pnt[c]);
//printf("%d\n",xx[c]);
}
//free(ttThis);
//free(xx);
}
解决方案
为一个指针struct ttall **tall = malloc(sizeof(struct ttall *));
分配空间。
但后来你用它索引它,tall[radius]
但半径大于 1。
您必须分配更多内存:
struct ttall **tall = malloc(3 * sizeof(struct ttall *));`
推荐阅读
- pyspark - Pyspark 中的 Percentile.INC
- r - 将按键字符转换为R中的整数
- python-3.x - 比较 2 个包含不同键的字典列表,如果找到匹配则更新
- qt - 我是否遗漏了什么或者这是创建自定义控件/组件的方式
- python - 如何使用 OpenCV-Python 更改某个区域的色调
- asp.net-mvc - Asp.net Core OpenIdConnect (OIDC) 在哪里验证状态参数
- javascript - 缩小后适合 X 轴和 scaleXY 以保持画布居中
- c# - 使用动态数量的元素寻址 JObject
- android - 华为在应用内购买的版本与从安卓购买的普通应用内是否相同
- api - Flutter DIO:使用带有 Dio 包的二进制体上传图像