www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/30/12:21:58

Date: Tue, 30 Jun 1998 17:02:41 +0200 (MET DST)
From: Federico Spinazzi <federico AT SysPr03 DOT disat DOT unimi DOT it>
To: Roberto <dante AT ctonline DOT it>
cc: djgpp AT delorie DOT com
Subject: Re: Allocation (malloc)
In-Reply-To: <35983589.7165448@news.interbusiness.it>
Message-ID: <Pine.LNX.3.93.980630163156.18208A-100000@SysPr03.disat.unimi.it>
MIME-Version: 1.0


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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019