当将数组传递给C ++中的函数时,为什么sizeof()无法与main函数一样工作? [英] When passing an array to a function in C++, why won't sizeof() work the same as in the main function?

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

问题描述

所以我的C ++老师在课堂上告诉我们,没有函数可以确定C ++中的数组大小,对此我并不满意.我在这里发现了一个关于stackoverflow的问题,该问题给出了这段代码(sizeof(array)/sizeof(*array)),尽管我不太了解它,但我知道它需要分配给数组的内存总量,然后除以我认为的默认内存分配其数据类型...(???) 我决定要练习编写函数(我在CS 111-基础知识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]那样传递数组,该数组都会衰减为指针(具有一个EXCEPTION,请参见下文),因此签名以上等同于

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()无法与main函数一样工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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