home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!utcsri!torn!watserv2.uwaterloo.ca!watmath!xjzhu
- From: xjzhu@math.uwaterloo.ca (Xiaojun Zhu)
- Subject: Summary: Dynamically allocate and de-allocate a 2-dim'l array.
- Message-ID: <Bxo164.1Ep@math.uwaterloo.ca>
- Keywords: new, delete, dynamic array
- Organization: University of Waterloo
- Date: Fri, 13 Nov 1992 17:42:04 GMT
- Lines: 38
-
- Object:
- 1. Giving thanks to all those who responded to my question
- 2. Trying to give the answer.
-
- Tip:
- When de-allocate an dynamically allocated array, use
- delete[] operator. (See <<C++ Programming Language>>,
- 2nd Edition, P.98)
-
- Code segment:
-
- 0. Declaration:
-
- typedef int T;
-
- int row, column;
- T **array;
-
- 1. Allocation:
- // note: row and column should have assigned values
- // at this stage
-
- array=new T*[row];
- for(int i=0; i<row; i++)
- {
- array[i]=new T[column];
- }
-
- 2. Deallocation:
- // note: several of you have pointed out to me that
- // the order is not important, anyway, I figured
- // to stick to the reverse order.
- for(i=row-1; i>=0; i++)
- {
- delete[] array[i];
- }
- delete[] array;
-
-