home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18724 < prev    next >
Encoding:
Text File  |  1993-01-05  |  2.6 KB  |  62 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!cs.utexas.edu!hermes.chpc.utexas.edu!news.utdallas.edu!corpgate!crchh327!crchh447!brunner
  3. From: brunner@crchh447.bnr.ca (James Brunner)
  4. Subject: Re: Dynamic Multidimensional Array Allocation in C++
  5. Sender: news@news.rich.bnr.ca (news server)
  6. Message-ID: <C0EE0J.Iv@news.rich.bnr.ca>
  7. Date: Tue, 5 Jan 1993 20:24:19 GMT
  8. Reply-To: brunner@crchh447.bnr.ca (James Brunner)
  9. References: <C0C68I.MA7@dcs.ed.ac.uk> <C0Cw7r.72t@dcs.ed.ac.uk>
  10. Nntp-Posting-Host: crchh447
  11. Organization: BNR, Inc.
  12. Lines: 48
  13.  
  14. In article <C0Cw7r.72t@dcs.ed.ac.uk>, tk@dcs.ed.ac.uk (Tommy Kelly) writes:
  15. |> In article <C0C68I.MA7@dcs.ed.ac.uk> tk@dcs.ed.ac.uk (Tommy Kelly) writes:
  16. |> >How do I implement a dynamically configurable two dimensional array in C++?
  17. |> >
  18. |> >For one dimension its easy, but doing:
  19. |> >
  20. |> >my_type *my_type_var = new my_type[a][b];
  21. |> 
  22. |> What I'm doing at the moment is explicitly creating a vector of vectors like:
  23. |> 
  24. |>     My_type **my_type_var = new My_type*[a];
  25. |>     for(int i=0; i<b; i++) {
  26. |>           my_type_var[i] = new My_type[b];
  27. |>     }
  28. |> 
  29. |> I can then access the array my_type_var[a][b] as normal.
  30. |> But it seems kinda inelegant.  Maybe I expect too much from C++...
  31.  
  32. Actually, your method is very elegant.  The problem is that array access is
  33. greatly a function of the compiler.  To access element [2][3] in a [][7] array,
  34. the compiler creates code that does this:
  35.  
  36.   element address = (start of array) + ((2 * 7) + 3) * (element size))
  37.  
  38. or more generally, to access element [x][y]:
  39.  
  40.   element address = (start of array) + ((x * 7) + y) * (element size))
  41.  
  42. Notice that the size of the lower dimensions are compiled in (the element size(
  43. is as well).
  44.  
  45. By creating an array of pointers to one dimensional arrays, you can access it
  46. (syntax wise) like a two dimensional array, but the compiler generates this:
  47.  
  48.   pointer address = (start of array) + x * (pointer size)
  49.   element address = (dereferenced pointer address) + y * (element size)
  50.  
  51. There is no need for the compiler to know any of the dimensions in advance.
  52.  
  53. Of course other benefits depend on how the 2 dimensional array is to be used.
  54. If for example you were mapping a terminal screenful of characters, with your
  55. method, a scroll operation would only involve shifting the pointers and clearing
  56. the last line.  Must faster than moving all of the elements.
  57. -- 
  58. ---------------------------------------------------------------------------
  59. Jim Brunner - (brunner@bnr.ca)
  60. All opinions are my own and have nothing whatsoever to do with BNR, NT,
  61. NTI, Bell Canada, or any of the BCE corporations or affiliates.
  62.