delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/06/20/05:02:43

From: "Red Angel" <red DOT angel AT flashnet DOT it>
Newsgroups: comp.os.msdos.djgpp
Subject: R: Dinamic allocation
Date: Sun, 20 Jun 1999 00:25:07 +0200
Organization: Customer of Flashnet S.p.A. - http://www.flashnet.it
Lines: 92
Message-ID: <7kh5jo$9cq$1@news.flashnet.it>
References: <LOBBKLEPLBKLOKFELHOIAELGCCAA DOT djgpp AT niemeyer DOT net>
NNTP-Posting-Host: pppn-22.to2.flashnet.it
X-Trace: news.flashnet.it 929831352 9626 195.191.110.26 (19 Jun 1999 22:29:12 GMT)
X-Complaints-To: abuse AT flashnet DOT it
NNTP-Posting-Date: 19 Jun 1999 22:29:12 GMT
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2014.211
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Try this:
char **spans;

spans = (char **) malloc(MAXHEIGHT*sizeof(char*));
for(i=0; i<MAXHEIGHT; i++)
      spans[i] = (char *) malloc(MAXWIDTH*sizeof(char));


or in another way:

char *spans;
spans = (char *) malloc(MAXHEIGHT*MAXWIDTH*sizeof(char));
(in this way you have to know that you have a one dimensional array so if
you want to do a thing like :
spans[y][x]=1 you can't in this way but you have to do this :
spans[(y*x)+x]=1)

Red Angel




Gustavo Niemeyer <djgpp AT niemeyer DOT net> wrote in message
LOBBKLEPLBKLOKFELHOIAELGCCAA DOT djgpp AT niemeyer DOT net...
> Thank you for your answer Antonio!
>
> I had to change some thing in your code to
> compile it. Code now looks like this:
>
>   char **spans;
>
>   spans = (char **) malloc(MAXHEIGHT);
>
>   for(i=0; i<MAXHEIGHT; i++)
>       spans[i] = (char *) malloc(MAXWIDTH);
>
>
> But it isn't working anyway! I tried to delete the
> second line but it didn't work again...
>
> By the way, I can't understand why the old code isn't
> working...
>
>
> Thank you!
>
>
> Gustavo Niemeyer
>
>
> -----Original Message-----
> From: Antonius Steinkamp [mailto:Antonius DOT Steinkamp AT t-online DOT de]
> Sent: Sexta-feira, 18 de Junho de 1999 22:34
> To: djgpp AT delorie DOT com
> Subject: Re: Dinamic allocation
>
>
> try to allocate this way
> spans = (char*)malloc(MAXHEIGHT)
> for ( int i=0; i<MAXHEIHT; i++) {
> spans[i] = (char)malloc(MAXWIDTH);
> }
> this is not tested but should work (aka indexes)
>
> Gustavo Niemeyer schrieb:
> >
> > Hi there!
> >
> > I think I'm doing some thing wrong here.
> >
> > When I try to hard code an array like this:
> >
> > char spans[MAXHEIGHT][MAXWIDTH];
> >
> > So the program runs ok. But when I try to
> > use dinamic memory allocation like this:
> >
> > char **spans;
> > spans = (char **) malloc(MAXWIDTH*MAXHEIGHT*sizeof(char));
> >
> > When I run the program it prints a General
> > protection fault error, pointing to a loop
> > that accesses the array.
> >
> > Probably is something I'm missing... isn't it?
> >
> > Thank you
> >
> > Gustavo Niemeyer
>


- Raw text -


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