C + + Array Reference instance analysis _c language

Source: Internet
Author: User

In C + +, the so-called array reference, that is, the reference to the array;
Such as:

int  a[10];  
int  (&B) [ten] = A;

If you write:

int  a[10];
int* &b = A;

The system will have an error: cannot convert from ' int ' to ' int *& '.
Perhaps you would say that the array name is not a pointer to this array? The title of A is int* type, B is to point to int* of the reference, should be right, ah, why will the error? This is because the compiler checks the reference to the array more strictly, you need to examine the dimensions of the array, where a is understood to be a pointer int [10] that points to an array of 10 int, and a reference type int (&) [10] for the reference, that is, a reference to a pointer to a 10 int array.

There is an "array price reduction" problem in C and C + +. As shown below:

#include <iostream>
void Test (char arr[100])
   std::cout << sizeof (arr) << Std::endl ; Output 4
int main ()
  char arr[100] = {0};
  Std::cout << sizeof (arr) << Std::endl; Output
  test (arr);

  return 0;

The output of this code is:

For the same arr, one output is 100 and the other output is 4. The arr in void Test (char arr[100]) was reduced.
The arr in void Test (char arr[100]) is reduced by the order
void Test (char arr[100]) is equivalent to void Test (char arr[]) and is equivalent to void Test (char* const ARR) if you intended it to be even equivalent to void test (char arr[10 ] )

The compiler does not check the number of dimensions of the array. Other words:

void Test (char arr[100])
   std::cout << sizeof (arr) << Std::endl;

be lowered into

void Test (char* const arr)
   std::cout << sizeof (arr) << Std::endl;//Since char*, of course output 4

This way, the size of the array is not checked, and the program that needs to ensure the size of the array can cause problems. How to solve this problem? You can use a reference to an array of arrays in C + +.

Look at the following code:

void Test (const char (&arr) [m])
   std::cout << sizeof (arr) << Std::endl;/Output
  Char arr[100] = {0};
  Std::cout << sizeof (arr) << Std::endl; Output
  test (arr);

This allows the test to accept an array of 100 chars and can only accept a char array of size 100.


Char arr[20] = {0};
Test (arr);

You'll get an error.

In C + +, references to arrays can pass array names directly, because the size of the array information is provided in the formal parameter. But then we can only fix the size of the array to use this function. A reference to the template Addends group solves this problem by looking at the following code:

Template <int sz>
void Test (char (&arr) [sz])
  (int i = 0; i < sz; i++)
char a[2] = {0}, b[15] = {0};
Test (a);  OK
Test (b); Ok......

This solves the problem of variable length of the array, but introduces a new problem:

(1) Multiple test codes are generated when there are multiple different test calls. The traditional function call is only one generation, and also the number of calls is irrelevant.
(2) because the code is generated at compile time, it needs to know the size of the reference array. So it's obvious that a function like this can't be used as a parameter to a function, so you can't use this function to handle dynamically allocated memory regions, which are determined at run time.

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.