简体   繁体   English

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

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

I'm working on Visual studio 2010. The program itself is originally designed thinking to a future port for CUDA, so all is set to go to it, but for now I'm just testing if it works on plain c++ (actually I'm trying to stick to c for now, since I'm more familiar with it). 我正在研究Visual Studio 2010.该程序本身最初是为未来的CUDA端口而设计的,所以所有的都设置为它,但是现在我只是测试它是否适用于普通的c ++(实际上我是'我现在试图坚持使用c,因为我对它更熟悉)。

The relevant code is: 相关代码是:

#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);

}

The vector div was previously filled with 1 and -1 as needed. 根据需要,向量div先前填充了1和-1。 The program runs fine while l = 0 and l =1. 程序运行正常,而l = 0和l = 1。 However when he starts the outer loop for the last time (l = 2) and it enters the if for the second time, it crashes on the line fprintf(file_output,"The eigenergy %d is %1.15lf and the wavefunction has %d nodes\\n",j,eigenergies[j-1],NmbrNodes[i]); 然而,当他最后一次启动外循环(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]); . The error message is 错误消息是

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.

When choosing to break the program, it opens the mlock.c file at the end of the function void __cdecl _lock . 当选择中断程序时,它会在函数void __cdecl _lock的末尾打开mlock.c文件。

I have already checked I'm not reading any of the vectors beyond their allocated space (eigenergies goes until eigenergies[20] and j =17 when this happens as well NmbrNodes goes until NmbrNodes[3071] and i = 3009 on the moment of the crash). 我已经检查过我没有读取超出其分配空间的任何向量(egnergies直到egnergies [20]和j = 17,当这发生时,NmbrNodes一直持续到NmbrNodes [3071]并且i = 3009在此时刻崩溃)。 So I don't know why he is trying to read a forbbiden memory address. 所以我不知道他为什么要读一个forbbiden内存地址。 Does any one have any idea? 有谁有想法吗?

Thanks! 谢谢!

Side notes: I have another function which does basically the same thing, but without writting anything to the hard drive, and this one runs just fine. 附注:我有另一个功能,基本上做同样的事情,但没有写任何东西到硬盘驱动器,这一个运行正常。 Also, sometimes it opens the file osfinfo.c instead of mlock.c and stops at the end of the function int __cdecl __lock_fhandle . 此外,有时它会打开文件osfinfo.c而不是mlock.c,并在函数int __cdecl __lock_fhandle的末尾停止。

Are you sure eigenenergies is not NULL ? 你确定eigenenergies不是NULL吗? You never check the return value of malloc() . 你永远不会检查malloc()的返回值。

Given that you get an access violation is at address 0x00000014 and you claim that 鉴于您获得访问冲突的地址为0x00000014并且您声明了这一点

eigenergies goes until eigenergies[20] egnergies直到egnergies [20]

that would be my first guess. 这将是我的第一个猜测。 Note that 0x14 equals 20 . 请注意, 0x14等于20

You might want to skip malloc() ing and just use double eigenenergies[NMBR_EXP_ENERGIES] . 您可能想要跳过malloc()并只使用double eigenenergies[NMBR_EXP_ENERGIES]

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

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