Domanda

I am trying to read from a text file into a linked list of structs in C.

In the program I have a struct definition in a .h file as such:

typedef struct SystemDetails *SyDetails;

The struct itself is defined in a .cpp file as such:

struct SystemDetails //stores system users data from the list
{
char fname[20],lname[20],password[7],email[30],SQuestion[10],SAnswer[10];
long unsigned int id;
int memory_alloc, request_ind,delete_ind;
SystemDetails *next;
};

The function that deals with the file and the data transfer is:

SyDetails System_Data()
{
FILE *fp=NULL;
SyDetails head=NULL,curr=NULL;
char fname[20],lname[20],password[7],email[30],SQuestion[10],SAnswer[10];
long unsigned int id=0;
int memory_alloc=0,request_ind=0,delete_ind=0;
fp=fopen("SystemList.txt","r+");
if(fp==NULL)//if file doesn't exist on the pc open a new file
{

    printf("error,file cant open\n");
    exit(-1);

}
else
{

    while(fscanf(fp, "%s %s %d %s %s %s %s %d %d %d", &fname,&lname,&id,&email,&password,&SQuestion,&SAnswer,&memory_alloc,&request_ind,&delete_ind)!=EOF)
    {
        if(head==NULL)
        {
            head=(SyDetails) malloc(sizeof (SyDetails));
            curr=head;
        }
        else
        {

            curr->next=(SyDetails) malloc(sizeof (SyDetails));
            curr=curr->next;
        }
        strncpy(curr->fname, fname, 20);
        strncpy(curr->lname, lname, 20);
        curr->id=id;
        strncpy(curr->email, email, 30);
        strncpy(curr->password, password, 10);
        strncpy(curr->SQuestion, SQuestion, 10);
        strncpy(curr->SAnswer, SAnswer, 10);
        curr->memory_alloc=memory_alloc;
        curr->request_ind=request_ind;
        curr->delete_ind=delete_ind;
        curr->next=NULL;

    }

}
return head;


}

Anyway the text file has 3 records , the first row works when head==NULL but at the second time when the function gets to the else to malloc the next node of the linked list it crashes at the malloc line and I get an error that says:

"Windows has triggered a breakpoint in final-project.exe.

This may be due to a corruption of the heap, which indicates a bug in final-project.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while final-project.exe has focus.

The output window may have more diagnostic information."

My guess is that there is something with the way I defined the pointers in the function.

I would appriciate any help , thanks!

È stato utile?

Soluzione

The line

head=(SyDetails) malloc(sizeof (SyDetails));

is wrong in two ways. First, the reason why you are corrupting the memory: SyDetails is a pointer, so this will allocate only sizeof(pointer) bytes which is less than the necessary sizeof(struct SystemDetails) - for which you should change it. Even better, change it to sizeof(*head) to be safe in case the type of head ever changes.

The second thing is that in C, you should not cast the return value of malloc().

All in all:

head  = malloc(sizeof(*head));

is what you really want.

Altri suggerimenti

One problem I've noticed is the following:

head=(SyDetails) malloc(sizeof (SyDetails));
....
curr->next=(SyDetails) malloc(sizeof (SyDetails));

It should be:

head=(SyDetails) malloc(sizeof (struct SystemDetails));
....
curr->next=(SyDetails) malloc(sizeof (struct SystemDetails));

because you want to allocated the number of bytes occupied by your structure and not by a pointer to it (SyDetails).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top