Jump to content

  • Log In with Google      Sign In   
  • Create Account


different behavior of "sizeof()"


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 はとぶ   Members   -  Reputation: 319

Like
0Likes
Like

Posted 21 July 2012 - 03:51 PM

Hi,

I have just come to notice something strange in the behavior of sizeof() function, if I write:

int checkSize( char *str )
{
	return sizeof( str );
}

void main()
{
	char text[30];
	int size = sizeof( text );  // size = 30
	int size2= checkSize( text ); // size2 = 4
}

size gets the value of the length of the array, but size2 gets 4 (the size of the sole pointer).

does anyone know why?...

is this the standard way of C++ to behave?... or is it maybe compiler thing?

Thanks!
"lots of shoulddas, coulddas, woulddas in the air, thinking about things they shouldda couldda wouldda donne, however all those shoulddas coulddas woulddas ran away when they saw the little did to come"

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9575

Like
5Likes
Like

Posted 21 July 2012 - 03:57 PM

This is how sizeof is supposed to work. If you use sizeof on an array you get the size of the array. If you use sizeof on a pointer you get the size of the pointer. It doesn't matter what the pointer points to; if you ask for the size of a pointer, you'll get the size of the pointer.

#3 Servant of the Lord   Crossbones+   -  Reputation: 18743

Like
0Likes
Like

Posted 21 July 2012 - 04:15 PM

@SiCrane: If you passed an array instead of a pointer, would it return 4 or 30?

int checkSize( char []str )
{
		return sizeof( str );
}

Edited by Servant of the Lord, 21 July 2012 - 04:16 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#4 phantom   Moderators   -  Reputation: 7156

Like
0Likes
Like

Posted 21 July 2012 - 04:19 PM

I think in that case you would still get '4' as char[] str has no size and is thus treated as a pointer.

#5 iMalc   Crossbones+   -  Reputation: 2301

Like
6Likes
Like

Posted 21 July 2012 - 04:25 PM

Still just the size of a pointer there. Note that you got the [] in the wrong place though. In C++ it comes after the variable name.

The bit of knowledge that you are lacking which would enable you to work out why it is like this, and answer the question for yourself, is that sizeof gets replaced with a single number at compile time. That clearly demonstrates how neither checkSize function case could result in returning the size of the array whose pointer was passed in, because there is no way for this function to return anything but the same value every time.
Then if you think about how it is used in main (which incidentally must return int, not void) The array that you are using it on can only be of one size, so you get that array's size no problem.

Edited by iMalc, 21 July 2012 - 04:26 PM.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

#6 はとぶ   Members   -  Reputation: 319

Like
0Likes
Like

Posted 21 July 2012 - 04:50 PM

Thanks!

yes, I tried also using char str[] as argument too, but it returned the same value.

I think iMalc's answer really pointed out how C++ decides to return the value when using sizeof().

Thanks to all of you!
"lots of shoulddas, coulddas, woulddas in the air, thinking about things they shouldda couldda wouldda donne, however all those shoulddas coulddas woulddas ran away when they saw the little did to come"

#7 SiCrane   Moderators   -  Reputation: 9575

Like
4Likes
Like

Posted 21 July 2012 - 04:54 PM

@SiCrane: If you passed an array instead of a pointer, would it return 4 or 30?

In C++, a function argument with a type array of T is automatically transformed into a type of pointer to T. This includes function arguments where the size of the array is specified. So even if you made the argument a char[30] you'd still get a pointer size back.

#8 e‍dd   Members   -  Reputation: 2105

Like
4Likes
Like

Posted 21 July 2012 - 09:03 PM

A useful trick is passing a reference to an array to prevent decaying to a pointer.

template<typename T, std::size_t N>
std::size_t array_length(const T (&array)[N])
{
    return sizeof array / sizeof *array;
}

Of course, if you pass it a string literal, you'll get an length that includes the '\0'.

#9 rip-off   Moderators   -  Reputation: 8166

Like
6Likes
Like

Posted 22 July 2012 - 04:12 AM

Alternative implementation:
template<typename T, std::size_t N>
std::size_t array_length(const T (&array)[N])
{
    return N;
}


#10 dilyan_rusev   Members   -  Reputation: 923

Like
0Likes
Like

Posted 22 July 2012 - 06:14 AM

And these will work only on static arrays. As the others said, sizeof gets replaced with a number at compile time. That is why all the WIN_STRUCT_NAME.cbSize vooodo works, otherwize this mechanism would be pretty much pointless.
I would advice you to either pass the size (with an additional argument, as C functions do), or, even better, use the built-in array (in c++11) or vector for dynamic arrays. You get bound checking for free (I hate out-of-index).

#11 e‍dd   Members   -  Reputation: 2105

Like
3Likes
Like

Posted 22 July 2012 - 06:31 AM

Alternative implementation:

template<typename T, std::size_t N>
std::size_t array_length(const T (&array)[N])
{
	return N;
}


Face-palming somewhat right now.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS