delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/06/19/20:21:07

Message-ID: <MAPI.Id.0016.00333138303633303030303930303039@MAPI.to.RFC822>
X-MSMail-Priority: Normal
X-Priority: 3
To: djgpp AT delorie DOT com
Cc: erik2 DOT berglund AT telia DOT com
MIME-Version: 1.0
From: "Erik Berglund" <erik2 DOT berglund AT telia DOT com>
Subject: Re: Dinamic allocation
Date: Sun, 20 Jun 99 02:19:21 +0100 (DJG)
Reply-To: djgpp AT delorie DOT com

Gustavo Niemeyer wrote:

> 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.

The allocation seems right, but I think the declaration should be 
"char *spans", and the data access later down below in your
program code should look something like:

data = spans[MAXWIDTH * y + x];

In this way, the compiler thinks spans is a simple char vector,
but that's ok, the multiplication will make it work anyway.

Hope this helps,
Erik


- Raw text -


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