Mail Archives: djgpp/1998/06/30/12:21:58
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 <conio.h>
#include <stdlib.h>
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<FOO; i++)
foo[i] = (int *)calloc(..);
without crashing and intead
int **foo;
can ?
I.E.: the first code SIGSEV, the second NOT. It is a particular case on my
code or it is a DJGPP feature (i.e.: due to calloc implementation) not
portable ? One of that case you say : it is a miracle if it works.
What is the point ?
(end of the question)
Thank you for help (present, past and future).
federico AT syspr03 DOT diat DOT unimi DOT it
- Raw text -