繁体   English   中英

使用fprintf获取访问冲突写入位置0x00000014

[英]Getting Access violation writing location 0x00000014 with fprintf

我正在研究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]);上崩溃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的末尾停止。

你确定eigenenergies不是NULL吗? 你永远不会检查malloc()的返回值。

鉴于您获得访问冲突的地址为0x00000014并且您声明了这一点

egnergies直到egnergies [20]

这将是我的第一个猜测。 请注意, 0x14等于20

您可能想要跳过malloc()并只使用double eigenenergies[NMBR_EXP_ENERGIES]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM