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

I am new to C ++ programming and I am trying to get the size of an array. Can someone explain to me why this is happening? I tried to run the code on runnable.com and showed the same result.

I am sure this is the wrong way to do this. If possible, can you suggest any EASY way to get the size of such an array?

#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
****************************************************************

** EDIT: Thanks for your answers. flies away: D

+4
source share
6 answers

. , int, , sizeof (int) 4. 6 , 6 * sizeof( int ), 24. 3 * sizeof( int ), 12. , , sizeof (int) 8, 48 (6 * sizeof * int)), 18 (3 * sizeof (int)).

, , , , , .

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

sizeof( YouArray ) / sizeof( ElementType )
+3

sizeof , . ; std::vector std::array, .

+1

sizeof , . , 32- ( LP64, , longs 64-, int 32), 4 . 1, 6 , 6x4 = 24 .

, sizeof (set1)/sizeof (set1 [0]). , , . , , int *, sizeof .

0

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;
}
0

, , , , :

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

, , , (, new[]): .

Microsoft Visual ++ _countof(), , , - , ( , .. - , , 4 32- , ).

, ARRAY_SIZE() Linux.

0
#include <cstddef>

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

- , .

++ 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);
}

which probably goes too far. Please note that non-random access iterators in containers that are not sized will probably not be able to compile (since it is -usually not defined), which is similar to purpoes (we don’t want to accidentally do O (n) work).

0
source

All Articles