Problem with the sizeof operator

As I want to dynamically find the size of an array in a function, I used the sizeof operator. But I got an unexpected result. here is one demo to show you what i want to do.

//------------------------------------------------------------------------------------------ #include <iostream> void getSize(int *S1){ int S_size = sizeof S1/sizeof(int); std::cout<<"array size(in function):"<<S_size<<std::endl; } int main(){ int S[]={1,2,3,2,5,6,25,1,6,21,121,36,1,31,1,31,1,661,6}; getSize(S); std::cout<<"array size:"<<sizeof S/sizeof(int)<<std::endl; return 0; } //------------------------------------------------------------------------------------------ 

compilation command: g ++ demo1.cc -o demo1 {fedora 12}

output:

 array size(in function):2 array size:19 

Please explain why this is happening. what can be done to solve this problem.

+4
source share
6 answers
 void getSize(int *S1) 

When you pass an array to this function, it decomposes into a pointer type, so the sizeof operator will return the size of the pointer.

However, you define your function as

 template<int N> void getSize(int (&S1)[N]) { //N is the size of array int S_size1 = N; int S_size2 = sizeof(S1)/sizeof(int); //would be equal to N!! std::cout<<"array size(in function):"<<S_size1<<std::endl; std::cout<<"array size(in function):"<<S_size2<<std::endl; } int S[]={1,2,3,2,5,6,25,1,6,21,121,36,1,31,1,31,1,661,6}; getSize(S); //same as before 

then you can have the size of the array in the function!

Watch the demo here: http://www.ideone.com/iGXNU

+9
source

Inside getSize() you get a pointer size that is 8 bytes (since you are probably using a 64-bit OS). In main() you get the size of the array.

If you want to know the size of the array, pass the result of sizeof(S) as an additional argument to getSize() .

Other alternatives would use some container (e.g. std::vector ) or a rotation function into a template function, as Nawaz suggested.

+3
source

you get the size of the pointer to the array. If you need the size of the array, you need to multiply the number of elements by the size of each element.

0
source

You will need to pass the size of the array to the function.

Since you only pass a pointer to the first element in the array, your function has no information about its actual size.

 void getSize(int *S1, size_t size) { int S_Size = sizeof(*S1) * size; } 

This is superfluous if you think about it: D

0
source

S is int * , a pointer to an integer, which is the memory address that is on your machine twice as large as an integer.

If you need the size of the array (i.e. the number of elements), you cannot get it directly in pure C. But since this is a C ++ question, there is a way: use vector , which has the size() method.

Actually, this is not entirely true: inside the function that you declare S (and only if it is explicitly initialized at compile time, as in your example, even new int[19] does not work), the sizeof operator really gets the correct answer, therefore C ++ allows you to do this:

 int S[]={1,2,3,2,5,6,25,1,6,21,121,36,1,31,1,31,1,661,6}; vector<int> v(S, S + sizeof(S) / sizeof(int) ); 

and then you can use v.size() (see these docs ).

A version of the Nawaz template elsewhere is another great suggestion that forces the compiler to transfer full information about building a C ++ array (again, note that this is all known at compile time, so you can be explicit about size in the argument).

0
source

To prevent this type of accidental abuse of sizeof, you can define a function that only works with arrays:

 template<class T, int N> int array_size(T (&)[N]) { return N; } 

If you use this in your code, you will see a compiler error when applied to S1, since it is not an array. Also, it is shorter and slightly more explicit than sizeof array / sizeof array [0] (using the size of the first element means you don't have to repeat the type of the array).

This also already exists in Boost in a more general way (accepting something with a size method such as std :: vector).

0
source

Source: https://habr.com/ru/post/1336657/


All Articles