Thread: Program Terminating With Error On Exit

  1. #1
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32

    Program Terminating With Error On Exit

    Hi,

    I have a program which when i select the quit option it terminates borland 3.1 with errors or just terminates.

    Below is a snippet of my code. I cannot see anythin which should be causing an error, however i am still fairly new to C.

    Code:
    case 'J':
    				/* Save Database */
    				if(saveDatabaseFile(ptrDatabase, fname, &counter, &lastRecordID, &changesMade) == 1){
    					printf("\n\n Database Saved!\n\n Press Any Key To Continue >");
    					getch();
    					clrscr();
    				}
                                    break;
    			case 'Q':
    				/* Exit */
    				if(changesMade == 1){
    					printf("\n Changes Have Been Made To The Database...\n\n Would You Like To Save These Changes? (Y/N/C) >");
    
    					do{
    						choice = toupper(getchar());
    					}while((choice != 'N') && (choice != 'Y') && (choice != 'C'));
    
    					if(choice == 'Y'){
    						saveDatabaseFile(ptrDatabase, fname, &counter, &lastRecordID, &changesMade);
    						return 1;
    					}else if(choice == 'N'){
    						return 1;
    					}
    					else if(choice == 'C'){
    						clrscr();
    						break;
    					}
    				}
    				else {
    					return 1;
    				}
    
    				break;
    			default:
    				printf("\n Error: Selection was not reconised...Please select again\n\n");
    				break;
    If anyone could shed some light on this problem it would be great. The program works fine untill the quit option is selected which would surgest that its only a problem with that section of code, however i do not know.

    Incedently it does run the saveDatabaseFile(...) function so i know it gets this far.

    main has a return type of int.

    Cheers in advance

    Chris

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's probably your computer's way of telling your your compiler is too damn old.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    unfortunatly i have to use 3.1 as its what the university use. apparently they think by using an old dos based version of the compiler we will learn the code better.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's a completely brain-dead belief, if that's really their reasoning. They should jump on the first compiler to support C99 immediately.

    Why exactly do you return 1, btw? If that code is in main(), 1 signals that something went wrong during program execution.

    You have far too much code inside the switch anyway. Separate it into functions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    i was just returning a value to exit the program. thats how we have been told to do it. I'll change it to 0.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Does the program crash if no changes had been made, (i.e. that it doesn't go through the inner loop), or if it does go through the loop does it crash if 'C' is entered?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    if not changes have been made the program crashes.

    if C is entered when changes have been made the program returns to the menu and does not crash.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Incedently it does run the saveDatabaseFile(...) function so i know it gets this far.
    Yeah, but that doesn't mean it didn't do any damage which causes something else to fall over later on (as it seems to be the case here).

    All the code you've posted so far seems pretty straight forward - how about posting the inside of that function you call?

    > saveDatabaseFile(ptrDatabase, fname, &counter, &lastRecordID, &changesMade);
    What are the types of all these variables?
    Are they pointers or arrays?
    If they're pointers, how much did you malloc - show your malloc calls.
    If they're arrays, how big did you make them?

  9. #9
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    Code:
    int saveDatabaseFile(RECORD *, char *, int *, int *, int *);
    int loadDatabaseFile(RECORD *, char *, int *, int *, int *);
    
    int ReallocateMemory(RECORD *, int *);
    int AllocateMemory(RECORD *);
    
    int saveDatabaseFile(RECORD * ptrDatabase, char * fname, int * counter, int * lastRecordID, int * changesMade){
    	RECORD * ptrNew;
    	int i;
    
    	FILE * ptrFile;
    
    	clrscr();
    
    	ptrFile=fopen(fname, "wb");
    
    	if(ptrFile==NULL){
    		printf("\n Error Opening Database File!\n\n Please Press Any Key To Continue >");
    		getch();
    
    		clrscr();
    
    		return 0;
    	}
    
    	ptrNew = ptrDatabase;
    
    	fwrite(lastRecordID, sizeof(int), 1, ptrFile);
    
    	for (i=0; i < * counter;  i++){
    		if(ptrNew->ID != 0){
    			fwrite(ptrNew, sizeof(RECORD), 1, ptrFile);
    		}
    
    		ptrNew++;
            }
    
    	fclose(ptrFile);
    
    	* changesMade = 0;
    
    	return 1;
    }
    
    int loadDatabaseFile(RECORD * ptrDatabase, char * fname, int * counter, int * lastRecordID, int * currentMemSize){
    	RECORD temp;
    	RECORD * ptrNew;
    	int i, length;
    	char buffer[100];
    
    	FILE * ptrFile;
    
    	ptrFile=fopen(fname, "rb");
    
    	if(ptrFile==NULL){
    		printf("\n Error Opening Database File!\n\n Please Press Any Key To Continue >");
    		getch();
    
    		clrscr();
    
    		return 0;
    	}
    
    	ptrNew = ptrDatabase;
    
    	fread( lastRecordID, sizeof(int), 1, ptrFile );
    
    	while(fread( &temp, sizeof(RECORD), 1, ptrFile )){
    		if(*counter >= *currentMemSize){
    			if(ReallocateMemory(ptrDatabase, currentMemSize) == 0){
    				printf("An Error Has Occured Reallocating Memory.\n\nPress Any Key To Continue.");
    				getch();
    				return 0;
    			}
    		}
    
    		*ptrNew = temp;
    
    		ptrNew++;
    		(*counter)++;
    	}
    
    	fclose(ptrFile);
    
    	return 1;
    }
    
    int ReallocateMemory(RECORD * ptrDatabase, int * currentMemSize) {
    	RECORD * ptrNew;
    
    	ptrNew = (RECORD *) realloc(ptrDatabase, (*currentMemSize + memIncrease) * sizeof(RECORD));
    	if (ptrNew == NULL){
    		return 0;
    	}
    	else{
    		ptrDatabase = ptrNew;
    		*currentMemSize += memIncrease;
    
    		return 1;
    	}
    }
    
    int AllocateMemory(RECORD * ptrDatabase) {
    	ptrDatabase = (RECORD *) calloc(memIncrease, sizeof(RECORD));
    
    	if (ptrDatabase == NULL){
    		return 0;
    	}
    	else{
    		return 1;
    	}
    }
    These are the functions to do with memory allocation and file i/o. Thanks alot for all your help.

    Chris

    ps. anythin else you need to know just let me know

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, neither your alloc or realloc functions return a modified pointer back to the caller.

    Use the debugger to check
    RECORD *ptr = NULL;
    AllocateMemory(ptr);
    // ptr will still be NULL here, no matter what you do.

  11. #11
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    i do not understand. It doesn't need to return a valid pointer becuase i pass the address of the pointer into the function. Could i see some code relating to the modifications you recomend. Thanks

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > It doesn't need to return a valid pointer becuase i pass the address of the pointer into the function.
    a) you didn't try my example in a test program
    b) you're passing pointers by value.
    c) look at malloc and realloc - they do ptr = function(), not function(ptr)

    You're not passing an address, you're passing a copy of the current value of the pointer.

  13. #13
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    I see what you mean by the NULL. Would this be right now. I have modified the functions

    Code:
    RECORD * ReallocateMemory(RECORD * ptrDatabase, int * currentMemSize) {
    	RECORD * ptrNew;
    
    	ptrNew = (RECORD *) realloc(ptrDatabase, (*currentMemSize + memIncrease) * sizeof(RECORD));
    	if (ptrNew == NULL){
    		return NULL;
    	}
    	else{
    		*currentMemSize += memIncrease;
    
    		return ptrNew;
    	}
    }
    
    RECORD * AllocateMemory() {
    
    RECORD * ptrNew;
    ptrNew = (RECORD *) calloc(memIncrease, sizeof(RECORD)); if (ptrNew == NULL){ return NULL; } else{ return ptrNew; } }
    and then do somethin like

    Code:
    ptrDatabase = AllocateMemory();
    I am fairly new to C and am still trying to get my head around pointers.

    Thanks again for your help

  14. #14
    Registered User chriscolden's Avatar
    Join Date
    Jan 2006
    Posts
    32
    i have got allocateMemory 2 return and set ptrDatabase. However I'm not sure how i would get reallocateMemory to work as Main calls createEntry which then in turn calls reallocateMemory. is there no way i could pass a referance into the function an not a value so the function can directly modify the memory (ptrDatabase).

    Chris

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, you declare something like
    void myFunc ( RECORD **ptr );

    You would call it with
    RECORD *myPtr = NULL;
    myFunc ( &myPtr );

    And within the function, you would have something like
    *ptr = malloc ( );


    > I am fairly new to C and am still trying to get my head around pointers.
    Try this - http://pw1.netcom.com/~tjensen/ptr/pointers.htm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Dialog Box causes program to exit
    By norbs27 in forum Windows Programming
    Replies: 10
    Last Post: 01-14-2008, 10:09 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Program uses a lot of memory and doesnt exit properly
    By TJJ in forum Windows Programming
    Replies: 13
    Last Post: 04-28-2004, 03:13 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM