在 C++ 中将数组传递给函数时,为什么 sizeof() 与主函数中的工作方式不同? [英] When passing an array to a function in C++, why won't sizeof() work the same as in the main function?

查看:18
本文介绍了在 C++ 中将数组传递给函数时,为什么 sizeof() 与主函数中的工作方式不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的 C++ 老师在课堂上告诉我们 C++ 中没有确定数组大小的函数,我对此并不满意.我在 stackoverflow 上发现了一个问题,它给出了这段代码 (sizeof(array)/sizeof(*array)) 虽然我不完全理解它,但我知道它需要总数量分配给数组的内存并将其除以我认为是其数据类型的默认内存分配...(???)我决定练习编写函数(我在 CS 111 - Fundamentals 1)并编写一个函数,该函数返回我传递给它的任何数组中的元素数.这是我写的:

So my C++ instructor told us in class that there was no function to determine an array size in C++ and I was not satisfied with that. I found a question here on stackoverflow that gave this bit of code (sizeof(array)/sizeof(*array)) and while I don't exactly understand it, I understand it takes the total amount of memory allocated to the array and divides it by what I assume is the default memory allocation of its data type...(???) I decided I wanted to practice writing functions (I'm in CS 111 - Fundamentals 1) and write a function that returned the number of elements in any array I passed it. This is what I wrote:

#include <iostream>
using namespace std;

int length_of_array(int some_list[])
{
    // This only returns the integer 1 for some reason
   return (sizeof(some_list)/sizeof(*some_list));
}

int main()
{
    // Declare and initialize an array with 15 elements
    int num_list[] = {2,4,6,8,10,12,14,16,18,20,22,24,26,28,30};

    //Outputs what I assume is the total size in bytes of the array
    cout << sizeof(num_list) << endl;

    //Outputs what I assume to be size of memory set aside for each in element in an array
    cout << sizeof(*num_list) << endl;

    //This extrapolates array's number of elements
    cout << "This is the output from direct coding in the\nint main function:\n" <<
            (sizeof(num_list)/sizeof(*num_list)) << endl;

    //This function should return the value 15 but does not
    int length = length_of_array(num_list);

    cout << "This is the length of the array determined\n";
    cout << "by the length_of_array function:\n"  << length << endl;



    return 0;
}

无论我做什么,该函数都返回 1.有人能给我一个 C++ 特定的解决方法并解释它是如何工作的吗?谢谢.

The function returns 1 no matter what I do. Would somebody please give me a C++ specific workaround and explanation of how it works? Thank you.

推荐答案

问题出在这里:

int length_of_array(int some_list[]);

基本上,每当您将数组作为函数的参数传递时,无论您是像 int arr[]int arr[42] 一样传递它,数组衰减为一个指针(有一个例外,见下文),所以上面的签名等价于

Basically, whenever you pass an array as the argument of a function, no matter if you pass it like int arr[] or int arr[42], the array decays to a pointer (with ONE EXCEPTION, see below), so the signature above is equivalent to

int length_of_array(int* some_list);

当然,在执行 sizeof(some_list)/sizeof(*some_list) 时,您将获得数组衰减到的指针大小与表示第一个元素的类型大小之间的比率.在您的情况下,1,因为在您的机器上看起来,指针的大小可能是 4 个字节(32 位),与 int 的大小相同.

So of course when doing sizeof(some_list)/sizeof(*some_list) you will get the ratio between the size of the pointer the array decayed to and the size of the type representing the first element. In your case, 1, as it looks like on your machine the size of a pointer is probably 4 bytes (32 bits), same as the size of an int.

所以我的 C++ 老师在课堂上告诉我们 C++ 中没有确定数组大小的函数,我对此并不满意.

你的老师错了!有一种方法可以通过引用传递数组并获取其大小:

YOUR TEACHER IS WRONG! There is a way of passing an array by reference and getting its size:

template<size_t N>
int length_of_array(int (&arr)[N])
{
    std::cout << N << std::endl; // WORKS!
    return N;
}

这篇关于在 C++ 中将数组传递给函数时,为什么 sizeof() 与主函数中的工作方式不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆