NOWA
NOWA

Reputation: 53

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

I'm new to C++ programming and I'm trying to get the size of an array. Can anybody explain me why this happens? I've tried running the code in runnable.com and the same result shows.

I'm sure this is not the right way to do it. If possible, can you suggest any EASY way to get the size of that kind of 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 : Thank you for your responses. flies away :D

Upvotes: 5

Views: 1023

Answers (6)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

The size of an array is equal to the sum of sizes of all its elements. As in your example you deal with arrays of type int then it seems sizeof( int ) in your system is equal to 4, So as the first array has 6 elements then its size is equal to 6 * sizeof( int )that is 24. The size of the second array is equal to 3 * sizeof( int )that is 12. If for example sizeof( int ) in your system would be equal to 8 then the size of the first array would be equal to 48 ( 6 * sizeof* int ) ) and the size of the second array would be equal to 24 ( 3 * sizeof( int ) ).

So if you want for example to know how many elements there are in an array you can calculate this the following way

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

or

sizeof( YouArray ) / sizeof( ElementType )

Upvotes: 4

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275385

#include <cstddef>

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

is a template that solves your problem.

A more industrial-strength version in C++11 is:

#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. Note that non-random access iterators in containers that lack a size will probably fail to compile (as - is usually not defined), which is sort of on purpoes (we don't want to accidentally do O(n) work).

Upvotes: 0

Mr.C64
Mr.C64

Reputation: 42934

To get the count of elements in an array allocated on the stack, the usual technique is to get the total size, in bytes, of the whole array, and then divide that by the size, in bytes, of a single array item:

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

However, this formula can be applied only to arrays allocated on the stack, not to arrays dynamically allocated on the heap (e.g. using new[]): in this latter case, you need to keep the array item count as a separate piece of information.

Microsoft Visual C++ provides a convenient _countof() macro, that does the above calculation for you, and safely breaks the compilation process if the argument is a pointer e.g. to a dynamically allocated array (since in that case the above formula gives an incorrect result, i.e. the size of a pointer - which is fixed, e.g. 4 bytes on 32-bit systems, and unrelated to the pointed-to array size - divided by the size of the first pointed item).

It seems there is an equivalent safe ARRAY_SIZE() macro for Linux.

Upvotes: 0

saruftw
saruftw

Reputation: 1164

sizeof() gives you the total size in bytes. For example, if int is 4, and you have 6 elements in array. It'll return 4*6=24 as the answer. Thus the following code will give you the size of 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))/4 << endl;
  cout << (sizeof(set234))/4 << endl;


  return 0;
}

You can also use this :

#include <iostream>
#include <array>

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

Upvotes: 0

Alan Stokes
Alan Stokes

Reputation: 18964

sizeof returns the number of bytes, not the number of elements. Mostly you should avoid arrays; use std::vector or std::array, both provide an easy way to get the number of elements.

Upvotes: 1

Doug Richardson
Doug Richardson

Reputation: 10811

sizeof returns the size of the array in bytes, not elements. It looks like you're running on a 32-bit system (or LP64, meaning only longs and pointers are 64-bit, but ints are still 32), so each element in your integer array is 4 bytes. In your set1, which is 6 elements, that is 6x4=24 bytes.

To get the size in elements, do sizeof(set1)/sizeof(set1[0]). Not however, that works for arrays where you've listed the elements at compile time. If, however, you just have an int*, the sizeof operator will return the size of the pointer.

Upvotes: 0

Related Questions