简体   繁体   English

编译器从 function 参数的数组中获取多少元素?

[英]How many elements does the compiler take from an array which is function argument?

I have heard that when using an array as a function argument, the compiler takes it as not actually an array but as a pointer to the 1st element of the array.我听说当使用数组作为 function 参数时,编译器将其视为实际上不是数组,而是作为指向数组第一个元素的指针。 So, when asking for size of A(int array) it should return size of pointer 4 (in bytes).因此,当询问 A(int array) 的大小时,它应该返回指针 4 的大小(以字节为单位)。 But I am getting 8. What is the problem?但我得到8。有什么问题?

I am using CodeBlocks.我正在使用代码块。

int SumOfElemets(int* A, int size){//       int* A  or  int A[]   ... it is the same!

    int i, sum = 0;
    printf("SOE - Size of A = %d, size of A[0] = %d\n", sizeof(A), sizeof(A[0]));

//here it should be 4 and 4 but i am getting 8 and 4

    for (i=0;i<size; i++){

        sum += A[i];    //A[i] is *(A+i)
    }
    return sum;
}


int main(){

    int A[] = {1,2,3,4,5};
    int size = sizeof(A)/sizeof(A[0]);    //size of the array
    int total = SumOfElemets(A, size);
    printf("Sum of elements = %d\n", total);
    printf("Main - Size of A = %d, size of A[0] = %d\n", sizeof(A), sizeof(A[0]));


    return 0;
}

Hi I have heard that when using an array as a function argument compiler takes it as not actually an array but as a pointer to the 1st element of the array.嗨,我听说当使用数组作为 function 参数编译器时,它实际上不是数组,而是指向数组第一个元素的指针。

Firstly function parameters having array types are adjusted by the compiler to pointers to the array element types.首先,编译器将具有数组类型的 function 参数调整为指向数组元素类型的指针。

For example these function declarations例如这些 function 声明

int SumOfElemets(int* A, int size);
int SumOfElemets(int A[], int size);
int SumOfElemets(int A[1], int size);
int SumOfElemets(int A[100], int size);

are equivalent and declare the same one function.是等效的并声明相同的 function。

On the other hand, arrays used in expressions as for example argument expressions with rare exceptions are implicitly converted to pointers to their first elements.另一方面,在表达式中使用的 arrays (例如具有罕见异常的参数表达式)被隐式转换为指向其第一个元素的指针。

So these function calls所以这些 function 调用

SumOfElemets(A, size);
SumOfElemets(&A[0], size);
SumOfElemets(A + 0, size);

are equivalent.是等价的。

The size of pointer is implementation defined as by the way the size of an object of the type int though usually sizeof( int ) or in the case of your code sizeof( A[0] ) is equal to 4.指针的大小被实现定义为int类型的 object 的大小,尽管通常sizeof( int )或在您的代码的情况下sizeof( A[0] )等于 4。

Pay attention to that expression with the sizeof operator has the type size_t .请注意带有sizeof运算符的表达式的类型为size_t So you need to use the conversion specifier %zu to output the value of the expression instead of %d所以你需要使用转换说明符%zu到 output 表达式的值而不是%d

printf("SOE - Size of A = %zu, size of A[0] = %zu\n", sizeof(A), sizeof(A[0])); 

The sizeof operator yields the size (in bytes) of its operand. sizeof运算符产生其操作数的大小(以字节为单位)。

When you call sizeof(A) , that means "what is the size of the pointer A ?".当您调用sizeof(A)时,这意味着“指针A的大小是多少?”。
That is different from sizeof(A[0]) , which means "what's the size of the first element of the array A ?".这与sizeof(A[0])不同,这意味着“数组A第一个元素的大小是多少?”。

The size of types is compiler-dependent , and in C implementations with a 64-bit address space, pointers are typically 64 bits (eight 8-bit bytes) and int are often 32 bits, therefore:类型的大小取决于编译器,并且在具有 64 位地址空间的 C 实现中, pointers通常是 64 位(八个 8 位字节),而int通常是 32 位,因此:

  • 8 bytes for sizeof(A) ; sizeof(A) 8 个字节;
  • 4 bytes for sizeof(A[0]) . sizeof(A[0]) 4 个字节。

(Clarified byEric Postpischil ) (由Eric Postpischil澄清)


Check out the following examples:查看以下示例:
Example with a x64 ( 64bit address space) compiler: https://godbolt.org/z/vYcWhvxsE使用x6464 位地址空间)编译器的示例:https://godbolt.org/z/vYcWhvxsE
Output: Output:

Size of pointer to int (x64): 8
Size of integer (x64): 4

Example with a x86 ( 32bit address space) compiler: https://godbolt.org/z/68PTsM6db使用x8632 位地址空间)编译器的示例:https://godbolt.org/z/68PTsM6db
Output: Output:

Size of pointer to int (x86): 4
Size of integer (x86): 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编译器如何在恒定时间内从对象数组中获取元素? - How does compiler fetch elements from array of objects in constant time? 在所有元素都来自{1,2,…,n}集的数组中,哪个数组的反转次数最多? 它有多少个反转? - Among all arrays with elements from the set {1, 2, …, n}, which array has the most inversions? How many inversions does it have? 如何更新具有嵌入文档的 mongoose 数组中的许多元素 - How to Update many elements in mongoose array which has embedded documents 如何定义一个 function,其参数从 main() function 获取变量? - How to define a function, parameters of which take variables from main() function? 如何将数组元素作为输入 - How to take array elements as input 如何从数组中删除哪些元素是另一个数组的元素? - How can remove elements from array which elements are another array? 如何使用angularjs从数组中随机取n个元素? - How to take random n elements from array using angularjs? 如何从数组中取出每第n个连续元素? - How to take every nth continuous elements from an array? 如何从Java中的文本文件获取数组大小和元素的输入? - How to take array size and elements' inputs from a text file in java? 如何按顺序从数组中获取元素? - How can I sequentially take elements from an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM