Hi, I was confused with the "sizeof()" function. In the main(), why calling the function getSizeof(list1) will return 4; while calling sizeof(list1) return 72.
I want(thought) that getSizeof(list1) should return 72 as well.
Thank you for any comment. Following is my code.
--------------------------------------------------
#include <iostream>
#include <string>
using namespace std;
int getSizeOf(double []);
int main()
{
double list1[]={5,789,34,2,24,22,111,4552,156};
In the case of the sizeof(list1) call, the size of list1 is known at compile-time, so sizeof() returns the byte-size of the array. In the getSizeOf() function, the size of "arr" isn't known at compile-time, so sizeof() returns the size of a pointer to double, which is 4 bytes.
sizeof works, in your case, by looking at the type of the variable you are trying to take the size of.
In main(), the compiler is smart enough to realize that list1 is an array of 9 doubles, since the compiler
sees the declaration of list1. Therefore 9 * sizeof double = 72.
In getSizeOf(), the compiler sees that arr is an array of doubles, but it does not know how many doubles
are in the array, since you did not specify. The compiler therefore reverts to treating the array arr as a
pointer, or in other words, it considers arr to be a double*. The size of a pointer-to-double is 4 on a 32
bit platform.
template <typename T,unsigned S>
unsigned ArraySize(const T (&v)[S]) // I hope i did this right ... can't compile to test right now
{
return S;
}
int main()
{
double foo[] = {1,2,3,4,5,6};
cout << ArraySize(foo); // prints '6'
// but note that only works with array names, not dynamically allocated arrays
int* bar = newint[10];
cout << ArraySize(bar); // ERROR, 'bar' is not an array name
}
void function(int[] arr)
{
// it also doesn't work if you pass the array to a function by pointer, because then it
//becomes a pointer:
cout << ArraySize(arr); // ERROR, 'arr' is a pointer, not an array name
}
Just for the reminder. Despite of acting like a function, it is still a keyword according to the manual.
Also it does not count a reserved memory but reads its predefined substitute text.
The problem he's having is one I had before, too. In main, list1 is of type int[9]. When he passes it to the function, it's, however, read as int*. The former returns the size of an int, times 9. The latter will return the size of an int, since it doesn't know the amount of integers the array contains.
To chipmunck: typename and class in template declarations are completely interchangeable;
there is absolutely no difference under any circumstance.
The argument is a const reference to an array of S elements of type T. It's best to leave
off the variable name for the parameter since the function doesn't actually use it; a good
compiler would generate a warning at the highest warning level.
Disch's solution is better than coder's solution because, for example, coder's numof()
solution would compile if, say, I passed in a std::vector<std::string> (or any other object
that supported an operator[]), at which point, in this case, it will yield the constant value
sizeof( std::vector ) / sizeof( std::string ) -- not the value you want under any circumstance.
In fact, Disch's solution is a very standard way to get the compiler to tell you the size
of an array, regardless of whether it looks "weird".
@jssmith
yes..i got cold feet and looked it up and found that sizeof() was an operator!!! ( not a key word). Since I have thought it to be a function for about 10 years and what I know about C++ is very small compared with what I don`t know.....its a bit like cashing in your piggy bank to find it full of washers.
Anyway thanks for sorting me out.
BTW
int x = sizeof x;
shouldn`t the second x be in( ) or are these optional.
rgds.
And yes, the parentheses are not always necessary. But practically no one knows the precedence rules for sizeof, so it's better to just always leave them. They don't hurt and the make the code easier to read.
well Disch and jssmith, you're right. it Disch's solution might be saver.
my method suffers from the same flaw sizeof does (taking everything). but it's easy to understand. i don't know how many ppl understand Disch's solution. and i for my part wouldn't use what i don't understand.
@buffbill
sizeof is both operator and keyword. and yes those () are optional
The only difference is that it's a two argument-template function, rather than one. He uses the address (with &) and then proceeds with the [S] to tell the compiler the amount of "values" the array holds. It's nothing special, you just need to look at it in small portions, not as a whole to understand it (when you haven't got a fine grip of what templates are, no offence intended).