簡體   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