使用fscanf时出现分段错误,使用valgrind后调试失败

时间:2013-09-24 02:56:39

标签: c segmentation-fault valgrind

我已经删除了有效的代码,因此下面的代码不是一个完整的程序,而是有问题的部分。

我在下面的fscanf行中遇到了分段错误。我为每个变量添加了字符宽度以尝试修复它,所以我不明白它为什么会出错。

我从CSV文件读取结构数组。

我的主要是这个:

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

main()
{
    menu();
}

然后menu.c:

#include <stdio.h>
#include <stdlib.h>
#include "DatabaseOps.h"
#include "menu.h"
#define SIZE 1000 //max size assumed to be 1000
void menu()
{
    int j, lastID; //keeping track of id numbers used

    Person* persons;
    persons = (Person*)malloc(SIZE * sizeof(Person)); /*declaring array of person structs on the heap*/

    for(j = 0; j < SIZE; j++) /*initialise all IDs to -1*/
        {
        persons[j].ID = -1;
        }

    int option = 1;
    while (option!=7)
    {
        printf("1. Load Database\n");
        scanf("%d", &option);

        switch (option)
            {
            case 1:
            printf("\nLoading Database\n\n");
            lastID = loadDb(persons);
            break;

            default:
            printf("Invalid choice, please try again\n\n");
            break;
            }
    }
} 

人员在menu.h中定义如下:

typedef struct Person
    {
    int ID;
    int salary;
    int deleted;
    char salutation[4];
    char firstName[21];
    char surName[31];
    char job[16];
    } Person;

然后导致错误的DatabaseOps.c文件:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "DatabaseOps.h"
#include "menu.h"

int loadDb(Person *inPersons)  //function reads csv file into an array of Employee structs
    {
    int i, newID = 0; /*declaring lastID counter to keep track of the last employee ID so that I can increment it when creating a new employee*/

    char* fileName = malloc( 100 * sizeof( char ));
    printf("Enter name of CSV file: ");
    scanf("%99s", fileName);

    FILE* f = fopen(fileName, "r");

    if(f==NULL)  /*If the file doesn't exist, return to menu*/
        {
        printf("Error: could not open file\n");
        }

    else
        { /*the fscanf function uses grouping by commas to seperate the CSV values - [^,]*/
        while(fscanf(f, "%d,%3[^,],%20[^,],%30[^,],%15[^,],%d,%d", &inPersons[i].ID, inPersons[i].salutation, inPersons[i].firstName, inPersons[i].surName, inPersons[i].job, &inPersons[i].salary, &inPersons[i].deleted)!=EOF)
            {
            newID = inPersons[i].ID; //Keeping track of the last used ID
            i++;
            }
        }
    fclose(f);

    return newID;
    }

Valgrind给了我这个错误,我不知道如何解释:

==19378== Use of uninitialised value of size 4
==19378==    at 0x405A215: _IO_vfscanf (in /lib/libc-2.12.so)
==19378==    by 0x4067368: __isoc99_fscanf (in /lib/libc-2.12.so)
==19378==    by 0x80486E5: loadDb (DatabaseOps.c:25)
==19378==    by 0x80485B5: menu (menu.c:29)
==19378==    by 0x804852E: main (main.c:6)
==19378== 
==19378== Invalid write of size 4
==19378==    at 0x405A215: _IO_vfscanf (in /lib/libc-2.12.so)
==19378==    by 0x4067368: __isoc99_fscanf (in /lib/libc-2.12.so)
==19378==    by 0x80486E5: loadDb (DatabaseOps.c:25)
==19378==    by 0x80485B5: menu (menu.c:29)
==19378==    by 0x804852E: main (main.c:6)
==19378==  Address 0x9acac288 is not stack'd, malloc'd or (recently) free'd
==19378== 
==19378== 
==19378== Process terminating with default action of signal 11 (SIGSEGV)
==19378==  Access not within mapped region at address 0x9ACAC288
==19378==    at 0x405A215: _IO_vfscanf (in /lib/libc-2.12.so)
==19378==    by 0x4067368: __isoc99_fscanf (in /lib/libc-2.12.so)
==19378==    by 0x80486E5: loadDb (DatabaseOps.c:25)
==19378==    by 0x80485B5: menu (menu.c:29)
==19378==    by 0x804852E: main (main.c:6)

1 个答案:

答案 0 :(得分:3)

int i, newID = 0; /*declaring lastID counter to keep track of the last employee ID so that I can increment it when creating a new employee*/

i需要初始化为0。