www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/05/17:15:52

To: djgpp AT delorie DOT com
Message-ID: <19970405.155744.10318.0.john.k.murphy@juno.com>
From: john DOT k DOT murphy AT juno DOT com (John K Murphy)
Date: Sat, 05 Apr 1997 17:03:09 EST

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++?

Sounds like you could use malloc() (C) or new() (C++) or....

I'm not sure if it's better or worse, but for me, this method is simpler:

get the array size, pass the size to a function that creates the array. 
EXAMPLE:

main()
{
   get int SCREEN_W, int SCREEN_H;
   do_something(SCREEN_W, SCREEN_H);
}

do_something(int SCREEN_W, int SCREEN_H)
{
   int Array[SCREEN_W][SCREEN_H];
   ...
   do stuff here
   ...
}

(don't try to compile this: it's pseudo-code :)

this is nice since then you can access your array like a normal array,
and it doesn't sound like the array needs to be dynamic, except at the
declaration.  You don't have to worry about the extra malloc stuff either

hope this helps...
.  ________ ________
|  \___  __\\       \
|      \ \   \  \ \  \
|    ___\ \   \  \_\  \
.    \_____\ohn\__\ \__\urphy

- Raw text -


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