malloc, free, new, delete

Source: Internet
Author: User

The processing of an indefinite-size multidimensional array in C language:

If you want to allocate space for a two-dimensional array (m*n), the code can be written as follows:

Char **a, I;

Allocate m pointer units First, note that the pointer unit

So the size of each unit is sizeof (char *)

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

and allocate n character cells,

The M-pointer unit above points to this N-character single head address

for (i = 0; i < m; i++)

A[i] = (char *) malloc (n * sizeof (char));

The release should be:

int i;

for (i=0;i<m;i++)

Free (void *) a[i]);

Free (void *) a);

If you allocate space 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));

Release the code as a reverse process, with the following code:

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 *) a);

The allocation and release of multidimensional arrays of more than three dimensions is the same as above.

How to allocate memory for two-dimensional arrays with fixed second-dimension lengths in C

In the code that is written, it is sometimes necessary to allocate memory for a two-dimensional array, the first dimension of the two-dimensional array is indefinite, and the second dimension is a fixed (like arr[n][3] array). What we can think of is the use of double pointers instead of arrays, of course, you can directly define ARR[N][3] (C99 standard support), but here's another way to say it.

To read the point cloud data into a two-dimensional array, for example, because the point cloud points n variable, it can be determined that the point is a three-dimensional point, you can define and allocate memory in the following ways:

Double (*arr) [3] = malloc (n*3*sizeof (double));

However, in the VC compilation environment, will be error-can not be converted from "void *" to "double (*) [3]" , this time should be in the malloc function before the type conversion, how should be converted? How to convert to a double (*) [3] type, you can convert as follows:

Double (*arr) [3] = (double ((*) [3]) malloc (n*3*sizeof (double));

Get! :)。

Third, C + + 's new delete

1. Common types (structure, class, etc.):

Int* A;

a=new int;

*a=3;

Delete A;

2. Arrays:

Int* A;

A=new Int[num];

delete []a;

malloc, free, new, delete

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.