0

What is the difference between these two declarations?

int myints[5];

array<int,5> myints;

If I use the first declarations and the function size(), there will be a error "Member reference base type 'int [5]' is not a structure or union". But if I use the second declarations and the function size(), the program works. Why would the first declarations does not work?

#include <iostream>
#include <iomanip>
#include <array>

using namespace std;

int main()
{
//int myints[5];      //illegal

array<int,5> myints;  //legal

cout << "size of myints: " << myints.size() << endl;    //Error if I use the first declarations
cout << "sizeof(myints): " << sizeof(myints) << endl;
}
b4hand
  • 9,550
  • 4
  • 44
  • 49
Coleman Tung
  • 35
  • 1
  • 4
  • 4
    You should read a [good introductory book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – juanchopanza Aug 15 '13 at 08:44
  • Because the C++ committee thought that the language wasn't confusing enough and decided to use the word "array" for both arrays as such and a class that works like an array but is less annoying. This will make future discussions of whether arrays are pointers even more entertaining. – molbdnilo Aug 15 '13 at 09:22

6 Answers6

3

As others have pointed out, std::array is an extension added to C++11 (so you may not have it), which wraps a C style array, in order to give it some (but not all) of an STL-like interface. The goal was that it could be used everywhere a C style array could; in particular, it accepts the same initialization syntax as C style arrays, and if the initialization type allows static initialization, its initialization can be static as well. (On the other hand, the compiler cannot deduce its size from the length of the initializer list, which it can for the older C style arrays.)

With regards to size, any experienced programmer will have a size function in their toolkit, along the same lines as std::begin and std::end (which are C++11 extensions, and which everyone had in their toolkit before C++11 standardized them). Something like:

template <typename T>
size_t
size( T const& c )
{
    return c.size();
}

template <typename T, size_t n>
size_t
size( T (&a)[n] )
{
    return n;
}

(In modern C++, the second could even be constexpr.)

Given this, you write size( myInts ), regardless of whether it is an std::array or a C style array.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
1

array<int,5> myints uses an std::array, a template that overlays enhanced functionality on-top of a "basic" C/C++ array (which is what int myints[5] is). With a basic array, you are just reserving a chunk of storage space, and are responsible for keeping track of its size yourself (although you can use sizeof() to help with this).

With the std::array you get helper functions that can make the array safer and easier to use.

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
1

std::array is new in C++11. As you have found, it has a size function. This tells you how many items are in the array.
sizeof on the other hand tells you how much memory a variable is taking up i.e. its size in bytes.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

array is a template class that has size() as it's member function while int[] is simple C array

mshriv
  • 332
  • 1
  • 2
0

By using int myints[5]; , you are declaring an array of 5 ints on the stack, which is the basic C array.

Instead, by using array<int,5> myints; you are declaring an object of type array, which is a container class defined by the STL (http://en.cppreference.com/w/cpp/container/array), which in turns implements the size()function to retrieve the container's size.

The STL containers are built on top of the "basic" C types to provide extra functionality and to make it easier to manage them.

Banex
  • 2,890
  • 3
  • 28
  • 38
0

int myints[5]; has no function size() but you can do

int size = sizeof(myints)/ sizeof(int);

to get the size of the array.

so basically you can do:

#include <iostream>
#include <iomanip>
#include <array>

using namespace std;

int main()
{

int myintsArr[5];      //legal

array<int,5> myints;  //legal

cout << "size of myints: " << myints.size() << endl;    //Error if I use the first declarations
cout << "sizeof(myintsArr): " << sizeof(myintsArr)/ sizeof(int) << endl;

}

and get the same result from both the arrays

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
  • `sizeof(myints) / sizeof(myint[0])` is dangerous; if used in a function `void f( int a[10] )`, for example, it will give incorrect results (where as the usual template solution will fail to compile). – James Kanze Aug 15 '13 at 09:02
  • @JamesKanze what do you think on this - http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c – No Idea For Name Aug 15 '13 at 10:32
  • That it's the only solution available in C (which is how the article you link to is labeled), but that there are better solutions available (and in general use) in C++. – James Kanze Aug 15 '13 at 10:58