473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

bus error in linked list

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

Nov 14 '05 #1
3 2481
picknicker187 wrote:

/*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.

--
Thomas.
Nov 14 '05 #2
On Fri, 08 Oct 2004 05:25:27 -0400, "picknicker187" <gr****@gmx.de>
wrote:
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>>
Nov 14 '05 #3
picknicker187 wrote:

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.


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;
}

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
0
by: crypto_solid via AccessMonster.com | last post by:
I have been using a SQL database with a VB5 frontend for about 5 years. Works well. Unfortunately I don't have access to the source code. I was tasked with implementing a "job entry" application...
4
by: Stian Karlsen | last post by:
Hi. I'm getting an error in my program. It doesn't occur each time even if the same things happend in my program each time I run it. There will however be different values used for calculations in...
6
by: mattmao | last post by:
Okay, this is just my exercise in order to prepare for the coming assignment regarding the damned Linked List issue... The task is simple and I am about to finish it. However, I couldn't go around...
1
by: Lpitt56 | last post by:
I am running MS Access 2007 and I want to update an Outlook Address book from my Access Database. I started out by importing the Outlook Address Book as a linked table and it linked fine. I then...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
6
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
4
by: franc sutherland | last post by:
Hello, I am using Access 2003. I am having trouble trapping the "can't append all the records in the append query" error message when appending data to a query from a table which is linked to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.