Dynamic Allocation of two-dimensional array continuous memory, non-sequential memory implementation

Source: Internet
Author: User
Method 1: Memory discontinuous

If the data type of an array element is int, the general method for dynamically allocating two-dimensional arrays is as follows:

Int ** P = NULL;

P = (INT **) malloc (nwidth * sizeof (int *));

If (! P)

Return NULL;

For (Int J = 0; j <nwidth; j ++)

{

P [J] = (int *) malloc (nheight * sizeof (INT ))

If (! P [J])

Return NULL;

}

This code is easy to understand. First, 1st dimensions are allocated and 2nd dimensions are allocated cyclically. Assume that the two-dimensional array is 3 × 2, and the memory condition after each sentence is run (square indicates memory, XX indicates random number, and below the square is the memory address. Of course, the address is just a signal, which is not consistent with the actual situation)

Three memory units are allocated after the first sentence.

After cyclic allocation, note that the following three segments of memory are usually discontinuous. In this case, the following table P [N] [m] is used to operate the array. If the whole block of memory is used, the Operation will fail. For example:

The original intention is to clear the following three blocks of 6 memory units to 0, but it is counterproductive to clear the remaining six memory units from P to 0, and P [] cannot be used. P is followed by three allocated memory units, but six are to be operated. The other three are unknown regions. Clear the three unknown regions of the dotted lines behind them, which is dangerous and may cause program crash.

The allocated memory needs to be released cyclically. The Code is as follows:

For (Int J = 0; j <nwidth; j ++)

{

Free (P [J]);

P [J] = NULL;

}

Free (P );

P = NULL;

Method 2: Memory continuity

To dynamically allocate two-dimensional arrays with continuous memory, use the following method:

// P is a pointer to a pointer.

Int ** P = NULL;

// Request the system to allocate a space containing nwidth int * pointers. P points to the first address of the space.

// This space is used as the index of a two-dimensional array

P = (INT **) malloc (nwidth * sizeof (int *));

If (! P) // If malloc fails

Return NULL;

// The request system allocates a space containing nwidth * nheight int pointers. P [0] points to the first address of the space.

// This space is used to save the data of a two-dimensional array.

P [0] = (int *) malloc (nheight * nwidth * sizeof (INT ));

If (! P [0]) // If malloc fails

{

Free (P );

Return NULL;

}

// Correlation between indexes and corresponding data

For (INT I = 1; I <nwidth; I ++)

P [I] = P [I-1] + nheight; // note and think about why nheight is directly added here

Memset (P [0], 0, nheight * nwidth * sizeof (INT); // reset the two-dimensional array

This code solves the problem of discontinuous allocated space. Memory usage after each statement is run:

The first sentence is the same as the previous one.

These six memory units are allocated at a time, so they are continuous.

The first address of the Two-dimensional array is P [0], and P is the first address of the 2nd-dimensional Index. Therefore, if you want to perform the overall memory (buffer) operation on the two-dimensional array, you need to use P [0] as the first address of the operation object.

At this point, the index is associated with the corresponding data address. The two-dimensional array can be operated through the table P [] [] and the buffer. Functions that operate on the buffer zone, such as memcpy, writehuge of cfile, and readhuge, are easy to use, saving the trouble of two loops.

As for release, do not release them cyclically. Because malloc is used twice, you only need to free it twice:

Free (P [0]); // release a two-dimensional array

P [0] = NULL;

Free (p); // release the pointer Space

P = NULL;

References:

Http://hi.baidu.com/jiaon/item/52017c5a145debcfd2e10c52

Http://hi.baidu.com/_forward001/blog/item/8a7a5f867095c63966096e59.html

Http://topic.csdn.net/u/20081124/18/60e5259e-f1ae-4fe9-bda1-cd7b609250d8.html? Seed = 1045715561 & R = 60398843 # r_60398843

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.