Path: utzoo!attcan!uunet!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!ub!rutgers!orstcs!bionette.cgrb.orst.edu!duvalj From: duvalj@bionette.cgrb.orst.edu (Joe Duval) Newsgroups: comp.os.msdos.programmer Subject: malloc() and free() is order important Message-ID: <20681@orstcs.CS.ORST.EDU> Date: 1 Oct 90 21:26:25 GMT Sender: usenet@orstcs.CS.ORST.EDU Reply-To: duvalj@bionette.cgrb.orst.edu (Joe Duval) Distribution: na Organization: Oregon State University - CMBL Lines: 75 Hi, I am using Turbo C on a 386-20 with 1 Meg of RAM. I have a program that needs to read in data from a couple of <30K files and display it on the screen in a scrollable window. (I have the window stuff down). I am having a problem when I free up the space held by the two data file and the windows. And I have noticed that my program will completely hang if I try to free up one of the variables before the others. I have yet to get my program to work correctly. My question is does the order of mallocing and freeing data matter? Should whatever I malloc first be free'd last or the opposite? My program is showing inconsistency that seems to relate to the malloc'ing and free'ing I am doing. Here is how my program "works": /* the input will be straight ASCII text produced by another Turbo C program. I want to be able to read in the data for the two files and display it on the screen. */ # include #define LINEDELTA 100 #define STRLEN 80 typedef char element[STRLEN] /* will be a line of a data file, lines can be up to 80 characters long */ int fillarray (char *filename, element *array) { /* fill the variable array with text from the file in filename realloc memory using LINEDELTA to increase the size of the array if the size of the array is not big enough */ } main () { element *data1, *data2; data1 = (element *) malloc (LINEDELTA*sizeof(element *)); data2 = (element *) malloc (LINEDELTA*sizeof(element *)); /* One of the files is usually about 20 times the size of the other. If I read in the smaller one first, the data in the smaller one gets trashed when I read in the data to the second one. What can I do to avoid that. things are fine if I read in the data for the larger one first (I may not know which of the two is the larger at run time) */ lines1 = fillarray ("test1", data1); lines2 = fillarray ("test2", data2); /* set up two windows. Uses a structure to keep track of lots of things about a window ala Al Stevens "Memory Resident Utilities, Screen I/O and Programming Techniques" Windows structures are kept in a linked list Display the data */ /* program gets through to here with no gliches usually :-) */ /* BUT it only will go so far through these lines, inconsistently crashing on one of these lines */ free (data1); free (data2); delete_window (window1); /* free's up the space occupied by this window */ delete_window (window2); /* and deletes it from the linked list */ } Thanks for reading and I'll be waiting for responses -- Joe Duval duvalj@bionette.cgrb.orst.edu Losing your temper generally represents the incipient stage of rectal- cranial inversion.