简体   繁体   中英

What is the purpose of sized array as function argument is c and c++?

Consider the following functions:

void func1(int unsized_array[]){}
void func2(int sized_array[10]){}
void func3(int *pointer){}

According to the result of:

    std::cout << std::is_same<decltype(func1), decltype(func2)>::value << std::endl;
    std::cout << std::is_same<decltype(func2), decltype(func3)>::value << std::endl;
    std::cout << std::is_same<decltype(func3), decltype(func1)>::value << std::endl;

Types of these 3 functions is the same. Also inside the function func2 , sizeof operator does not provide the size of array elements all combined.

So what is the purpose of sized array as function argument (like func2 )?

  1. Since array declarators generally may have size expressions (as when defining an array other than in a parameter or when declaring a parameter that contains an array as a sub-part, such as a pointer to an array), it would take more work to exclude it from the grammar than to leave it in harmlessly. (There are numerous constructions in C that can result in no effect, such as a statement expression with no side effects ( 3*4; ), the left operand of a comma expression with no side effects, a function or loop with an empty body, and so on. It would take a lot of work to exclude things that have no effect from the language.)

  2. Array sizes in parameters do alter the meaning when the static keyword is present; void func2(int asd[static 10]) declares a function that must be passed a pointer to the first of at least ten elements, which is different from the meaning of the other declarations. (And this would compound the problem of excluding a size expression from the grammar of an array parameter declaration, as it would be necessary to ban a bare size but not one with static .)

  3. The array size may be useful documentation to the function implementor and to the function user.

  4. The array size is evaluated. If, for example, the definition is void func2(int asd[printf("Hello")]) {} , then “Hello” will be printed when the function is called. (Some compilers might not do this; the C standard is unclear on it.)

  5. A compiler could use the size expression to emit warnings if it sees the function uses more than the stated number of elements or a caller passes fewer than the stated number. (Clang 11 appears not to do the former and not to do the latter unless static is used.)

There is no purpose as they all decay to the same type, as you correctly identified. For C++ this behavior is inherited from C.

Just like how adding const to a non-reference argument and other keywords won't change anything to the outside, they will all result to the void(int) signature.

void foo1(int a) {}
void foo2(const int a) {}
void foo3(volatile int a) {}

But instead of int[10] you can have something that actually works:

void func2(int(&asd)[10]) {}

This will only accept int arrays with size 10. You will now also be able use sizeof to get the array size at compile time, which was not possible for int[10] obviously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM