The difference analysis of the dynamic array of [] static array and new assignment in C + + _c language

Source: Internet
Author: User

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

When the static array name is sizeof, the result is the size of the space occupied by the whole array ;
You can therefore get the length of the array using the sizeof (array name)/sizeof (the * array name).
int a[5]; Then sizeof (a) =20,sizeof (*a) =4. Because the entire array occupies 20 bytes, the first element (int) occupies 4 bytes.
int *a=new int[4]; then sizeof (a) =sizeof (*a) = 4, because the number of addresses is 4 bytes, and the int type also accounts for 4 bytes.

Second, the static array as a function parameter, in the function of the sizeof operation of the array name, the result is 4, because at this time the array name represents the pointer is an address , occupies 4 bytes of memory (because in passing the parameters of the array name, the compiler does not check the length of the group, Refer to the previous example of a C + + array of reference case 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, the efficiency is low, and [] directly on the stack allocation, will automatically release, high efficiency, but the stack space is limited .

The problem of returning an array by function

A static array of function declarations is not possible to return through a function, because of the lifetime problem, the memory occupied by the function calling its internal variable is freed. If you want to return an array through a function, you can create the array in the function dynamically with new and return to its first address.
The reason for this is understandable, because [] a static array is applied on the stack, the local variables in the function are also in the stack, and the new dynamic array is allocated in the heap, so after the function returns, the contents of the stack are automatically released, and the contents of the heap are not automatically freed if there is no delete .

Examples are as follows:

int *test (int *b)//b can be the array name of a static array, or it can be the first address of a dynamic array
{for
  (int i=0;i<5;i++)/output incoming array elements
   cout<<* (b+i) << "";
  cout<<endl;
  int *c=new int[5]; Dynamically create an array
  //If the green part is swapped to int c[5], then call test in the main function cannot get the values of the C array
  for (i=0;i<5;i++)  //new array equal to the passed-in array values plus 5
   * (c +i) =* (b+i) +5;
  return C;     Returns the first address of the newly created dynamic array,
int main ()
{
 int *b=new int[5];//Create dynamic array b for
 (int i=0;i<5;i++)//Assignment
  * (b+i) =i; 
 The green section can also be replaced with an int b[5]={0,1,2,3,4}, or it can be a static array
 int *c=test (b);   Takes B as a parameter, invokes the test function, and the return value is assigned to the  various
   cout<<* (c+i) << "" of the array returned by the C for (i=0;i<5;i++)//output test;
 cout<<endl;
 return 0;
}

I believe I have seen this article analysis can further deepen the reader's understanding of C + + arrays.

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.