Results 1 to 2 of 2

Thread: Dynamic memory allocation for two-dimensional arrays

  1. #1
    Join Date
    Jan 2010
    Posts
    119

    Dynamic memory allocation for two-dimensional arrays

    Summary :
    The C language does not support internal dynamic memory allocation for two-dimensional arrays. Create a structure requires some programming set up, once created, the elements on familiar double bracket ([] []) notation may be referenced. Memory requirements when building the structure. This technology is platform independent work in the operating systems MS-DOS, Windows, Windows NT and OS / 2


    Description :
    So that the compiler generates code for two-dimensional array element must referenced the number of columns of the array at compile time must be known. Therefore, it is possible to reserve a two-dimensional array dynamically when pointer declaration "Width" contains the array. Below is a code fragment that illustrates this:
    Code:
    char (* array) [columns]; array = (char (*) [columns]) malloc (sizeof (char) * rows * columns) if (array == NULL) (printf ("Not enough memory n! \" ) return;)

  2. #2
    Join Date
    Jan 2010
    Posts
    119

    Re: Dynamic memory allocation for two-dimensional arrays

    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.

Similar Threads

  1. How can I use parallel_for to pass two dimensional arrays
    By Tamonash in forum Software Development
    Replies: 7
    Last Post: 08-10-2010, 12:04 AM
  2. Dynamic allocation of an array structure
    By Samarth in forum Software Development
    Replies: 3
    Last Post: 26-12-2009, 12:49 PM
  3. Two Dimensional Arrays in JavaScript
    By Bettina in forum Software Development
    Replies: 5
    Last Post: 15-12-2009, 02:01 PM
  4. Sorting Two Dimensional Arrays
    By Diwakar_12 in forum Software Development
    Replies: 2
    Last Post: 20-03-2009, 01:53 PM
  5. SBSMonitoring/SQL Memory allocation error (run away Memory usage)
    By Dilbert in forum Small Business Server
    Replies: 1
    Last Post: 21-09-2005, 11:32 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,024,981.74176 seconds with 17 queries