Improved error handling for out of memory problems.

master
penguinc 2005-09-01 22:01:20 +00:00
parent a2efe57668
commit 2c0aadbaae
4 changed files with 35 additions and 21 deletions

View File

@ -4,15 +4,10 @@
#include "List.h"
void FatalError ( const char *ErrorMessage ) {
printf ( "%s\n", ErrorMessage );
exit ( 1 );
}
void MakeList ( List *mylist, short (*Compare) ( const Element Data1, const Element Data2 ) ) {
mylist->RootNode = malloc ( sizeof( Node ) );
if ( mylist->RootNode == NULL )
FatalError ( "MakeList: Could not allocate a Node" );
return; /* out of memory */
mylist->Compare = Compare;
mylist->RootNode->Previous = NULL;
mylist->RootNode->Next = NULL;
@ -50,12 +45,12 @@ Node *Get ( List *mylist, unsigned short index ) {
return GetPtr;
}
void InsertAt ( List *mylist, Element Data, Node *Pos ) {
short InsertAt ( List *mylist, Element Data, Node *Pos ) {
Node *New;
New = malloc ( sizeof( Node ) );
if ( New == NULL )
FatalError ( "InsertAt: Could not allocate new Node" );
return 0;
New->NodeElement = Data;
if ( mylist->size == 0 ) {
@ -64,7 +59,7 @@ void InsertAt ( List *mylist, Element Data, Node *Pos ) {
mylist->RootNode->Previous = New;
mylist->RootNode->Next = New;
mylist->size++;
return;
return 1;
}
mylist->size++;
@ -76,14 +71,15 @@ void InsertAt ( List *mylist, Element Data, Node *Pos ) {
}
if ( New->Previous != NULL )
New->Previous->Next = New;
return 1;
}
short IsEmpty ( List *mylist ) {
return mylist->size == 0;
}
void Push ( List *mylist, Element Data ) {
InsertAt ( mylist, Data, mylist->RootNode->Next );
short Push ( List *mylist, Element Data ) {
return InsertAt ( mylist, Data, mylist->RootNode->Next );
}
Element Pop ( List *mylist ) {
@ -100,8 +96,8 @@ Element Pop ( List *mylist ) {
return data;
}
void Enqueue ( List *mylist, Element Data ) {
InsertAt ( mylist, Data, mylist->RootNode );
short Enqueue ( List *mylist, Element Data ) {
return InsertAt ( mylist, Data, mylist->RootNode );
}
Element Dequeue ( List *mylist ) {

6
List.h
View File

@ -25,17 +25,17 @@ Node *Find ( List *mylist, Element Data );
/* Returns the index-th item in the List */
Node *Get ( List *mylist, unsigned short index );
/* Inserts Data in front of Node Pos into the List */
void InsertAt ( List *mylist, Element Data, Node *Pos );
short InsertAt ( List *mylist, Element Data, Node *Pos );
/* Returns true if list size is 0 */
short IsEmpty ( List *mylist );
/* Pushes Data into mylist as if it were a stack */
void Push ( List *mylist, Element Data );
short Push ( List *mylist, Element Data );
/* Pops the top Element off mylist, must free the returned Element when done */
Element Pop ( List *mylist );
/* Enqueues Data as if List were a queue */
void Enqueue ( List *mylist, Element Data );
short Enqueue ( List *mylist, Element Data );
/* Dequeues the front Element off mylist, must free the returned Element when done */
Element Dequeue ( List *mylist );

View File

@ -12,4 +12,4 @@ $(SRCS):
$(CC) $(CFLAGS) -o $@ $*.c
clean:
@rm *.o
@rm *.o $(O_TARGET)

24
main.c
View File

@ -39,6 +39,13 @@ short CompareString ( const Element Data1, const Element Data2 ) {
return strcmp ( Data1, Data2 );
}
void OutOfMemory ( List *mylist ) {
printf ( "Ran out of memory!\n" );
if ( mylist != NULL && mylist->RootNode != NULL )
MakeEmpty ( mylist );
exit ( 1 );
}
#ifdef USE_TI92PLUS
void _main ( void )
@ -59,6 +66,8 @@ int main ( void )
#endif
MakeList ( &mylist, CompareString );
if ( mylist.RootNode == NULL )
OutOfMemory ( &mylist );
PrintList ( &mylist );
for ( i = 0; i < 10; i++ ) {
@ -72,15 +81,24 @@ int main ( void )
tmp = malloc ( sizeof( Element ) * 3 );
sprintf ( tmp, "%d", 20 );
FindPtr = Find ( &mylist, (Element)"3" );
InsertAt ( &mylist, tmp, FindPtr );
if ( !InsertAt ( &mylist, tmp, FindPtr ) ) {
free ( tmp );
OutOfMemory ( &mylist );
}
tmp = malloc ( sizeof( Element ) * 4 );
sprintf ( tmp, "%s", "FOO" );
Enqueue ( &mylist, tmp );
if ( !Enqueue ( &mylist, tmp ) ) {
free ( tmp );
OutOfMemory ( &mylist );
}
tmp = malloc ( sizeof( Element ) * 4 );
sprintf ( tmp, "%s", "BAR" );
Enqueue ( &mylist, tmp );
if ( !Enqueue ( &mylist, tmp ) ) {
free ( tmp );
OutOfMemory ( &mylist );
}
PrintList ( &mylist );
MakeEmpty ( &mylist );