在初始化的、未知大小的数组-C++中使用sizeof()

Using sizeof() in an initialized, unknown size array - C++

本文关键字:-C++ sizeof 数组 初始化 未知      更新时间:2023-10-16

我是C++编程的新手,我正在尝试获取数组的大小。有人能解释一下为什么会发生这种事吗?我试过在runnable.com上运行代码,结果也是一样。

我确信这不是正确的方法。如果可能的话,你能建议任何简单的方法来获得这种数组的大小吗?

#include <iostream>
using namespace std;
int main ()
{
  int set1[] = {1, 9, 3, 50, 31, 65};
  int set234[] = {3, 5, 5};
  cout << sizeof(set1) << endl;
  cout << sizeof(set234) << endl;
  cout << "When sizeof() return value is divided by 4"<< endl;
  cout << sizeof(set1) / 4 << endl; 
  cout << sizeof(set234) / 4 << endl;
  return 0;
}
****************************************************************
Output:
24
12
When sizeof() return value is divided by 4
6
3
****************************************************************

**编辑:感谢您的回复飞走了:D

数组的大小等于其所有元素的大小之和。在你的例子中,你处理的是int类型的数组,那么系统中的sizeof(int)似乎等于4,所以第一个数组有6个元素,那么它的大小等于6 * sizeof( int ),也就是24。第二阵列的大小等于CCD_ 2,即12。例如,如果系统中的sizeof(int)等于8,那么第一个数组的大小将等于48(6*sizeof*int)),而第二个数组的尺寸将等于24(3*sizeof(int))。

因此,例如,如果你想知道一个数组中有多少元素,你可以用以下方法计算

sizeof( YouArray ) / sizeof( YourArray[0] )

sizeof( YouArray ) / sizeof( ElementType )

sizeof返回字节数,而不是元素数。大多数情况下,您应该避免数组;使用std::vectorstd::array,两者都提供了一种获取元素数量的简单方法。

sizeof返回数组的大小(以字节为单位),而不是元素。它看起来像是在32位系统上运行(或LP64,意味着只有long和指针是64位,但int仍然是32位),所以整数数组中的每个元素都是4个字节。在您的set1中,即6个元素,即6x4=24个字节。

要获取元素中的大小,请执行sizeof(set1)/sizeof(set1[0])。然而,对于在编译时列出了元素的数组,这并不适用。但是,如果您只有一个int*,sizeof运算符将返回指针的大小。

sizeof()提供以字节为单位的总大小。例如,如果int是4,并且数组中有6个元素。它将返回4*6=24作为答案。因此,下面的代码将为您提供一个数组的大小。

#include <iostream>
using namespace std;
int main ()
{
  int set1[] = {1, 9, 3, 50, 31, 65};
  int set234[] = {3, 5, 5};
  cout << (sizeof(set1))/4 << endl;
  cout << (sizeof(set234))/4 << endl;

  return 0;
}

你也可以使用这个:

#include <iostream>
#include <array>
int main ()
{
  array<int,5> a;
  cout << "size is" << a.size();
return 0;
}

要获得堆栈上分配的数组中元素的计数,通常的技术是获得整个数组的总大小(以字节为单位),然后将其除以单个数组项的大小(以比特为单位):

<item count> = <total array size in bytes> / <single array item size in bytes>

但是,此公式只能应用于堆栈上分配的数组,而不能应用于堆上动态分配的数组(例如使用new[]):在后一种情况下,您需要将数组项计数作为一条单独的信息。

Microsoft Visual C++提供了一个方便的_countof()宏,可以为您进行上述计算,如果参数是指针,则安全地中断编译过程,例如指向动态分配的数组(因为在这种情况下,上面的公式给出了不正确的结果,即指针的大小除以第一个指向项的大小,指针的大小是固定的,例如在32位系统上为4个字节,与指向数组的大小无关)。

似乎有一个适用于Linux的安全ARRAY_SIZE()宏。

#include <cstddef>
template<class T, std::size_t N>
std::size_t length_of( T(&)[N] ) {
  return N;
}

是解决您问题的模板。

C++11中更具工业强度的版本是:

#include <array>
template<class T, std::size_t N>
constexpr std::size_t size( T(&)[N] ) {
  return N;
}
template<class T, std::size_t N>
constexpr std::size_t size( std::array<T,N> const& ) {
  return N;
}
template<class C,class=void>
struct has_size : std::false_type {};
template<class>using void_t=void;
template<class C, void_t< decltype( std::declval<C&>().size() ) > >
struct has_size : std::true_type {};
template<bool b, class T=void>using enable_if_t=typename std::enable_if<b,T>::type;
template<class C, class=enable_if_t< has_size<C>::value, bool[1] >>
constexpr std::size_t size( C const& c ) {
  return c.size();
}
template<class C, class=enable_if_t< !has_size<C>::value, bool[2] >>
constexpr std::size_t size( C const& c ) {
  using std::begin; using std::end;
  return end(c)-begin(c);
}

这可能太过分了。请注意,容器中缺少大小的非随机访问迭代器可能无法编译(因为通常没有定义-),这有点不合目的(我们不想意外地做O(n)工作)。