Problem with Sizeof Operator

problem with sizeof operator

void getSize(int *S1)

When you pass an array to this function, it decays to pointer type, so sizeof operator will return the size of 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 array, in the function!

See the demonstration yourself here : http://www.ideone.com/iGXNU

Why does sizeof operator on an array give incorrect result?

Sizeof returns the size in bytes.

char x = 'a';
cout << sizeof(x); // this would be 1

This means that the space taken by the array of characters is their count * 1 byte

int on most modern machines is 32 bit (4 bytes)

If you had, let's say int array[] = { 1, 2, 3, 4, 5 }
and you wanted to get the count of its elements you might do the following:

cout << sizeof(array)/sizeof(int); // (20 / 4) -> 5





EDIT:

What you've declared here:

const char* strArray[]= {"Hi","How" ,"Howhuw","bdeuvc"};

It's an array that contains 4 char* (char pointers). The size of char pointer on your machine is 8 bytes.

char* x = "a";
cout << sizeof(x);

A pointer is a variable (you can think of it as an integer) that holds the adress in memory where the variable it's pointing at starts:

strArray[0] points at a memorry adress where "Hi" starts.

strArray[1] points at a memorry adress where "How" starts.

... and so on

Why is this sizeof operator giving error?

Parameter a is a pointer to an incomplete type. You cannot use sizeof on it since it is a pointer to an array and the size of the array isn't defined.

You can define it:

void func(int (*a)[10]){

In this case sizeof(*a) will equal to sizeof( int )*10 and sizeof(*a)/sizeof(int); will give the element count of a, which is 10 in this case.

Since you are passing a pointer to an array of size 3 you should use 3 not 10.
You can use forward parameters to have a variable sized argument a.

sizeof operator error on data types

According to the C standard (cited from ISO/IEC 9899:TC3 section 6.5.3.4):

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type.

So using a type name without parenthesis isn't legal.

Also, sizeof returns an implementation defined value of the type size_t and you probably should not cast it.

Why is the sizeof operator not evaluated in a for loop condition?

The problem is , the result of sizeof() operator is of type size_t, which is an unsigned type.

Next, in the comparison, i <= sizeof(i) as per the usual arithmetic conversion rules, -2, which is a signed value, gets promoted to an unsigned value, producing a huge value, evaluating the condition to false. So the loop condition is not satisfied and the loop body is not executed.

Run your program through a debugger and see the values in each step, it'll be more clear to you once you see the promoted values in the comparison.

How do i handle this situation C++ sizeof problem

If I have understood correctly you are trying to get the size of the passed array in the function.

Do it the following way

template <size_t N>
void f( int* ( &b )[N] )
{
cout << sizeof( b ) << endl;
}

In C you should declare the function with one more parameter like

void f(int* b[], size_t n )
{
printf( "%zu\n", n * sizeof( *b ) );
}

and call it like

f(a, sizeof( a ) / sizeof( *a ) );

Such an approach you can use also in C++.

Unexpected behaviour when using sizeof operator

When using the sizeof operator with a type, you must place the type in parentheses.

When using the sizeof operator with a variable, you may omit the parentheses.


See §6.5.3 Unary operators and §6.5.3.4 The sizeof and _Alignof operators in the C11 draft. Credit to @JonathanLeffler for identifying the sections.



Related Topics



Leave a reply



Submit