Входной файл имеет целые числа: 3 5 2 9 Связанный список работает, и метод вставки сортирует данные должным образом. Я просто не могу понять, почему это отображается. Я освободил значения указателя и закрыл открытые файлы. Я не очень знаком с использованием C, поэтому я предполагаю, что это связано с неуместным значением указателя? Любая помощь будет оценена.
сообщение об ошибке выглядит следующим образом:
======= Backtrace: =========
/lib/i686/cmov/libc.so.6(+0x6af71)[0xb7639f71]
/lib/i686/cmov/libc.so.6(+0x6c7c8)[0xb763b7c8]
/lib/i686/cmov/libc.so.6(cfree+0x6d)[0xb763e8ad]
./assignment[0x8048779]
/lib/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb75e5ca6]
./assignment[0x80484d1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:01 16674 /home/os/Documents/assignment
08049000-0804a000 rw-p 00000000 08:01 16674 /home/os/Documents/assignment
09379000-0939a000 rw-p 00000000 00:00 0 [heap]
b7400000-b7421000 rw-p 00000000 00:00 0
b7421000-b7500000 ---p 00000000 00:00 0
b75a0000-b75bd000 r-xp 00000000 08:01 245763 /lib/libgcc_s.so.1
b75bd000-b75be000 rw-p 0001c000 08:01 245763 /lib/libgcc_s.so.1
b75ce000-b75cf000 rw-p 00000000 00:00 0
b75cf000-b770f000 r-xp 00000000 08:01 267347 /lib/i686/cmov/libc-2.11.3.so
b770f000-b7710000 ---p 00140000 08:01 267347 /lib/i686/cmov/libc-2.11.3.so
b7710000-b7712000 r--p 00140000 08:01 267347 /lib/i686/cmov/libc-2.11.3.so
b7712000-b7713000 rw-p 00142000 08:01 267347 /lib/i686/cmov/libc-2.11.3.so
b7713000-b7716000 rw-p 00000000 00:00 0
b7723000-b7728000 rw-p 00000000 00:00 0
b7728000-b7729000 r-xp 00000000 00:00 0 [vdso]
b7729000-b7744000 r-xp 00000000 08:01 250358 /lib/ld-2.11.3.so
b7744000-b7745000 r--p 0001b000 08:01 250358 /lib/ld-2.11.3.so
b7745000-b7746000 rw-p 0001c000 08:01 250358 /lib/ld-2.11.3.so
bfe1d000-bfe32000 rw-p 00000000 00:00 0 [stack]
^C
os@debian:~/Documents$
#include <stdio.h>
#include <stdlib.h>
//Source: "geeksforgeeks.org/c-program-bubble-sort-linked-list/"
//Source: "geeksforgeeks.org/fgetc-fputc-c/"
struct Node
{
int value;
struct Node* next;
} Node;
//defining an insert that will put an integer into the linked list
void insert(struct Node *);
//defining a head pointer variable
struct Node* head = NULL;
void main(int argc, char * argv[])
{
//checking to see if the arguments passed were 3
if(argc != 3)
{
printf("Please run as %s [filename]\n", argv[1]);
return;
}
FILE *fp;
//checking if the input file is readable
if(!(fp = fopen(argv[1], "r")))
{
printf("filename %s does not exist", argv[1]);
return;
}
//allocating memory to line
struct Node * line = (struct Node*) malloc(sizeof(struct Node));
printf("made it here \n");
//ensuring the dynamic memory management allocated the memory to line
if(line == NULL)
{
printf("Cannot do dynamic memory management\n");
return;
}
//printf("file in content is %s: \n", argv[1]);
//loop through the file and and put the information into the list
while(fscanf(fp, "%d", &(line->value)) != EOF)
{
line -> next = NULL;
insert(line);
line = (struct Node*) malloc(sizeof(struct Node));
if(line == NULL)
{
printf("No more space to allocate for dynamic memory management\n");
return;
}
printf("%d", line->value);
}
free(line);
//while(fscanf(fp, "%d", &(line->value)) != EOF)
//{
// printf("%d", line->value);
//}
fclose(fp);
printf("Content is in the linked list\n");
//////////////////////////////////////////
FILE *f;
//checking if the output file is valid
if(!(f = fopen(argv[2], "w")))
{
printf("file %s not valid \n", argv[2]);
return;
}
//print the sorted list to the output file and move the pointer to the next //node.
while(head != NULL)
{
line = head;
head = head->next;
fprintf(f, "%d ", line->value);
free(line);
}
printf("Content is in %s [filename] as follows: \n", argv[2]);
fclose(f);
}
void insert(struct Node * element)
{
struct Node * temp = head;
struct Node * pretemp = NULL;
while(temp != NULL && temp->value < element->value)
{
pretemp = temp;
temp = temp->next;
}
if(pretemp == NULL)
{
element->next = head;
head = element;
}
else
{
pretemp->next = element;
element->next = temp;
}
}