C语言2D浮点数组内存管理问题:0xC0000005错误排查求助
Alright, let's break down exactly what's causing that frustrating memory access error (0xC0000005) and fix the other logical issues in your simulation code:
1. Uninitialized deltX Variable
First off, you declared const float deltX, deltY = 0.0002; but only assigned a value to deltY. deltX will hold a random garbage value, which will completely mess up your finite difference calculations. Fix this by initializing both variables explicitly:
const float deltX = 0.0002, deltY = 0.0002;
2. Array Index Out-of-Bounds (The Root of the 0xC0000005 Error)
This is the main culprit behind your crash. When i = 0, you try to access T[i-1][j] (which is T[-1][j]—a memory location way outside your array). Similarly, when i = 499, you access T[500][j], which is beyond the last index of your T[500][500] array. The same problem happens for j = 0 and j = 499 with the Y-direction neighbors.
To fix this:
- Adjust your inner loops to skip boundary elements (since in heat conduction, boundaries usually have fixed conditions). Run
ifrom 1 to 498 (inclusive) andjfrom 1 to 498. - Leave the boundary cells (i=0, i=499, j=0, j=499) at their initial temperature (20°C here) as a simple Dirichlet boundary condition.
3. Overwriting Data Mid-Time Step
You're updating T[i][j] in-place while still using the same array to calculate neighboring cell values. This means you're using updated values from the same time step instead of the original state, which will give you incorrect heat propagation results.
The fix:
- Use a temporary 2D array (
new_Tin the code below) to store all the new temperature values for the entire time step. Once you've calculated every cell's new value, copy the temporary array back toTfor the next iteration.
4. Excessive Console Output
Printing the entire 500x500 array 1000 times will flood your console and slow down the program to a crawl. Instead, print progress updates only at intervals (like every 100 steps) or remove the full array print entirely (you can add back a small sample print to verify results if needed).
Modified Working Code
Here's the corrected version with all these fixes applied:
#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 = 0.0002, deltY = 0.0002; // Fixed uninitialized deltX const float timeStep = 0.0000009; const int maxTimeSteps = 1000; int h, i, j; /* 2D Array of Temperature Values */ float T[500][500]; float new_T[500][500]; // Temporary array to store new state 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; } new_T[i][j] = T[i][j]; // Initialize temp array with starting values } } printf("Updating values...\n"); for (h = 0; h < maxTimeSteps; h++){ // Update only inner cells (skip boundaries to avoid out-of-bounds access) 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) ); new_T[i][j] = T[i][j] + dTdt * timeStep; } } // Copy new state back to T for the next time step for (i = 0; i < 500; i++){ for (j = 0; j < 500; j++){ T[i][j] = new_T[i][j]; } } // Print progress every 100 steps to avoid flooding the console if (h % 100 == 0){ printf("Completed time step %d\n", h); // Optional: Uncomment to check center temperature // printf("Center temperature: %.2f°C\n", T[250][250]); } } printf("Simulation complete!\n"); return 0; }
Quick Additional Notes
- If you need different boundary conditions (like insulated edges instead of fixed temperature), you'd modify how the boundary cells are updated (e.g., using a Neumann condition where the derivative at the boundary is zero).
- Using a temporary array ensures all calculations use the state from the start of the time step, which is essential for accurate finite difference method simulations.
内容的提问来源于stack exchange,提问作者Gabriel Mulcahy

