c - 将变量分配给数组元素的问题
问题描述
所以我目前正在编写一个函数,可以将数据输出到准备导出到文本的数组中。函数在收集变量时工作正常,但在 Xcode 中出现“线程 1:EXC_BAD_ACCESS (code=1, address=0x0)”错误并且不知道如何调试它。我尝试使用 calloc 为数组分配内存并使用地址位置,但仍然收到类似的错误消息并且使用地址位置不起作用。
有没有人对我如何解决这个问题有任何建议?错误代码显示在 for 循环的第一行,并且该函数作为更大函数的一部分运行。
void LamVelProf(double dP, double L, double d, double mu)
{
double *r = malloc(sizeof(r)); //Point radius from centreline
double *R = malloc(sizeof(R)); //Absolute pipe radius
double *vx = malloc(sizeof(vx));
double *gvx = malloc(sizeof(gvx));
double *offset = malloc(sizeof(offset));
double **profile[7500][4];
**profile = calloc((7500*4), sizeof(double));
//double **profile = calloc((7500*4), sizeof(double));
int *i = malloc(sizeof(i));
*R = d/2; //Setting the boundary condition
*offset = 0.001;
*i = 0;
for(*r = 0; *r < (*R + (*offset/2)); *r = (*r)+(*offset))
{
**profile[*i][0] = *r;
LamVelProCalc(dP, L, d, mu, *r, vx);
**profile[*i][1] = *vx;
LamGenProCalc(*r, d, gvx);
**profile[*i][2] = *gvx;//Results from general profile
**profile[*i][3] = *i+1;
++*i; //Increasing count by 1
}
printf("%i rows generated\n", *i);
free(r);
free(R);
free(offset);
int *row = malloc(sizeof(row));
int *col = malloc(sizeof(col));
for(*row = 0; *row < *i + 1; *row = *row + 1)
{
for(*col = 0; *col < 4; *col = *col + 1)
{
printf("%f", **profile[*row][*col]);
if(*col == 3)
{
printf("\n");
}else{
printf("\t");
}
}
}
}
解决方案
我不得不积极地去指针化这段代码以使其回到可理解的领域,最终结果是这样的:
void LamVelProf(double dP, double L, double d, double mu)
{
double R = d/2;
double offset = 0.001;
double profile[7500][4];
int i = 0;
for (double r = 0; r < (R + (offset/2)); r += offset) {
double vx = 0.0; // Initialize appropriately
double gvx = 0.0;
profile[i][0] = r;
// No idea what this does, or why the return value is ignored
LamVelProCalc(dP, L, d, mu, r, vx);
profile[i][1] = vx;
// No idea what this does, or why the return value is ignored
LamGenProCalc(r, d, gvx);
profile[i][2] = gvx;//Results from general profile
profile[i][3] = i+1;
++i; //Increasing count by 1
}
printf("%i rows generated\n", i);
for(int row = 0; row < i + 1; ++row)
{
for(int col = 0; col < 4; ++col)
{
printf("%f", profile[row][col]);
if (col == 3) {
printf("\n");
} else {
printf("\t");
}
}
}
}
如您所见,其中埋藏了两个函数调用,它们可能应该具有指针参数,我的猜测是vx
并且gvx
旨在由该函数操纵。在 C 语言中,通常使用指针来操作外部变量,因此指针参数几乎总是意味着“数组”或“可变参数”,具体取决于上下文。
换句话说,我希望看到:
LamVelProCalc(dP, L, d, mu, r, &vx);
甚至更好:
double vx = LamVelProCalc(dP, L, d, mu, r);
而是显式返回该值。
尽管请注意上述问题,但现在应该可以编译并运行而不会崩溃。
当涉及到解决问题的编译器建议时,请记住将它们全部考虑在内。归根结底,您是程序员,而不是编译器,并且并非它所做的每一个有根据的猜测都是对手头问题的有效解释。如果您坚定不移地遵循编译器的建议,它可能会导致您走上非常非常奇怪的道路,就像这里可能发生的那样。
需要注意的是,拥有变量r
并且R
是边缘程序员滥用。请不要这样做。
要记住的另一件事是您对7500
此处的随意使用。这只是对您需要多少条目的猜测吗?计算它几乎总是更好,您知道for
循环将如何运行,以便您可以进行数学计算并相应地分配。
如果这是您通过其他方法达到的限制,则值得使用 a#define
来表示,例如:
#define MAX_PROFILE_ENTRIES 7500
现在很清楚这个数字背后的含义是什么。
推荐阅读
- node.js - 电子 setProxy() 和 fromPartition() 不起作用
- c++ - 简单的 C++11 哈希函数无法编译
- python - 如何检查两个python队列是否相同
- sql - 在 MSACCESS 中按类别和子类别创建前 N 个列表
- python - 如何将变量从一个python脚本传递给另一个?
- swift - 领域过滤器不总是找到数据?
- java - 有没有更好的方法来声明应该在 onNext() 和 onCompleted() 中使用的对象?
- java - 如何在 jOOQ 模拟数据中返回结果列表
- python - 比较一行中的值并将结果写入新列
- ros - 是否可以使用 NVIDIA Jetson TX2 通过 UART 端口控制 DJI M210 无人机的速度?