www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/04/17:59:56

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Dynamic Allocation of Multidimensional Arrays
Date: Thu, 03 Apr 1997 20:18:02 -0800
Organization: Two pounds of chaos and a pinch of salt
Lines: 33
Message-ID: <334480FA.22D9@cs.com>
References: <33448D13 DOT 664 AT ucsu DOT Colorado DOT edu>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp222.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

William Heymann wrote:
> 
> I was wondering how do I make an multi-dimensional array that itilalizes
> at run time without knowing any of the parameters. What I am doing is I
> am using an array to keep track of objects on the screen so that my
> colision detection works. I am also using Allegro and would like to add
> in the function that allows the user to select what resolution to run
> the screen in. Since the array is screen resolution dependent I need a
> way to create the array at runtime using the the values I get from
> Allegro.
> 
> So how do I make a dynamic two dimensional array in C++?

In C, you can just dynamically allocate a linear block of memory and
index it just like you would the screen memory.  For example:

unsigned char *buffer;

buffer = calloc( sizeof(char) * SCREEN_WIDTH * SCREEN_HEIGHT );
...
buffer + x + SCREEN_WIDTH * y = pixel;
  -or-
buffer[x + SCREEN_WIDTH * y] = pixel;

When you're done with it, be sure to free the buffer.  I can't imagine
that you'd do things much differently iin C++.

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I  |        fighteer AT cs DOT com         |
| Plan:  To find ANYONE willing to |   http://www.cs.com/fighteer   |
| play Descent 2 on DWANGO!        | Tagline: <this space for rent> |
---------------------------------------------------------------------

- Raw text -


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