GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Oct-2004, 03:10
picknicker187 picknicker187 is offline
New Member
 
Join Date: Oct 2004
Posts: 2
picknicker187 is on a distinguished road

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


CPP / C++ / C Code:
/*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;
}
Last edited by LuciWiz : 08-Oct-2004 at 04:35. Reason: Please insert your C++ code between [c++] [/c++] tags
  #2  
Old 08-Oct-2004, 08:08
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,309
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by picknicker187
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!!!!

I am going to try to help you get this program to run "as soon as possible"

Put printf() statements at various places to see how the program is getting before it bombs out.

For example if you know it is reading the words correctly (the program has printed them out correctly after reading the file). You might try the following:

CPP / C++ / C Code:
 neu=(Relations*)malloc(sizeof(Relations));
  printf("neu = %p, head = %p, buf: %c, word: <%s>, dest: <%s>\n", 
          neu, head, buf, word, dest);
  neu->next=head;
  head = neu;
  neu->relType = buf;
  printf("Getting ready to copy word:\n");
  strcpy(neu->word, word);
  printf("Getting ready to copy dest:\n");
  strcpy(neu->dest, dest);

Now, look at what happens when you execute this.

It not only tells you how far it got, the values that it prints can tell you why there is a problem.

Now, if you fix whatever caused this problem, there may be other things happening that you don't understand. If so, use the same technique to track down the bugs as you find them. printf() is a very powerful debugging tool.

Why do I claim that this gets you there "as soon as possible"? Well, I believe that this will be faster than posting a request for help and waiting for a helpful response.

If you have any specific questions about the actions, people on this forum are usually willing to try to help.

Regards,

Dave
  #3  
Old 08-Oct-2004, 10:44
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
hi,

CPP / C++ / C Code:
strcpy(neu->word, word);
strcpy(neu->dest, dest);

both strcpy destination strings above are dynmaic storages and you have not allocated memory for them before writing into them. Program is trying to write in some random memory location and crashing.

You need to allocated memory for neu->word and neu->dest using either malloc or calloc.

CPP / C++ / C Code:
neu->word=(char *)calloc((sizeof(word))+1,sizeof(char));
neu->dest=(char *)calloc((sizeof(dest))+1,sizeof(char));
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge sort on a linked list Temujin_12 C++ Forum 1 06-Mar-2008 20:33
Insert problem in linked list with two function code Kay Chan C++ Forum 1 03-Sep-2004 09:52
Question about linked list and queue Kay Chan C++ Forum 7 02-Sep-2004 09:27
help on linked lists any1????? nick4 C Programming Language 1 17-May-2004 09:32
[include] list1.h -- Linked list class dsmith C Programming Language 2 04-May-2004 09:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:01.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.