Differential analysis of dynamic arrays in C + + about [] static arrays and new allocations

Source: Internet
Author: User

This article mainly describes the C + + in the [] static array and new allocation of dynamic array of the difference analysis, very important concept, the need for friends can refer to the following

This paper analyzes the difference between the [] static array and the new assignment dynamic array in C + + language, which can help us to deepen the understanding of C + + language array. The specific differences are as follows:

First, the static array name of the sizeof operation, the result is the entire array of space-occupying size;
So you can get the length of the array with sizeof (array name)/sizeof (* array name).
int a[5]; Then sizeof (a) =20,sizeof (*a) =4. Because the entire array occupies 20 bytes, the first element (int) is 4 bytes.
int *a=new int[4]; sizeof (a) =sizeof (*a) = 4, because the number of address bits is 4 bytes, and the int type also accounts for 4 bytes.

When a static array is used as a function parameter, the sizeof operation of the array name is performed within the function, and the result is 4, because at this time the arrays name represents a pointer that is an address , which occupies 4 bytes of memory (because the compiler does not check the length of the array when it passes the parameters of the arrays name). Refer to the previous C + + array reference instance analysis). For the function name of the dynamic array, the result is 4 whenever the sizeof operation is performed.

Third, new also need you delete, is in the heap allocation space, inefficient, and [] directly on the stack allocation, will be automatically released, high efficiency, but the stack space is limited.

Iv. questions about returning an array through a function

A static array of function declarations cannot be returned by a function because of the lifetime problem, the memory occupied by the function's internal variables is freed when it is called. If you want to return an array from a function, you can dynamically create the array with new in the function and return to its first address.
This can be understood because the [] static array is applied in the stack, and the local variables in the function are in the stack, and the new dynamic array is allocated in the heap, so when the function returns, the contents of the stack are automatically freed, and the contents of the heap are not automatically freed without the delete.

Examples are as follows:

int*test (int*B)//B can be an array name for a static array, or it can be the first address of a dynamic array{   for(intI=0;i<5; i++)//outputs the elements of an incoming arraycout<<* (b+i) <<" "; cout<<Endl; int*c=New int[5];//Create an array dynamically//If the green part is replaced with an int c[5], the call to test in the main function fails to get the C array   for(i=0;i<5; i++)//The values of the new array are equal to the values of the incoming array plus 5* (c+i) =* (b+i) +5; returnC//returns the first address of the newly created dynamic array}intMain () {int*b=New int[5];//Create dynamic array b  for(intI=0;i<5; i++)//Assign Value* (b+i) =i; //The green part can also be changed to int b[5]={0,1,2,3,4}; that is, it can also be a static array int*c=test (b);//use B as a parameter, call the test function, assign the return value to C  for(i=0;i<5; i++)//outputs the items of the array returned by Testcout<<* (c+i) <<" "; cout<<Endl;return 0;}/*how to ask Hovertree.com*/

I believe that after reading the example analysis, we can further deepen the reader's understanding of C + + array.

Recommended:

Http://www.cnblogs.com/roucheng/p/cppjy.html

Differential analysis of dynamic arrays in C + + about [] static arrays and new allocations

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.