简体   繁体   English

传递给函数的数组对象的大小

[英]Size of array object passed to function

I am running the following code in C++:我在 C++ 中运行以下代码:

class Tile {
    //class definition
};

bool myFunction(Tile* Tiles) {
    sizeof(Tiles);
    sizeof(Tiles[0]);
    //other code
}

int main () {
    Tile tiles [16];
    //other stuff
    myFunction(tiles);
    return 0;
}

When I use the sizeof method as depicted above, it only gives me the size of the pointer, rather than the actual object.当我使用上面描述的 sizeof 方法时,它只给我指针的大小,而不是实际对象。 I am trying to find the size of the object.我试图找到对象的大小。 My question is how do I take the size of the object given a pointer?我的问题是如何获取给定指针的对象大小?

Arrays naturally decays to pointers, and when they do that all size information is lost.数组自然会衰减为指针,当它们这样做时,所有大小信息都会丢失。 The most common solution is to pass along the number of elements in the array as an argument.最常见的解决方案是将数组中的元素数量作为参数传递。 It's also possible to use templates to deduce the array size.也可以使用模板来推断数组大小。

Or use std::array (or std::vector , depending on situation) instead, which is the solution I recommend.或者使用std::array (或std::vector ,视情况而定),这是我推荐的解决方案。

The simplest, C-sytle way is to pass the array size as a parameter.最简单的 C 风格方法是将数组大小作为参数传递。

bool myFunction_ver1(Tile* Tiles, std::size_t size)
{
    //...
}

But C++ offers more.但是 C++ 提供了更多。 Since array size is contained in the data type of the array, a template can help.由于数组大小包含在数组的数据类型中,因此模板可以提供帮助。 The array should be passed by reference to prevent the array from being adjusted to pointer.数组应该通过引用传递,以防止数组被调整为指针。

template <std::size_t N>
bool myFunction_ver2(const Tile (&Tiles)[N])
{
    // N is the array size
}

But we should prefer using std::array or std::vector instead of raw array.但是我们应该更喜欢使用std::arraystd::vector而不是原始数组。

template <std::size_t N>
bool myFunction_ver3(const std::array<Tile, N>& Tiles)
{
    // You may use N or Tiles.size()
}

bool myFunction_ver4(const std::vector<Tile>& Tiles)
{
    // use Tiles.size()
}

Raw arrays decay to pointers as function arguments, so any length information associated with the type is erased without using templates.原始数组衰减为指针作为函数参数,因此在不使用模板的情况下擦除与类型相关的任何长度信息。 So, using raw arrays you can pass the size of the array as an argument.因此,使用原始数组,您可以将数组的大小作为参数传递。

bool myFunction(std::size_t n, Tile tiles[n]) {

  std::size_t z = n * sizeof(Tile);

  // etc...
}

Using either std::vector or std::array is generally a better solution though.不过,使用std::vectorstd::array通常是更好的解决方案。

Answering my own question:回答我自己的问题:

Why I'm posting this answer为什么我要发布这个答案

I was new to C++ at the time, and other questions have been marked as a duplicate of this.当时我是 C++ 的新手,其他问题已被标记为与此相同。 As such, I'm guessing that other questions like this are from C++ newbies.因此,我猜其他像这样的问题来自 C++ 新手。 This answer will be for the benefit of those newbies.这个答案将是为了那些新手的利益。

My answer我的答案

Unlike in higher-level languages like Python or JAVA, the length of an array is not stored with the contents of the array, so there is no way to access it.与 Python 或 JAVA 等高级语言不同,数组的长度与数组的内容一起存储,因此无法访问它。

As such, it must be passed as a separate parameter.因此,它必须作为单独的参数传递。 It may not make much sense in higher-level languages, but it's necessary in both C and C++.它在高级语言中可能没有多大意义,但在 C 和 C++ 中都是必要的。

Also, unlike in Python and JAVA, an array is not an object.此外,与 Python 和 JAVA 不同,数组不是对象。 To fully understand C/C++ arrays, one first must understand pointers.要完全理解 C/C++ 数组,首先必须理解指针。

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

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