Memory Allocation and release of multi-dimensional arrays in C Language (malloc and free)

Source: Internet
Author: User

When writing code, you may encounter the issue of memory allocation and release of multi-dimensional arrays. errors may occur during the allocation and release processes. The following is a sample code for your reference.

 

To allocate space to a two-dimensional array (M * n), the code can be written as follows:

Char ** A, I;

// Allocate M pointer units first. Note that they are pointer units.

// The size of each unit is sizeof (char *)

A = (char **) malloc (M * sizeof (char *));

// Assign n character units,

// The above M pointer units point to the first address of the N character units

For (I = 0; I <m; I ++)

A [I] = (char *) malloc (N * sizeof (char ));

(Pay attention to the red part)

Release should be:

Int I;

For (I = 0; I <m; I ++)

Free (void *) A [I]);

Free (void *) );

If space is allocated for a three-dimensional array (M * n * P), it should be:

Char *** A, I, J;

A = (char ***) malloc (M * sizeof (char **));

For (I = 0; I <m; ++ I)

A [I] = (char **) malloc (N * sizeof (char *));

For (I = 0; I <m; ++ I)

For (j = 0; j <n; ++ J)

A [I] [J] = (char *) malloc (p * sizeof (char ));

The release code is an inverse process. The specific code is:

Int I, j ,;

For (I = 0; I <m; ++ I)

For (j = 0; j <n; ++ J)

Free (void *) A [I] [J]);

For (I = 0; I <m; ++ I)

Free (void *) A [I]);

Free (void *) );

 

The principles of allocation and release of multidimensional arrays above three dimensions are the same as those above.

(Transfer)

 

What is the second-Dimensional Fixed two-dimensional array allocation memory in C?

In the code, sometimes the memory needs to be allocated to a two-dimensional array. The first-dimensional length of the two-dimensional array is not fixed, the second dimension is fixed (like the array of ARR [N] [3 ). We can think of a double pointer instead of an array. Of course, we can also directly assign a value to n to define arr [N] [3] (supported by the c99 standard ), but here is another method.

Here, we take reading the point cloud data into a two-dimensional array as an example. because the number of points in the point cloud is N, we can determine that the point is a three-dimensional point. You can define and allocate the memory in the following way:

Double (* ARR) [3] = malloc (N * 3 * sizeof (double ));

But in the VC compiling environment, an error will be reported --Cannot be converted from "Void *" to "Double (*) [3]"In this case, type conversion should be performed before the malloc function. How should we convert it? How can we convert it to the double (*) [3] type? The conversion can be as follows:

Double (* ARR) [3] = (double (*) [3]) malloc (N * 3 * sizeof (double ));

Done! :).

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.