Mail Archives: djgpp/1999/06/19/20:21:07
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 -