我正在研究Visual Studio 2010.该程序本身最初是为未来的CUDA端口而设计的,所以所有的都设置为它,但是现在我只是测试它是否适用于普通的c ++(实际上我现在正试图坚持使用c,因为我对它更熟悉。)
相关代码是:
#define NMBR_EXP_ENERGIES 21
#define NMBR_Ls 3
#define NMBR_POINTS 20000
<Emin, Emax, dE are initialized as global constants>
int NMBR_EXP_ENERGIES_L[NMBR_Ls];
<Some functions>
void write_results(double ** u, int * NmbrNodes, int * div){
const char prefix[] = "wave_function_";
char filename[24];
double *eigenergies;
int div0,i,j,k,l,m;
FILE *file_u;
FILE *file_output;
eigenergies = (double *)malloc(NMBR_EXP_ENERGIES*sizeof(double));
j=0;
m=0;
file_output = fopen("computation_results.out","w");
fprintf(file_output,"This file contains the output\n");
for(l=0;l<NMBR_Ls;l++){
div0=div[l*NMBR_ENERGIES];
for(i = l*NMBR_ENERGIES;i<NMBR_ENERGIES*(l+1);i++){
if (div[i] != div0 && m<NMBR_EXP_ENERGIES_L[l]){
div0=div[i];
j++;
m++;
eigenergies[j-1] = (Emin+((double) (i-l*NMBR_ENERGIES))*dE)-dE/2.0;
fprintf(file_output,"The eigenergy %d is %1.15lf and the wavefunction has %d nodes\n",j,eigenergies[j-1],NmbrNodes[i]);
sprintf(filename,"%d_%s%d.out",l,prefix,j);
file_u = fopen(filename,"w");
for(k=0;k<NMBR_POINTS;k++){
fprintf(file_u,"%lf %1.15lf \n",k*RMAX/NMBR_POINTS,u[i][k]);
}
fclose(file_u);
}
}
if (j < NMBR_EXP_ENERGIES_L[l]){
j = NMBR_EXP_ENERGIES_L[l];
}
m=0;
}
fprintf(file_output,"R = %1.15lf\n ",error_control(eigenergies));
fprintf(file_output,"%d eigenergies were found\nIts eigenfunctions were stored on the file %sj.out, 1<j<%d",j,prefix,j);
fclose(file_output);
free(eigenergies);
}
<Some functions>
int main(void){
<Code that executes the computation and stores it on u[i][j],NmbrNodes[i] and div[i]>
write_results(u, NmbrNodes, div);
}
根据需要,向量div先前填充了1和-1。程序运行正常,而l = 0和l = 1。但是,当他最后一次启动外循环(l = 2)并且第二次进入if时,它会在行fprintf(file_output,"The eigenergy %d is %1.15lf and the wavefunction has %d nodes\n",j,eigenergies[j-1],NmbrNodes[i]);
上崩溃。错误消息是
First-chance exception at 0x77dd3ea0 in Potential_Model_Numerov.exe: 0xC0000005: Access violation writing location 0x00000014.
Unhandled exception at 0x77dd3ea0 in Potential_Model_Numerov.exe: 0xC0000005: Access violation writing location 0x00000014.
当选择中断程序时,它会在函数void __cdecl _lock
的末尾打开mlock.c文件。
我已经检查过我没有读到超出其分配空间的任何向量(egnergies直到egnergies [20]和j = 17,当这发生时,NmbrNodes一直持续到NmbrNodes [3071]和i = 3009崩溃的时刻)。所以我不知道他为什么要读一个forbbiden内存地址。有人有任何想法吗?
谢谢!
附注:我有另一个功能,基本上做同样的事情,但没有写任何东西到硬盘驱动器,这一个运行得很好。此外,有时它会打开文件osfinfo.c而不是mlock.c,并在函数int __cdecl __lock_fhandle
的末尾停止。
答案 0 :(得分:1)
您确定eigenenergies
不是NULL
吗?您永远不会检查malloc()
的返回值。
鉴于您获得的访问冲突位于地址0x00000014并且您声称
egnergies直到egnergies [20]
这将是我的第一次猜测。请注意,0x14
等于20
。
您可能希望跳过malloc()
并仅使用double eigenenergies[NMBR_EXP_ENERGIES]
。