bus error in linked list

P

picknicker187

hi,

the program below is supposed to read data out of a txt file into an array
and the second part of the data to a linked list.

8 is the number of words, the 10 terms below represent relations between
the words, but this is not important.
the txt file looks like this:

8
Auto
Fahrzeug
Maschine
Boot
Nachen
Kraftfahrzeug
Blechkiste
Gegenstand
H Fahrzeug Auto
H Fahrzeug Boot
S Auto Kraftfahrzeug
S Auto Blechkiste
S Boot Nachen
S Nachen Boot
H Maschine Auto
Y Auto Fahrzeug
Y Boot Fahrzeug
Y Fahrzeug Gegenstand

the problem is in the last part of the program, the linked list. the
program compiles good, but everytime i run it, it makes a bus error. i´ve
tried so many things, but it just keeps doing it. maybe there´s a logic
mistake in the list or something i can´t see and someone else will see
right up. please help me with this one, i need this program to run as soon
as possible.

thanks!!!!

bastian


/*reads words to an array and to a linked list*/
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

int x, y;

typedef struct relations {
char relType;
char *word;
char *dest;
struct relations *next;
}Relations;

int main(void)
{
FILE *fp;
char buf, word[80], dest[80];

Relations *neu = NULL;
Relations *head = NULL;
Relations *aktuell = head;

if( (fp = fopen("eingabe.txt", "r")) == NULL)
{
fprintf(stderr, ("Fehler\n"));
exit(1);
}

fscanf(fp, "%d", &x);
char words[x][20];

for(y=0; y < x; y++)
{
fscanf(fp, "%s", words[y]);
}

for(y=0; y < x; y++)
{
printf("%s\n", words[y]);
}
fscanf(fp, "%c", &buf);
fscanf(fp, ">%c %s %s\n", &buf, word, dest);

neu=(Relations*)malloc(sizeof(Relations));
neu->next=head;
head = neu;
neu->relType = buf;
strcpy(neu->word, word);
strcpy(neu->dest, dest);

aktuell=head;
while(!feof(fp)){
fscanf(fp, ">%c %s %s\n", &buf, word, dest);
while (aktuell -> next) aktuell = aktuell -> next;
neu = (Relations*) malloc(sizeof(Relations));
aktuell -> next = neu;
neu -> next = NULL;
neu->relType = buf;
strcpy(neu->word, word);
strcpy(neu->dest, dest);
printf("%c", buf);
}

fclose(fp);
return 0;
}n
 
T

Thomas Stegen

picknicker187 said:
/*reads words to an array and to a linked list*/
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

int x, y;

typedef struct relations {
char relType;
char *word;
char *dest;
struct relations *next;
}Relations;

int main(void)
{
FILE *fp;
char buf, word[80], dest[80];

Relations *neu = NULL;
Relations *head = NULL;
Relations *aktuell = head;

if( (fp = fopen("eingabe.txt", "r")) == NULL)
{
fprintf(stderr, ("Fehler\n"));
exit(1);

Don't use exit(1) as 1 is not guaranteed to be meaningful in any way.
Use EXIT_FAILURE (from stdio.h) instead.

exit(EXIT_FAILURE);
}

fscanf(fp, "%d", &x);
char words[x][20];

Hrm, this won't work. Unless you are using C99. You probably aren't
using C99. (And a bus error rhymes very well with this kind of
problem.) Also declare all variables at the beginning of a scope

Try

char (*words)[20]; /*A pointer to an array of 20 chars*/

words = malloc(x * sizeof *words); /*notice this format of this and
compare it to what you have below, this format I have here is
recommended, the details are in the devil, uh, I mean the archives.
Google!*/

if(words == NULL)
{
fprintf(stderr, ("Fehler\n"));
exit(EXIT_FAILURE);
}
for(y=0; y < x; y++)
{
fscanf(fp, "%s", words[y]);
}

Note that this can overflow if the text on the line is to long.

fscanf(fp, "%19s", words[y]);

The above limits the number of characters read to 19 leaving room for
null terminator. Still slightly problematic though. What if there are
more than 19 characters on the line? The next call to fscanf will read
those instead of the line it should read.

Try fgets in conjunction with something else

for(y=0; y < x; y++)
{
size_t length;
fgets(words[y], 20, fp);
length = strlen(words[y]);

/*remove newline if it is there*/
if(words[y][length] == '\n')
{
words[y][length] == '\0';
}
else
{
/*clear until end of line*/
while(!feof(fp) && getc(fp) != '\n')
/*do nothing*/ ;
}
}
for(y=0; y < x; y++)
{
printf("%s\n", words[y]);
}
fscanf(fp, "%c", &buf);
fscanf(fp, ">%c %s %s\n", &buf, word, dest);

neu=(Relations*)malloc(sizeof(Relations));

See above for a comment on proper malloc usage.
neu->next=head;
head = neu;
neu->relType = buf;
strcpy(neu->word, word);

OK, where does the memory neu->word is pointing? Answer this
and your problem might be solved.

(HINT: WYSIWYG, do you see memory being allocated anywhere?)
strcpy(neu->dest, dest);

Same here.
aktuell=head;
while(!feof(fp)){
fscanf(fp, ">%c %s %s\n", &buf, word, dest);
while (aktuell -> next) aktuell = aktuell -> next;
neu = (Relations*) malloc(sizeof(Relations));
aktuell -> next = neu;
neu -> next = NULL;
neu->relType = buf;
strcpy(neu->word, word);
strcpy(neu->dest, dest);
printf("%c", buf);
}

Don't really have time to comment on the above, but there are some
of the same problems in there.

Keep at it, I've seen much worse. You are not the first or the last to
be tripped up by this. Also try cranking up the warning levels on your
compiler.

If, by any chance, you are using gcc try the following command line
gcc -Wall -W -ansi <infile> -o <outfile>

You have many more, but the above should be enough.

HTH.
 
B

Barry Schwarz

hi,

the program below is supposed to read data out of a txt file into an array
and the second part of the data to a linked list.

snip description of text file
the problem is in the last part of the program, the linked list. the
program compiles good, but everytime i run it, it makes a bus error. i´ve
tried so many things, but it just keeps doing it. maybe there´s a logic
mistake in the list or something i can´t see and someone else will see
right up. please help me with this one, i need this program to run as soon
as possible.

thanks!!!!

bastian


/*reads words to an array and to a linked list*/
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

int x, y;

typedef struct relations {
char relType;
char *word;
char *dest;
struct relations *next;
}Relations;

int main(void)
{
FILE *fp;
char buf, word[80], dest[80];

Relations *neu = NULL;
Relations *head = NULL;
Relations *aktuell = head;

if( (fp = fopen("eingabe.txt", "r")) == NULL)
{
fprintf(stderr, ("Fehler\n"));
exit(1);
}

fscanf(fp, "%d", &x);

Input operations should always be checked for success.
char words[x][20];

This is non-standard before C99, due to both the variable dimension
and the definition not at the beginning of a block.
for(y=0; y < x; y++)
{
fscanf(fp, "%s", words[y]);
}

for(y=0; y < x; y++)
{
printf("%s\n", words[y]);
}
fscanf(fp, "%c", &buf);
fscanf(fp, ">%c %s %s\n", &buf, word, dest);

neu=(Relations*)malloc(sizeof(Relations));
neu->next=head;
head = neu;
neu->relType = buf;
strcpy(neu->word, word);
strcpy(neu->dest, dest);

aktuell=head;
while(!feof(fp)){
fscanf(fp, ">%c %s %s\n", &buf, word, dest);
while (aktuell -> next) aktuell = aktuell -> next;
neu = (Relations*) malloc(sizeof(Relations));
aktuell -> next = neu;
neu -> next = NULL;
neu->relType = buf;
strcpy(neu->word, word);

Here is one of your problems. neu points to a newly allocated area of
memory which is uninitialized. Therefore, the value of neu->word is
indeterminate (it probably does not point to memory you own). You
cannot copy into the area it "points to". This invokes undefined
behavior of a sort that often manifests itself as a bus error.
strcpy(neu->dest, dest);
Ditto.

printf("%c", buf);
}

fclose(fp);
return 0;
}n



<<Remove the del for email>>
 
C

CBFalconer

picknicker187 said:
the program below is supposed to read data out of a txt file into
an array and the second part of the data to a linked list.

8 is the number of words, the 10 terms below represent relations
between the words, but this is not important.

the txt file looks like this:

8
Auto
Fahrzeug
Maschine
Boot
Nachen
Kraftfahrzeug
Blechkiste
Gegenstand

the problem is in the last part of the program, the linked list.
the program compiles good, but everytime i run it, it makes a bus
error. i´ve tried so many things, but it just keeps doing it.
maybe there´s a logic mistake in the list or something i can´t
see and someone else will see right up. please help me with this
one, i need this program to run as soon as possible.

After reformatting to something sane, gcc reveals the following
errors. You are doing illegal things. Also, do not cast the
return value from malloc.

/*reads words to an array and to a linked list*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int x, y;

typedef struct relations {
char relType;
char *word;
char *dest;
struct relations *next;
} Relations;

int
main(void)
{
FILE *fp;
char buf, word[80], dest[80];

Relations *neu = NULL;
Relations *head = NULL;
Relations *aktuell = head;

if ((fp = fopen("eingabe.txt", "r")) == NULL) {
fprintf(stderr, ("Fehler\n"));
exit(1);
}

fscanf(fp, "%d", &x);
char words[x][20];

junk.c:31: warning: ISO C89 forbids variable-size array `words'
junk.c:31: warning: ISO C89 forbids mixed declarations and code

for (y = 0; y < x; y++) {
fscanf(fp, "%s", words[y]);
}

for (y = 0; y < x; y++) {
printf("%s\n", words[y]);
}
fscanf(fp, "%c", &buf);
fscanf(fp, ">%c %s %s\n", &buf, word, dest);

neu = (Relations *) malloc(sizeof(Relations));
neu->next = head;
head = neu;
neu->relType = buf;
strcpy(neu->word, word);
strcpy(neu->dest, dest);

aktuell = head;
while (!feof(fp)) {
fscanf(fp, ">%c %s %s\n", &buf, word, dest);
while (aktuell->next)
aktuell = aktuell->next;
neu = (Relations *) malloc(sizeof(Relations));
aktuell->next = neu;
neu->next = NULL;
neu->relType = buf;
strcpy(neu->word, word);
strcpy(neu->dest, dest);
printf("%c", buf);
}

fclose(fp);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top