home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16248 < prev    next >
Encoding:
Text File  |  1992-11-14  |  1.3 KB  |  49 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!utcsri!torn!watserv2.uwaterloo.ca!watmath!xjzhu
  3. From: xjzhu@math.uwaterloo.ca (Xiaojun Zhu)
  4. Subject: Summary: Dynamically allocate and de-allocate a 2-dim'l array.
  5. Message-ID: <Bxo164.1Ep@math.uwaterloo.ca>
  6. Keywords: new, delete, dynamic array
  7. Organization: University of Waterloo
  8. Date: Fri, 13 Nov 1992 17:42:04 GMT
  9. Lines: 38
  10.  
  11. Object: 
  12.          1. Giving thanks to all those who responded to my question
  13.          2. Trying to give the answer.
  14.  
  15. Tip:
  16.         When de-allocate an dynamically allocated array, use
  17.         delete[] operator. (See <<C++ Programming Language>>,
  18.         2nd Edition, P.98)
  19.  
  20. Code segment:
  21.  
  22.    0. Declaration:
  23.  
  24.       typedef int T;  
  25.  
  26.       int row, column;
  27.       T **array;
  28.  
  29.    1. Allocation: 
  30.                   // note: row and column should have assigned values
  31.                   // at this stage
  32.  
  33.       array=new T*[row];
  34.       for(int i=0; i<row; i++)
  35.       {
  36.          array[i]=new T[column];
  37.       }
  38.        
  39.    2. Deallocation:
  40.                    // note: several of you have pointed out to me that
  41.                    // the order is not important, anyway, I figured
  42.                    // to stick to the reverse order.
  43.       for(i=row-1; i>=0; i++) 
  44.       {
  45.           delete[] array[i];
  46.       }
  47.       delete[] array;
  48.  
  49.