Different Sizeof Results

Different sizeof results

Because you can't pass entire arrays as function parameters in C. You're actually passing a pointer to it; the brackets are syntactic sugar. There are no guarantees the array you're pointing to has size 8, since you could pass this function any character pointer you want.

// These all do the same thing
void foo(char cvalue[8])
void foo(char cvalue[])
void foo(char *cvalue)

Different compilers showing different results for sizeof() operator

The declaration

unsigned const* const a, b;

is parsed as

unsigned const (* const a), b;

Thus, a and b have different types - a is a const pointer to const unsigned int, while b is just a const unsigned int.

This is an aspect of C declaration syntax that isn't well-taught and constantly trips people up. Declarations are split into two main parts - a sequence of declaration specifiers and a sequence of declarators. Pointer-ness, array-ness, and function-ness are specified as part of the declarator. For array and function declarators it's obvious because the [] and () operators are postfix, but for pointers it's confusing because a) the * operator is unary, not postfix, and b) whitespace isn't meaningful - T *p and T* p are both interpreted the same way, as T (*p).

For the declaration above, the declaration specifiers are unsigned const and the declarators are * const a and b.

C does not specify exact sizes for most types - instead, it specifies a minimum range of values those types must be able to represent. An unsigned int must be able to represent values in at least the range [0..65535], meaning it must be at least 16 bits wide, but it may be wider. Pointer types are as big as the architecture needs them to be, typically 8 bytes on a 64-bit system.

Why sizeof() method is giving different results?

In C, a character representation (like 'a') has type int. So, sizeof operator returns the size of an integer.

In C++, it's of a character type.

Why does the sizeof operator produce different results for an array

Arrays and pointers are not the same, and this is a prime example of this.

In most contexts, an array decays to a pointer to its first member. One of the few times this decay does not happen is when the array is the subject of the sizeof operator. In that case it refers to the entire array and the expression evaluates to the size of the entire array in bytes.

This is described in section 6.3.2.1p3 of the C standard:

Except when it is the operand of the sizeof operator, the _Alignof operator, or theunary & operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

As well as the C++11 standard in sections 7.2:

An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The temporary materialization conversion (7.4) is applied. The result is a pointer to the first element of the array.

And 8.3.3p4:

The lvalue-to-rvalue (7.1), array-to-pointer (7.2), and function-to-pointer (7.3) standard conversions are not applied to the operand of sizeof. If the operand is a prvalue, the temporary materialization conversion (7.4)is applied.

So what we actually have is:

int b = sizeof(array);     // size of the entire array
int c = sizeof(array[0]); // size of the first element of the array
int d = sizeof(&array[0]); // size of a pointer to an array element

Different results when using sizeof to count number of elements in array

It's because your template:

template <typename T>
void count(T a[])

decays to:

template <typename T>
void count(T a*)

so you're printing the sizeof a pointer, not of the array as your main is.

The error message says this:

will return size of int*

sizeof operator giving wrong results

The syntax int maxChainLen(val p[], int n) is equivalent to: int maxChainLen(val* p,int n) - the function actually accepts a pointer to val.

When you call this function, the array you pass decays to a pointer.

You can also see this answer for exact standard-backed wording.

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 sizeof(array) and sizeof(&array[0]) gives different results?

In the expression sizeof array, array is not converted to a pointer type.

C11, § 6.3.2.1 Lvalues, arrays, and function designators


Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.

Therefore, its type is char[20], not char *. The size of this type is sizeof(char) * 20 = 20 bytes.

C11, § 6.5.3.4 The sizeof and _Alignof operators


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

&array[0] type is char *. That's why the sizeof(&array[0]) gives the same result as sizeof(char *) (4 bytes on your machine).

C++ sizeof different result for the same value

When you use sizeof on arr in _tmain, you're taking the size of the array itself, which is 2 * sizeof(char*) = 2 * 4 = 8 on your architecture, because your char*s occupy 32 bits = 4 bytes.

When you use sizeof on b in test, you're taking the size of a pointer to the first element of the array, which is sizeof(char**) = 4 on your architecture. The reason you're taking the size of the pointer and not the array is that you can't pass arrays as such to functions - instead, you pass them via a pointer to their first element. The information about the actual size of the array is thus lost in the call. Put another way, the signature of your function test is equivalent to int test(char **b).



Related Topics



Leave a reply



Submit