rg2c+@andrew.cmu.edu (Robert Nelson Gasch) (12/03/90)
Hi, I'm using ThinkC 3.0 and am experiencing some very strange behavior with some pointers that I'm using. I'm using a datatype genearray defined as follows: typedef struct{ short genes [MAXGENE]; int num; int ID; short int c_rate; short int m_rate; int maxage; }genetype, *geneptr; typedef geneptr genearray [MAXSPECIES]; the function createnewgenes is supposed to allocate 1 pointer in genearray: geneptr createnewgenes (thisgeneptr) geneptr thisgeneptr; { int x; /* allocate new pointer to genes */ thisgeneptr = (geneptr) malloc (sizeof (genetype)); for (x=0; x<MAXGENE; x++) thisgeneptr->genes[x] = 0; /* initialize genes to zero (blank) */ return (thisgeneptr); } for the following function, the global variables are set up as follows: extern landscapetype landscape; extern genearray genes; extern int numorgs, numorgtypes, maxage, maxageID; void do_reproduce (x, y) int x, y; { geneptr temporg; int ID = landscape[x][y].ID[0], found_ID = EMPTY; numorgs += 1; if (rand()%genes[ID]->m_rate == 0) /* mutate */ { numorgtypes += 1; temporg = createnewgenes (temporg); do_mutate (temporg, ID); more_stuff_is_done } } Now here's the problem that I discovered using the debugger: The parameters are passed in correctly, the variables declared properly so everything seems to be going OK. When I call temporg = createnewgenes (temporg); temporg is initialized properly, but so is genearray[0] (which has been allocated previously by a call to createnewgenes). I have no idea why allocating temporg interferes with the state of genearray[0] (they are 2 seperate entities after all). Any ideas?? Thanx alot --> Rob