www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/04/09:45:22

Message-Id: <m0wD4JG-000S1zC@natacha.inti.edu.ar>
Comments: Authenticated sender is <salvador AT natacha DOT inti DOT edu DOT ar>
From: "Salvador Eduardo Tropea (SET)" <salvador AT natacha DOT inti DOT edu DOT ar>
Organization: INTI
To: heymann AT ucsu DOT Colorado DOT edu, djgpp AT delorie DOT com
Date: Fri, 4 Apr 1997 11:40:33 +0000
MIME-Version: 1.0
Subject: Re: Dynamic Allocation of Multidimensional Arrays

William Heymann <heymann AT ucsu DOT Colorado DOT edu> wrote:
> So how do I make a dynamic two dimensional array in C++?

  What about using malloc and calculating the offset similar to how you calculate 
coordinates in the screen?
  For example if you need an array of 320x200 ints:

  int *array;

  array=(int *)malloc(320*200*sizeof(int));
  
  ..... suppose that X and Y are the index values ...
 
  array[X+Y*320]=n;

  Of course 320 and 200 can be variables, and ofcourse isn't too fast because 
the product can't be optimized if you use a variable.
  If you can determine the maximun "X" of the array perhaps you can allocate 
ever this width and only change the height, so the product is constant, for 
example maximun X is 1024, you'll have:

  array[X+Y*1024]

  Where 1024 is a constant and will be optimized as a shift.

SET
--------------- 0 --------------------------------
Salvador Eduardo Tropea (SET).
Address: Curapaligue 2124, Caseros, 3 de Febrero
Buenos Aires, (1678), ARGENTINA
TE: +(541) 759 0013

- Raw text -


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