簡體   English   中英

程序收到信號:“ EXC_BAD_ACCESS”?

[英]Program received signal: “EXC_BAD_ACCESS”?

我正在制作一個只讀取文件內容的小程序,但是在運行它時出現此錯誤:程序收到信號:“ EXC_BAD_ACCESS”。

我還從Xcode收到警告:在代碼(main.c)的第10行中,“賦值使指針從整數開始而沒有強制轉換”:

#include <stdio.h>
#include <stdlib.h>

#define FILE_LOCATION "../../Data"

int main (int argc, const char * argv[]) {
    FILE *dataFile;
    char c;

    if ( dataFile = fopen(FILE_LOCATION, "r") == NULL ) {
        printf("FAILURE!");
        exit(1);
    }

    while ( (c = fgetc(dataFile)) != EOF ) {
        printf("%c", c);
    }

    fclose(dataFile);

    return 0;
}

這是調試器的輸出:

[Session started at 2012-06-27 10:28:13 +0200.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1515) (Sat Jan 15 08:33:48 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
Loading program into debugger…
Program loaded.
run
[Switching to process 34331]
Running…
Program received signal:  “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
(gdb)

指針有問題嗎?函數是否錯誤? 我發現了一個跟蹤內存問題的東西,稱為NSZombie,那是什么,我可以使用它嗎?

if ( (dataFile = fopen(FILE_LOCATION, "r")) == NULL ) {

這是另一個錯誤:

char c;

while ( (c = fgetc(dataFile)) != EOF ) {

在使用fgetc()類的重要功能之前,您應該真正學習它們的文檔

具體來說, fgetc()返回int ,這是適合EOF值所必需的。 如果返回char ,那么將必須有一個字符,其數值與EOF發生沖突,因此不能出現在二進制文件中。 那會很爛,所以這不是它的工作原理。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM