C语言 线程 1:EXC_BAD_ACCESS(代码=1,地址=0x0)标准 C 内存问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19239566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) standard C memory issue
提问by zeus_masta_funk
I'm writing code to compare two input files in standard C, using the Xcode IDE. I keep getting this error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0). I've done some reading on this and believe it to be a memory issue, but no matter what I try I can't seem to fix it (I've also tried making the structures dynamically using malloc and listed that at the bottom of the code). It's strange because it writes all of the data and then spits out that error at the end. The file format is something like this: start(int)..stop(int) id(+ or -) now some stuff I don't care about for the rest of the line I've just been testing this on a file with only + id's so the "-" aspect isn't part of the issue. Anyway I'm quite tired and have been staring at this for a few hours, so please forgive me if it doesn't make sense, I will update it after a few hours of sleep.
我正在编写代码来比较标准 C 中的两个输入文件,使用 Xcode IDE。我不断收到此错误:线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)。我已经对此进行了一些阅读并认为这是一个内存问题,但无论我尝试什么,我似乎都无法修复它(我也尝试使用 malloc 动态创建结构并将其列在底部编码)。这很奇怪,因为它写入所有数据,然后在最后吐出该错误。文件格式是这样的: start(int)..stop(int) id(+ or -) 现在一些我不关心的东西我刚刚在一个文件上测试了这个只有+ id,所以“-”方面不是问题的一部分。无论如何,我很累,已经盯着这个看了几个小时,所以如果没有,请原谅我
typedef struct
{
int start;
int stop;
char *strandID;
} location;
int main(int argc, const char * argv[])
{
if (argc != 4)
{
fprintf(stderr,
"Usage is ./a.out windowfile.txt genefile.txt outputFileName");
exit(-1);
}
//const vars
const char *windowInput = argv[1];
const char *geneInput = argv[2];
const char *outputfile = argv[3];
const int windowHeader = 9;
const int geneHeader = 3;
//get size of structures -- I have debugged and these work correctly, returning the size of my structure
const int posWsize = getSize(windowInput, "+", windowHeader);
const int negWsize = getSize(windowInput, "-", windowHeader);
const int posGsize = getSize(geneInput, "+", geneHeader);
const int negGsize = getSize(geneInput, "-", geneHeader);
//declare structs
location posWindow[posWsize];
location negWindow[negWsize];
location posGene[posGsize];
location negGene[negGsize];
//extract data here
getLocations(posWindow, negWindow, windowInput, windowHeader);
return 0;
}
void getLocations(location *posL, location *negL, const char *input,
const int header)
{
FILE *fileptr = NULL;
fileptr = fopen(input, "r"); //open file
if (fileptr == NULL)
{ //check for errors while opening
fprintf(stderr, "Error reading %s\n", input);
exit(-1);
}
char tmpLoc[20];
char tmpID[2];
int eofVar = 0;
int lineCount = 0;
while (lineCount < header)
{ //skip header and get to data
eofVar = fgetc(fileptr);
if (eofVar == '\n')
lineCount++;
}
int pCount = 0;
int nCount = 0;
while (eofVar != EOF)
{
fscanf(fileptr, "%s %s", tmpLoc, tmpID); //scan in first two strings
if (!strcmp(tmpID, "+"))
{ //if + strand
char *locTok = NULL;
locTok = strtok(tmpLoc, ".."); //tok and get values
posL[pCount].start = atoi(locTok);
locTok = strtok(NULL, "..");
posL[pCount].stop = atoi(locTok); //ERROR IS SHOWN HERE
posL[pCount].strandID = tmpID;
printf("start=%d\tstop=%d\tID=%s\tindex=%d\n", posL[pCount].start,
posL[pCount].stop, posL[pCount].strandID, pCount);
pCount++;
}
else if (!strcmp(tmpID, "-"))
{ //if - strand
char *locTok = NULL;
locTok = strtok(tmpLoc, ".."); //tok and get values
negL[nCount].start = atoi(locTok);
locTok = strtok(NULL, "..");
negL[nCount].stop = atoi(locTok);
negL[nCount].strandID = tmpID;
nCount++;
}
while ((eofVar = fgetc(fileptr)) != '\n')
{
if (eofVar == EOF)
break;
}
}
fclose(fileptr);
}
//dynamic way...same issue -- just replace this with the above if statement and use the create location function
if (!strcmp(tmpID, "+"))
{ //if + strand
int locStart;
int locStop;
locStart = atoi(strtok(tmpLoc, ".."));//tok and get values
locStop = atoi(strtok(NULL, ".."));
posL[pCount] = *createlocation(locStart, locStop, tmpID);
pCount++;
}
location *createlocation(int start, int stop, char *strandID)
{
location *tmp = NULL;
tmp = (location *) malloc(sizeof(location) * 1);
tmp->start = start;
tmp->stop = stop;
tmp->strandID = (char *) malloc(sizeof(char) * 2);
strcpy(tmp->strandID, strandID);
return tmp;
}
采纳答案by tangrs
Check the return value of strtok.
检查 的返回值strtok。
In your code here
在您的代码中
locTok = strtok(NULL, "..");
posL[pCount].stop = atoi(locTok); //ERROR IS SHOWN HERE
strtokis returning a NULL pointer and according to documentation,
strtok正在返回一个 NULL 指针,根据文档,
A null pointer is returned if there are no tokens left to retrieve.
如果没有要检索的标记,则返回空指针。
which matches my original guess that because the address code is 0x0there's a NULL pointer deference somewhere.
这符合我最初的猜测,因为地址代码是0x0某处有一个 NULL 指针。
Obviously, the following call to atoiis expecting a non-NULL pointer and crashes.
显然,以下对 的调用atoi期望非空指针并崩溃。
回答by ZAFAR007
You Can Also Use Exception Breakpoint in Xcode.
您还可以在 Xcode 中使用异常断点。
An exception breakpoint tells the debugger to pause whenever a problem is encountered anywhere in your program, so you can evaluate your program's state before it crashes.
异常断点告诉调试器在程序中的任何地方遇到问题时暂停,这样您就可以在程序崩溃之前评估程序的状态。
Go to the Breakpoint Navigation (Cmd+8), then click the + button in the bottom left and choose Add Exception Breakpoint. You can leave it there.
转到 Breakpoint Navigation (Cmd+8),然后单击左下角的 + 按钮并选择 Add Exception Breakpoint。你可以把它留在那里。
回答by Pierre Fontaine
for xc
对于 xc
in your main()function, try to remove char*argv[]or both arguments.
在您的main()函数中,尝试删除char*argv[]或同时删除这两个参数。
回答by Hongyu Mao
You should delete the arguments of you main function. And it will work.
您应该删除主函数的参数。它会起作用。


