473,668 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(R elations));
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 2514
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_FAILU RE);
}

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_FAILU RE);
}

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(R elations));
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, "picknicker 187" <gr****@gmx.d e>
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<stdli b.h>
#include<stdio .h>
#include<strin g.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(R elations));
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(R elations));
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(R elations));
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********@yah oo.com) (cb********@wor ldnet.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
859
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 LOCATE subroutine that returns a node???? Any help would be appreciated. Thanks
0
6877
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 that will allow a restricted list of users to enter jobs in the database. We do not want these users to be able to access all the data. I created an application in Access 2002 that performs this function seemingly quite well. However we now...
4
10847
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 the program. The error happends suddenly and at different times. I load quiet alot of data into my program. I'm wondering if there are anyone witch are familiar with this sort of error, and has some good ideas for what may be wrong. What is...
6
2136
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 one last bit: how to print out the elements? Here is so far what I've got: #include <stdio.h> #include <stdlib.h> struct intRecord
1
2877
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 built a query to filter a master contact list of contacts and I made it an append query to added to the linked Outlook address book table. However, I am getting an error message that says I do not have the necessary permissions to use this object. At...
6
4154
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 try my own small program. The problem is, I seem to be having trouble with memory, i.e. sometimes my program will work and display the correct output, and sometimes it will not and display garbage (in a printf call) so i assume i have been using...
6
3256
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 read my program I appriciate it. the program suppose to print a message on the screen #include <iostream> #include <string>
0
8624
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 anything from a short integer value to a complex struct type, also has a pointer to the next node in the single-linked list. That pointer will be NULL if the end of the single-linked list is encountered. The single-linked list travels only one...
4
7599
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 an excel spreadsheet. There are two tables. One is a list of general contacts, and the other is a list of clubs. The clubs contain members who are within the contacts table. When I add a list of new club members from the
0
8382
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8802
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8586
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7405
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.