www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/30/15:15:26

From: sparhawk AT eunet DOT at (Gerhard Gruber)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Allocation (malloc)
Date: Tue, 30 Jun 1998 19:04:39 GMT
Organization: Customer of EUnet Austria
Lines: 67
Message-ID: <359f2e98.3655664@news.Austria.EU.net>
References: <Pine DOT LNX DOT 3 DOT 93 DOT 980630163156 DOT 18208A-100000 AT SysPr03 DOT disat DOT unimi DOT it>
NNTP-Posting-Host: e162.dynamic.vienna.at.eu.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Destination: Federico Spinazzi <federico AT SysPr03 DOT disat DOT unimi DOT it>
From: Gruber Gerhard
Group: comp.os.msdos.djgpp
Date: Tue, 30 Jun 1998 17:02:41 +0200 (MET DST):

>I think
>here you are declaring pointer to a 2-dimensional array. Why not to use

I suppose you never tested that code?

>#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 *);
) missing

>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);	

I suggest using malloc(HOW_MANY... * sizeof(int)) instead.

>You should be able to read and write in vertice[][].
> BUT, why MSS complain thet I have corrupted memory calling 

Because if you reference the memory this way you would need one single chunk
of memory that is big enough for the array.

assuming int m[10][10]; you need to allocate a block of 100 ints assigned to
m. Your above code doesn't do this because you'd allocate 10 pointers! to
ints. But that doesn't work either because the compiler needs to know how big
the array is in order to calculate the indizes when you access the array.

>free(vertice) ?
>
>Is it a correct waring o a bug in it?

It is correct.

>I've written to MSS writers but they are too busy ...

 :)))) What did you expect? :)

>I'm right or not ?.

You are right. they really are to busy. :) SCNR.

--
Bye,
   Gerhard

email: sparhawk AT eunet DOT at
       g DOT gruber AT sis DOT co DOT at

Spelling corrections are appreciated.

- Raw text -


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