delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/04/22:14:12

From: "Christopher Nelson" <paradox AT gye DOT satnet DOT net>
To: <djgpp AT delorie DOT com>
Subject: Re: What's the best way to make a 2d map
Date: Tue, 4 May 1999 11:57:55 -0600
Message-ID: <01be9657$a33f2c20$LocalHost@thendren>
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 4.71.1712.3
X-MimeOLE: Produced By Microsoft MimeOLE V4.71.1712.3
Reply-To: djgpp AT delorie DOT com


>Hi, I need to set-up a map like :
>
>int sx=100,sy=100;
>char map[sx][sx];
>
>I know I can use :
>
>char (*map)[sx]=new[sx][sy];
>
>But i want to change this array size anytime in the game. So I must
>delete []map;
>then remake the array.


or, you could allocate the new map, copy whatever old information you want
into the new map, and then delete the old map.

>Do it be better to do this using malloc and realloc ?
>how can I set up a 2 and a 3 dimentional array using malloc ? and how I
>resize it with realloc ?

new just calls malloc.  the compiler takes all your "dimensions", multiplies
them together, and then multiplies them by the size of the variable which
you are creating.

e.g.

unsigned char *new_map = new unsigned char[x][y];

is the same as

unsigned char *new_map = new unsigned char[x*y];

or

unsigned char *new_map = (unsigned char *)malloc(x*y);

..

an access is always the same.

map[x][y] = map[x+(y*row_size)];

it's just that when you allocate it, the compiler knows what the row_size
is, so the mul is transparent to you.

    -={C}=-

- Raw text -


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