Accessing a record with error 0x00000014 with fprintf

I am working on Visual studio 2010. The program itself is originally intended for the future port for CUDA, so everything is ready for it, but at the moment I'm just testing whether it works on simple C ++ (in fact, I'm trying to stick to c at the moment , since I know him).

Relevant Code:

#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) (il*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 div vector was pre-populated with 1 and -1 as needed. The program works fine with l = 0 and l = 1. However, when it starts the external loop for the last time (l = 2), and it enters if if the second time it falls 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]); . Error message

 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 you select to break the program, it opens the mlock.c file at the end of the void __cdecl _lock .

I have already verified that I do not read any of the vectors outside their allocated space (eigenergies goes before eigenergies [20] and j = 17 when this happens, and also NmbrNodes goes until NmbrNodes [3071] and I = 3009 at the time of the accident). Therefore, I do not know why he is trying to read the prohibition memory address. Does anyone have any ideas?

Thanks!

Side notes: I have another function that does basically the same thing, but without imposing anything on the hard drive, and it works fine. In addition, sometimes it opens the osfinfo.c file instead of mlock.c and stops at the end of the int __cdecl __lock_fhandle .

+4
source share
1 answer

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

Given that you are receiving an access violation at 0x00000014, and you are claiming that

eigenergies goes on until generation starts [20]

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

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

+1
source

Source: https://habr.com/ru/post/1500966/


All Articles