Thread: Program is crashing since I added a new function. Where have I gone wrong?? :S

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    14

    Exclamation Program is crashing since I added a new function. Where have I gone wrong?? :S

    I've made a program that takes information from a list of mp3s in a text file and stores them into a structure, which is then looked up using an array of pointers.
    Each mp3 has the info: Track No - Title - Artist - Album - Genre
    My program was working fine until tonight when I added a function that looks up all the different genres of music and displays a list of the genres to the user (I later want to allow the user to select one of these genres and have a playlist created with all the mp3s in this genre).
    Now whenever I execute the program, it stops when it reaches the part when the new function is used, saying:
    Code:
    lab1.exe has encountered a problem and needs to close.  We are sorry for the inconvenience.
    I tried to Debug, and it gave me this message
    Code:
    Unhandled exception in mp3list.exe: 0xC0000005: Access violation
    Does anyone know what the problem might be?
    Here is the code I have written. The function that's causing the problem is the 'lookupgenres' function.

    Code:
    /* A program to take information from a .txt file and store it in structures
       which can be looked up via an array of pointers */
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <console_lib.h>
    #include <conio.h>
    
    typedef struct mp3info
    {
    	char title[100];
    	char artist[100];
    	char album[100];
    	char genre[100];
    } SONG;
    
    void lookupgenres ( SONG *array[20001] )
    {
    	typedef struct genrelist
    	{
    		char name[100];
    		struct genrelist *next;
    	} GENRES;
    
    	int tracknumber;
    	GENRES genre;
    	GENRES *current;
    	int compare;
    	int count;
    
    	/* Check through all tracks for different genres */
    
    	for ( tracknumber = 1; tracknumber <= 100; tracknumber++ )
    	{
    		/* Check to see if genre has been added */
    
    		/* If the genre list is empty, the check does not need to be made and the
    		   genre of the first track can be added to the list */
    
    		if ( genre.name == NULL )
    			strcpy ( genre.name, array[tracknumber]->genre );
    		else
    		{
    			compare = 1;
    			current = &genre;
    
    			compare = strcmp ( current->name, array[tracknumber]->genre );
    
    			while ( ( compare != 0 ) && ( current->next != NULL ) )
    			{
    				current = current->next;
    				compare = strcmp ( current->name, array[tracknumber]->genre );
    			}
    
    			/* If the genre has not been added to the list, add it */
    
    			if ( compare != 0 )
    			{
    				current = &genre;
    				while ( current->next != NULL )
    					current = current->next;
    				current->next = malloc ( sizeof ( GENRES ) );
    				current = current->next;
    				strcpy ( current->name, array[tracknumber]->genre );
    			}
    		}
    	}
    
    	current = &genre;
    
    	for ( count = 1; count != 0 ; count++ )
    	{
    		printf ( "%d - %s\n", count, current->name );
    		if ( current->next != NULL )
    			current = current->next;
    		else
    			count = 0;
    	}
    }
    
    /* ------------------------------------------------------------------------ */
    
    int main (void)
    {
    	char string[500];
    	int tracknumber;
    	SONG *current;
    	SONG *array[20001];
    
    	/* Open file mp3list.txt for reading */
    
    	FILE *fp;
    	fp = fopen ( "C:\\mp3list.txt", "r" );
    
    	while ( tracknumber < 100 )
    	{
    		fgets ( string, 500, fp );
    		current = store ( string, &tracknumber );
    		array[tracknumber] = current;
    		printf ( "%d - %s - %s - %s - %s", tracknumber, current->title, current->artist, current->album, current->genre );
    	}
    
    	printf ( "Loading Complete!\n\n");
    
    	lookupgenres ( array );
    
    	return 0;
    
    }
    If anyone has any idea where I might be going wrong with this the help would be really appreciated!

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    That appears to be some sort of segfault. Could be your store function. At least, that's what I think it is, right now.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Use a debugger, makes it easy to find where you are segfaulting.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Array indexes start at zero, not one.
    Code:
    for ( tracknumber = 1; tracknumber <= 100; tracknumber++ )
    	{
    		/* Check to see if genre has been added */
    
    		/* If the genre list is empty, the check does not need to be made and the
    		   genre of the first track can be added to the list */
    
    		if ( genre.name == NULL )
    			strcpy ( genre.name, array[tracknumber]->genre );
    Also, genre.name will never be NULL because it is an array, not a pointer.
    Code:
    GENRES genre;
    GENRES *current;
    You can't use these pointers to walk through your array, and you genre->next isn't ever pointed to anything useful. So anything you do with it is undefined behavior (ie: bad). Such as:
    Code:
    while ( ( compare != 0 ) && ( current->next != NULL ) )
    			{
    				current = current->next;
    That'll get you started.
    My program was working fine until tonight when I added a function
    Very tempting to just say "I bet it's your new function."

    Guess what? I'd have been right.


    Quzah.
    Last edited by quzah; 01-12-2007 at 11:27 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if ( genre.name == NULL )
    always false
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > current = store ( string, &tracknumber );
    It's also possible that this is also messing with memory given that your new function shows weaknesses.

    When dealing with memory problems, cause and effect are usually in two very different places in the code. Meaning you might get to the point where nobody can see anything wrong with lookupgenres(), yet its still crashing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM