当我调用atoi()函数时编程段错误

时间:2014-04-25 02:37:55

标签: c memory segmentation-fault runtime-error atoi

我的程序存在segfaulting问题,应该在包含保龄球分数的文件中读取,然后输出你的总得分,它会调用atoi()函数进行段错误。

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

/*
int myAtoi(char *str)
{
    int res = 0, i; // Initialize result

    // Iterate through all characters of input string and update result
    for (i = 0; str[i] != '\0'; ++i)
        res = res*10 + str[i] - '0';

    // return result.
    return res;
}
*/

//Cleans and totals the values
int cleanValues(char *values)
{
    //debug
    puts(values);
    //declares vars
    int i, total=0, temp;
    //iterates through the array converting and totaling the integer values.
    for(i=0; i!=37; ++i)
    {
        //converts and stores the values in temp
        temp = atoi(values[i]);
        //atoi() returns a zero when it the value is not a number and if the value it is zero it wouldn't mater anyways so in both cases we skip it here.
        if(temp != 0)
            //adds temp to the total.
            total+=temp;
        else
                //increments i
                    ++i;
        //debug
        printf("%c %d %d\n",values[i], i, total);
    }
    //returns the total value which is then returned to the main by readFile()
    return total;
}

//reads the file, the name of the file is passed from the main().
int readFile(char *name)
{
    //creates the array to hold the read values
    char values[37];
    //creates a pointer to a memory location where we store the file
    FILE *filePointer;

    //opens the file for reading
    filePointer=fopen(name, "r");

    //checks to see if the file contains a information.
    if(filePointer == NULL)
    {
        printf("File %s is not available\n", name);
        exit(1);
    }

    //reads from the file and shoves it into the array
    fgets(values, 37, filePointer);

    //debugging output for checking the value of values
    puts(values);

    return cleenValues(values);

}


//the main function... this shouldn't require an explanation... it is the running part of the program! OK there I said it!
int main(int argc, char* argv[])
{
    //checks to see if the program has been called correctly using the command line arguments
    if(argc!=2)
    {
         printf("USEAGE: %s nameOfTheInputFile\n", argv[0]);
         return 0;
    }
    //prints the total score by returning the value of mathing()
    printf("Your total score is: %d",readFile(argv[1]));

    return 0;
}

有谁知道为什么这是segfaulting?我很困惑,我所展示的每一个人都很感激,所有的帮助都得到了赞赏。

这是GDB在它中断之前的步进。

38                      temp = atoi(values[i]);
   atoi (str=0x39 <Address 0x39 out of bounds>) at Bowling.c:19
19          int res = 0, i; // Initialize result
22          for (i = 0; str[i] != '\0'; ++i)
   Program received signal SIGSEGV, Segmentation fault.
   0x0000000100401126 in atoi (str=0x39 <Address 0x39 out of bounds>) at Bowling.c:22
22          for (i = 0; str[i] != '\0'; ++i)
      1 [main] a 5640 cygwin_exception::open_stackdumpfile: Dumping stack trace to a.exe.stackdump
[Inferior 1 (process 5640) exited with code 0105400]

如果我正在读这个,那就是说阵列超出了指定的内存或其他东西。

2 个答案:

答案 0 :(得分:3)

当你传入一个字符串时,你正在将一个角色传递给atoi。我想你可以改变

atoi(values[i])

atoi(values)

虽然您不再需要for循环。我不完全确定循环意味着什么目的。您应该能够通过一次调用将字符串转换为整数。

答案 1 :(得分:3)

您正在传递 value [i] 这是一个字符,在atoi函数中,您正在取消引用该字符值,这会导致访问内存位置,这会限制导致分段错误。

根据你的atoi函数,你必须传递一个字符串作为atoi函数的参数。