使用fprintf访问违例写入位置0x00000014。

时间:2020-12-27 02:26:40

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. The program runs fine while l = 0 and 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]);. The error message is

在需要时,向量div之前填充了1和-1。程序在l = 0和l =1时运行良好。然而,当他最后一次启动外环(l = 2)并第二次进入if时,它在fprintf行上崩溃(file_output,“eigenergy %d为%1.15lf,波形函数有%d节点n”、j、eigenergy [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.

When choosing to break the program, it opens the mlock.c file at the end of the function void __cdecl _lock.

当选择中断程序时,它将打开mlock。在函数末尾的c文件void __cdecl _lock。

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). So I don't know why he is trying to read a forbbiden memory address. Does any one have any idea?

我已经检查过,我没有读取任何超出其分配空间的向量(eigenergy直到eigenergy[20]和j =17,当这种情况发生时,NmbrNodes直到NmbrNodes[3071]和I = 3009在崩溃时刻)。所以我不知道他为什么要读forbbiden的记忆地址。有人知道吗?

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结束时停止。

1 个解决方案

#1


1  

Are you sure eigenenergies is not 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]

eigenergies走直到eigenergies[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(),只使用双特征能[nmbr_exp_energy]。

#1


1  

Are you sure eigenenergies is not 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]

eigenergies走直到eigenergies[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(),只使用双特征能[nmbr_exp_energy]。