简体   繁体   中英

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).

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

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 .

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?

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 .

Are you sure eigenenergies is not NULL ? You never check the return value of malloc() .

Given that you get an access violation is at address 0x00000014 and you claim that

eigenergies goes until eigenergies[20]

that would be my first guess. Note that 0x14 equals 20 .

You might want to skip malloc() ing and just use double eigenenergies[NMBR_EXP_ENERGIES] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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