i.e., the number of elements the array can hold?
executive summary:
int a[17];
n = sizeof(a)/sizeof(a[0]);
To determine the size of your array in bytes, you can use the sizeof operator:
int a[17];
int n = sizeof(a);
On my computer, ints are 4 bytes long, so n is 68.
To determine the number of elements in the array, we can divide the total size of the array by the size of the array element. You could do this with the type, like this
int a[17];
int n = sizeof(a) / sizeof(int);
and get the proper answer (68 / 4 = 17), but if the type of a changed you would have a nasty bug if you forgot to change the sizeof(int) as well.
So the preferred divisor is sizeof(a[0]), the size of the zeroeth element of the array.
int a[17];
int n = sizeof(a) / sizeof(a[0]));
Another advantage is that you can now easily parameterize the array name in a macro and get:
#define NELEMS(x) (sizeof(x) / sizeof(x[0]))
int a[17];
int n = NELEMS(a);
It is worth noting that sizeof
doesn't help if you are dealing with a pointer to an array:
int a[10];
int* p = a;
assert(sizeof(a) / sizeof(a[0]) == 10);
assert(sizeof(p) == sizeof(void*));
@Skizz: The standard wins over Wikipedia any day of the week :)
@DrPizza: Wikipedia does not contradict that sizeof(char)
is 1. In fact the article defines that sizeof(char)
is 1 by saying that sizeof
is relative to the size of char. (Note the difference between the "size of char" and "sizeof(char)
" :)
So, while sizeof(char) == 1
, that does not necessarily imply that a char occupies exactly one byte in memory. (But the standard apparently says so explicitly, so we're good)
@ Magnus: The standard defines sizeof as yielding the number of bytes in the object and that sizeof (char) is always one. The number of bits in a byte is implementation specific.
Skizz (the pedant)
Edit: ANSI C++ standard section 5.3.3 Sizeof:
The sizeof operator yields the number of bytes in the object representation of its operand. [...] sizeof (char), sizeof (signed char) and sizeof (unsigned char) are 1; the result of sizeof applied to any other fundamental type is implementation-defined.
Section 1.6 The C++ memory model:
The fundamental storage unit in the C++ memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set and is composed of a contiguous sequence of bits, the number of which is implementation-defined.
@Skizz: I am pretty sure I am right, although the best "source" I can give you at the moment is Wikipedia, from the article on sizeof:
wikipedia is wrong, Skizz is right. sizeof(char) is 1, by definition.
I mean, just read the WP entry really closely to see that it's wrong. "multiples of char". sizeof(char) can never be anything other than "1". If it were (say) 2, it would mean that sizeof(char) was twice the size of char!
The sizeof "trick" is the best way I know, with one small but (to me, this being a major pet peeve) important change in the use of parenthesis.
As the Wikipedia entry makes clear, C's sizeof
is not a function; it's an operator. Thus, it does not require parenthesis around its argument, unless the argument is a type name. This is easy to remember, since it makes the argument look like a cast expression, which also uses parenthesis.
So: If you have the following:
int myArray[10];
You can find the number of elements with code like this:
size_t n = sizeof myArray / sizeof *myArray;
That, to me, reads a lot easier than the alternative with parenthesis. I also favor use of the asterisk in the right-hand part of the division, since it's more concise than indexing.
Of course, this is all compile-time too, so there's no need to worry about the division affecting the performance of the program. So use this form wherever you can.
It is always best to use sizeof on an actual object when you have one, rather than on a type, since then you don't need to worry about making an error and stating the wrong type.
For instance, say you have a function that outputs some data as a stream of bytes, for instance across a network. Let's call the function send()
, and make it take as arguments a pointer to the object to send, and the number of bytes in the object. So, the prototype becomes:
void send(const void *object, size_t size);
And then you need to send an integer, so you code it up like this:
int foo = 4711;
send(&foo, sizeof (int));
Now, you've introduced a subtle way of shooting yourself in the foot, by specifying the type of foo
in two places. If one changes but the other doesn't, the code breaks. Thus, always do it like this:
send(&foo, sizeof foo);
Now you're protected. Sure, you duplicate the name of the variable, but that has a high probability of breaking in a way the compiler can detect, if you change it.