However, both dimensions of a two-dimensional array at run time the array must be dynamically allocated grant. A two-dimensional array dynamically allocated should be considered in this case as an array of one-dimensional arrays. Some effort of memory, but the assignment must be an array of arrays, pointers, which are not necessary defined static two-dimensional arrays. Despite the effort, each element of the two-dimensional arrays with the bracket notation just like a statically defined two-dimensional array can be referenced.
These are the steps for a two-dimensional array dynamically reserve:
Declare a double dereferenced pointer of the desired type. Reserve the number of lines the size of a pointer dereference and assign to the first at the beginning of this assignment. Go through the lines and reserve the number of column size of the element.
Code:
Array of Arrays of Pointers element ------ ------------------------- ------- | | -> | | | | | | |. . . . . . . | | ------ ------------------------- ------- | | -> | | | | | | |. . . . . . . | | ------ ------------------------- ------- | | -> | | | | | | |. . . . . . . | | ------ ------------------------- ------- | | -> | | | | | | |. . . . . . . | | ------------------------- ------ -------..... ------------------------- ------- | | -> | | | | | | |. . . . . . . | | ------ ------------------------- ------- | | -> | | | | | | |. . . . . . . | | ------ ------------------------- -------
_fmalloc (use) for MS-DOS and 16-bit Windows, to use the wide or global heap. Note for Windows: _fmalloc () is available when compiling with Microsoft C / C + + compiler versions 7.0, 8.0 and 8.0 c are used. Do not use GlobalAlloc and GlobalLock, since this technology can use up to many selectors for relatively small allocations.
Unlike a static two-dimensional array declared the lines are not interrelated. As this is an array of arrays it is possible, however, an entire structure is greater than 64 KB without a big hand. If the number of rows or the size of the lines is greater than 64 KB, extensive hands are needed.
A small example :
/ * Semi-pseudo code for a two-dimensional array of char's *
Note that this sample uses the far heap and uses far pointers. *
For 32-bit Windows applications on Windows NT or Win32S or for * OS / 2,
use malloc ( ) and remove the _far keywords;. * / char *
_far _far unsigned
int * array,
unsigned int rows, columns,
/ * let's keep it within 64k * /
/ * Set the rows and columns to desired dimensions. * /
Rows = 8;
columns = 12;
array = (char * _far _far *)
_fmalloc (sizeof (char _far *) * rows)
if (array == NULL) (printf ("Not enough memory \ n");
return
for (i = 0; i <rows; i + +)
(array [i] = (char _far *) _fmalloc (sizeof (char) * columns)
if (array [i] == NULL) (printf (" not memory \ n enough "),
/ * handle error, _free (the previously allocated rows * /
return))
/ * to use the array: array [row] [column] * /
array [0] [1] = 'x'
/ * etc. * / To * Free the memory used by the array ** * /
for (i = 0; i <rows; i + +) _ffree (array [i]); _ffree (array);
Services :
Microsoft provides the services in the Knowledge Base articles and information as a service performance. Microsoft makes no warranty, please see the following articles and information in your application environment, the desired results. The decision on whether and how you use the article information is, Help, and more. With the exception of legal liability for Options Contact Microsoft in connection with your use of this product or information is excluded.
Bookmarks