[英]managing memory in C, 2D float array
I'm very inexperienced with C and have to use it for a piece of coursework exploring heat transfer. 我对C非常缺乏经验,因此必须将其用于探索传热的一部分课程中。
I'm getting all sorts of errors with the (0xC0000005) return code. 我收到(0xC0000005)返回码的各种错误。 I'm aware that this is an attempt to access memory out of bounds and that I'm probably failing to allocate memory properly somewhere but I haven't been able to figure it out.
我知道这是一种尝试访问内存的尝试,并且可能无法在某处正确分配内存,但是我无法弄清楚。
Any idea what needs changing? 知道什么需要改变吗?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
/* constant alpha squared, the area of the domain */
const float alphSqrd = 0.01;
const float deltX, deltY = 0.0002;
const float timeStep = 0.0000009;
const int maxTimeSteps = 1000;
int h, i, j;
/* 2D Array of Temperature Values */
float T [500][500];
float dTdt;
/* initialise temperature values */
printf("Initialising 2D array...\n");
for (i=0; i<500; i++){
for (j=0; j<500; j++){
if (150<=i && i<350 && 150<=j && j<350){
T[i][j] = 50;
} else {
T[i][j] = 20;
}
}
}
printf("Updating values...\n");
for (h=0; h<maxTimeSteps; h++){
for (i=0; i<500; i++){
for (j=0; j<500; j++){
dTdt = alphSqrd*(((T[i+1][j]-2*T[i][j]+T[i-1][j])/(deltX*deltX))+((T[i][j+1]-2*T[i][j]+T[i][j-1])/(deltY*deltY)));
T[i][j] = T[i][j] + dTdt * timeStep;
printf("%f ",T[i][j]);
}
printf("\n");
}
}
return 0;
}
During your dTdt
calculation you are using T[i-1][j]
and T[i-1][j]
. 在
dTdt
计算期间,您正在使用T[i-1][j]
和T[i-1][j]
。 If i is maximal (0 or 500) this exceeds the array limits. 如果i最大(0或500),则超出数组限制。 The same holds for j.
j也是如此。 Thus, you use uninitialised memory.
因此,您将使用未初始化的内存。 You need to loop form
1
to 499
and treat the boundary differently depending on the problem you are trying to solve. 您需要将表格
1
循环到499
并根据要解决的问题对边界进行不同的处理。
Although that's not the problem you're describing, one issue is that you're not initializing deltX
. 尽管这不是您要描述的问题,但是一个问题是您没有初始化
deltX
。 Instead of this 代替这个
const float deltX, deltY = 0.0002;
you want 你要
const float deltX = 0.0002 , deltY = 0.0002;
Aside from that, you have an out of range issue. 除此之外,您还有超出范围的问题。 If you're going to access index
i - 1
and i + 1
, you can't loop from 0
to 499
on an array of 500
elements. 如果要访问索引
i - 1
和i + 1
,则不能在500
元素的数组上从0
循环到499
。
It works for me if I adjust the loops like this: 如果我像这样调整循环,它将对我有用:
for (i = 1; i < 499; i++) {
for (j = 1; j < 499; j++) {
Firstly adjust the loop min and max values; 首先调整回路的最小值和最大值;
for (i=1; i<499; i++){
for (j=1; j<499; j++){
And you should make comment line to printf()
. 并且您应该在
printf()
注释行。 stdout
takes much time in the loop. stdout
在循环中花费很多时间。
for (h=0; h<maxTimeSteps; h++){
for (i=1; i<499; i++){
for (j=1; j<499; j++){
dTdt = alphSqrd*(((T[i+1][j]-2*T[i][j]+T[i-1][j])/(deltX*deltX))+((T[i][j+1]-2*T[i][j]+T[i][j-1])/(deltY*deltY)));
T[i][j] = T[i][j] + dTdt * timeStep;
//printf("%f ",T[i][j]);
}
//printf("\n");
}
}
I compiled this code on my virtual Linux and it took nearly 3 seconds. 我在虚拟Linux上编译了此代码,花了将近3秒钟。 enter image description here
在此处输入图片说明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.