need C help
Microstation32 Moderator
mstat32 at ingr.com
Wed Apr 12 07:41:32 AEST 1989
Hi Guys,
We are having a problem with creating/freeing linked lists.
This program was written with TurboC very late at night in a
tavern.:-) If you got an extra free minute and a set of big
brains would you please take a look??!! Thanks!!!!
/* THIS PROGRAM JUST BUILDS A LINKED LIST OF STRUCTURES CONTAINING
THE NAMES OF THE FILES IN A DIRECTORY. IT THEN FREES EACH NODE
IN THE LIST AND REPEATS THE PROCESS AGAIN. THE LIST BUILDS AND
FREES CORRECTLY THE FIRST TIME THROUGH THE LOOP BUT NOT ANY TIME
THEREAFTER. YOU WILL NOTICE THE FIRST TIME THROUGH, AS IT IS
BUILDING THE LIST AND DISPLAYING THE FILENAMES AND POINTER FIELDS,
THAT THE RIGHT POINTER OF THE LAST NODE IS NULL AS IT IS APPENDED
AND JUST BEFORE THE LIST IS FREED. THE SECOND TIME THROUGH, THE
RIGHT POINTER OF THE LAST NODE IS NULL AS IT IS APPENDED BUT JUST
BEFORE IT IS FREED, IT IS NOT NULL. WHY???????
THE MEMORY IS FREED ONLY THE FIRST TIME "QUE_CLOSE" IS CALLED AND
NOT ON ANY SUBSEQUENT CALL. WHY????
THE OTHER FUNCTIONS ASSOCIATED WITH THIS FILE ARE IN "FUNCQUE.C".
THIS SMALL PROGRAM IS THE ROOT OF A MUCH LARGER PROGRAM I AM
WORKING ON. IT WOULD BE GREATLY APPRECIATED IF ANYONE HAS ANY
COMMENTS, SUGGESTIONS, OR FIXES TO THIS PROBLEM.
THANKS IN ADVANCE!!!! John S.
*/
/* This was originally "LISTHEAD.H" and included here. */
#include <ctype.h>
#include <bios.h>
#include <alloc.h>
#include <dos.h>
#include <conio.h>
#include <dir.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <graphics.h>
#include "mouse.h"
extern struct A_File
{
char File_Name[14];
struct A_File *left;
struct A_File *right;
};
extern int First_file;
extern struct A_File *Top;
extern struct A_File *Last;
extern struct A_File *Win_ptr;
/************END OF "LISTHEAD.H**************************/
/***********Main begins here*****************/
char Buf[80]; /* Temporary buffer to store filename */
char Name_Buf[14];
char ch;
int i,z;
int not_quitting; /* Flag to signal eof */
int Counter = 0;
int Num_Lines = 0;
struct A_File *p;
int Num_Files;
int First_file;
struct A_File *Top=NULL; /* points to first node in list */
struct A_File *Last=NULL; /* points to last node in list */
struct A_File *Win_ptr=NULL; /* Head pointer of linked list */
FILE *fp;
main()
{
int filecount=0;
Num_Files=0;
for (z=0; z<6; ++z)
{
not_quitting = 1;
system("dir>list.dat");
printf("Last right is %p", Last->right); /* display last node's right ptr
just before we free */
if (Win_ptr != NULL)
{
printf("Win_ptr before free is %p\n", Win_ptr);
Que_Close(filecount);
printf("Win_ptr after free is %p\n", Win_ptr);
}
filecount=0;
fp = fopen("list.dat", "r");
while(!feof(fp))
{
fgets(Buf,80,fp);
++Num_Lines;
}
fclose(fp);
fp = fopen("list.dat", "r");
/* BUILD LIST OF FILES */
First_file = 1;
fgets(Buf,80,fp); /* get rid of junk lines before actual file names */
fgets(Buf,80,fp);
fgets(Buf,80,fp);
fgets(Buf,80,fp);
Counter = 4;
do {
++Counter;
if (Counter != (Num_Lines - 1))
{
fgets(Buf,80,fp);
if (First_file)
{
if((p=(struct A_File *)malloc(sizeof(struct A_File)))==NULL)
{
printf("allocation error - aborting");
exit(0);
}
}
else
{
if((p->right=(struct A_File *)malloc(sizeof(struct A_File)))==NULL)
{
printf("allocation error - aborting");
exit(0);
}
p = p->right;
}
ch = Buf[13];
if (isspace(ch)) /* if it is not a directory, add to list */
{
for(i=0; i<14; ++i)
Name_Buf[i] = Buf[i];
Name_Buf[13] = '\0';
strcpy(p->File_Name, Name_Buf);
printf("p's address before append is %p\n", p);
Que_Append(p);
filecount = filecount + 1;
Num_Files = Num_Files + 1;
}
}
else
not_quitting = 0;
}while (not_quitting);
fclose (fp);
}
} /* END PROGRAM */
/** THESE FUNCTIONS ARE IN A SEPERATE FILE: "LISTQUE.C" **/
/* FUNCTION TO CLOSE LINKED LIST */
Que_Close()
{
struct A_File *d;
struct A_File *dnext;
printf("Memory before free is %u\n", coreleft());
printf("win ptr inside is %p\n", Win_ptr);
for (d = Win_ptr; d != NULL; d = dnext)
{
printf("file to free = %s\n", d->File_Name);
dnext = d->right;
printf("dnext is %p\n", dnext);
free (d);
}
printf("Memory free after is %u\n", coreleft());
Win_ptr = NULL;
Top = NULL;
Last = NULL;
} /* END FUNCTION QUE_CLOSE 0 */
/* FUNCTION TO APPEND A LINK TO WINDOW LIST */
Que_Append (p)
struct A_File *p;
{
printf("p right before set to null is %p\n", p->right);
p->right = NULL;
if (First_file)
{
Win_ptr = p;
Top = p;
Last = p;
p->left = NULL;
First_file = 0;
}
else
{
Last->right = p;
p->left = Last;
Last = p;
}
printf("File appended was %s with addr %p\n",p->File_Name,p);
printf("p right after set to null is %p\n", p->right);
} /* END FUNCTION QUE_APPD */
/*** END OF FILE "LISTQUE.C" *****/
mailpath ingr!b11!strato!edward
"I don't need no quotes!"
More information about the Comp.lang.c
mailing list