# [Go] Array Reference: C + + array to do the parameter in-depth analysis

Source: Internet
Author: User

"Array Reference" to avoid "array reduction" (this article was posted in the vckbase\c++ forum)

Inspired by [Hpho] A section of the template function, close-up this article, if there is similar, it is regrettable.
Array reduction is a nuisance, which is an unresolved problem in C language, first look at a piece of code to understand what "array reduction"

#include <IOSTREAM>
using namespace Std;

void Test (char array[20])
{
cout << sizeof (array) << Endl; Output 4
}

int main (void)
{
Char array[20] = {0};
cout << sizeof (array) << Endl; Output 20
Test (array);
}

Why is the same declared array one output 201 output 4? This is because the array in void Test (char array[20]) is reduced to order,void Test (char array[20]) is equivalent to void Test (char array[]) and is equivalent to void Test (CH ar* const Array), if you are Bt (joking), it is also equivalent to void Test (char array[999]).
So
void Test (char array[20])
{
cout << sizeof (array) << Endl;
}
be lowered into
void Test (char* const array)
{
cout << sizeof (array) << Endl; Since it's char*, of course output 4
}
The problem is that you can define an array of less than 20 elements and pass it to test and wait for the program to crash . In some of the more demanding occasions can not use the array to do parameters, really TMD heart is unwilling.

So how to solve this problem in C language?
There is no way, should say there is no good way. A: Make a structure, where only one char array[20], and then use this structure pointer instead of Char ARRAY[20]. This is a very cumbersome approach, and not intuitive; B: Use _msize inside test to calculate the array length. This is not the case, first it makes the wrong discovery deferred to the runtime, not the compile period, followed by _msize length/element size >=array length, i.e. new char[19] and new ARRAY[20] The allocated size is the same, so that It does not cause the program to crash, but the result of the operation is incorrect.

Thanks to [Hpho], inspired, C + + has a "reference" that C does not have, but how to declare the array reference? After several tests, look

#include <IOSTREAM>
using namespace Std;

void Test (char (&array) [20])//is not much like the difference between Char *p[20] and char (*P) [20]?
{
cout << sizeof (array) << Endl;
}

int main (void)
{
Char array[20] = {0};
cout << sizeof (array) << Endl;
Test (array);
}

In C + +, an array is never passed by value, it is a pointer to the first element, or No. 0, to be exact.

For example, the following declaration:
void Putvalues (int[10]);
is considered by the compiler
void Putvalues (int*);
The length of the array is independent of the parameter declaration, so the following three declarations are equivalent:
Three equivalent putvalues () declarations
void Putvalues (int*);
void Putvalues (int[]);
void Putvalues (int[10]);

Because the array is passed as a pointer, this has two implications for programmers:

1. changes to the parameter array within the modulated function will be applied to the array arguments instead of the local copy, and when the array being used as an argument must remain unchanged, the programmer needs to preserve the copy function of the original array by declaring the parameter type as const to indicate that it does not want to change the array element.
void Putvalues (const int[10]);

2. the length of the array is not part of the parameter type, and the function does not know the actual length of the array passed to it, and the codec does not know that the length of the array is not checked when the compiler makes parameter type checks on the actual parameter type. For example:
void Putvalues (int[10]); As int*
int main () {
int I, j[2];
Putvalues (&i); OK: &i is int*; Potential run-through errors
Putvalues (j); Ok:j is converted to a pointer to the NO. 0 element
The argument type is int*: A potential run error
return 0;
}

The type checking of a parameter only guarantees that the two invocations of Putvalues () provide an argument of type int*, and the type check cannot verify that the argument is an array of 10 elements. Traditionally, a C-style string is an array of characters, and it ends with an empty character encoding. But all other types, including the character array that you want to handle with empty characters, must somehow know its length when passing an argument to a function.

A common mechanism is to provide an additional parameter that contains the length of the array. For example:
void Putvalues (int[], int size);
int main () {
int I, j[2];
Putvalues (&i, 1);
Putvalues (J, 2);
return 0;
}

Another mechanism is to declare a parameter as a reference to an array

When a parameter is a reference to an array type, the length of the array becomes part of the parameter and the argument type, and the compiler checks to see if the length of the array argument matches the length specified in the function parameter type.
An array with a parameter of 10 int
parameter is a reference to an array of ten INTs
void putvalues (int (&arr) [10])//cannot be written as &arr[10] because the subscript operator has higher precedence
int main () {
int I, j[2];
Putvalues (i); Error: argument is not an array of 10 int
Putvalues (j); Error: argument is not an array of 10 int
return 0;
}

[Go] Array Reference: C + + array to do the parameter in-depth analysis

Related Keywords:
Related Article

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.