Date: Tue, 30 Jun 1998 17:02:41 +0200 (MET DST) From: Federico Spinazzi To: Roberto cc: djgpp AT delorie DOT com Subject: Re: Allocation (malloc) In-Reply-To: <35983589.7165448@news.interbusiness.it> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk On Tue, 30 Jun 1998, Roberto wrote: > I have traslated my BorlandC code in djgpp code but i have some > problem. I allocate the memory how following : > int *vertice[10]; I want to take the occasion for point out a problem I have with 2-dimensional array. I don't think I'm answering to a C newbie, so I'm not pretending to give the right answer: I propose a workaround but with a subltle problem I can't understand (I found it using the mss library: mss10b.zip at any (?) djgpp/v2tk directory) I think here you are declaring pointer to a 2-dimensional array. Why not to use #include #include int main() { int **vertice, count, allocated; /* Maybe you know that you need a 10-element array. However: */ vertice = (int **)malloc(sizeof(int *); if (vertice == NULL) { cprintf("Not enough memory"); exit(0); } for (allocated = 1, count = 0; count<10; count++) { vertice[count] = (int *)malloc(HOW_MANY_I_NEED); if ((vertice[count] == NULL)) { int i; for (i=allocated; i; i--) free(vertice[i - 1]; free(vertice); /*^^ AARGH ! see below ... */ cprintf("Not enough memory"); exit(0); } allocated++; } /* the use vertice */ for(count = 10; count; count--) free(vertice[count - 1]); free(vertice); /*^^ Re: AARGH !*/ } You should be able to read and write in vertice[][]. BUT, why MSS complain thet I have corrupted memory calling free(vertice) ? Is it a correct waring o a bug in it? I've written to MSS writers but they are too busy ... I'm right or not ?. Why (beginning of the question) int **foo = NULL; can't be followed by (really followed, NO allocation for foo = (int ** )calloc(1, sizeof(int *));) for (i=0; i