Sign in to follow this  
Simplicity

size_t

Recommended Posts

Quote:
Original post by Konfusius
size_t is defined as the data type that is returned by the sizeof operator.


To elaborate further size_t is a type alias for some unsigned integral type (does not necessarily mean unsigned int) used to refer to sizes, subscript access for operator overloading etc.

Then there is ptrdiff_t, ptrdiff_t is a type alias for some signed integral type which represents the difference between two pointers (hence the abbreviation ptr-diff-t).

Use these for portable behaviour, they are both defined in header stddef.h for C and in cstddef for C++.

[Edited by - snk_kid on October 23, 2005 2:10:44 PM]

Share this post


Link to post
Share on other sites
Quote:

To elaborate further size_t is a type alias for some unsigned integral type (does not necessarily mean unsigned int) used to refer to sizes, subscript access for operator overloading etc.


To clarify, sizes means number of bytes. The sizeof operator returns the size in bytes of its operand.

Share this post


Link to post
Share on other sites
Quote:
Original post by LessBread
To clarify, sizes means number of bytes. The sizeof operator returns the size in bytes of its operand.


<nitpick>
sizeof actually returns the size in number of char's, which generally speaking is one byte, but worth remebering. See MSDN for details.
</nitpick>

Share this post


Link to post
Share on other sites
Quote:
Original post by desertcube
<nitpick>
sizeof actually returns the size in number of char's, which generally speaking is one byte, but worth remebering. See MSDN for details.
</nitpick>

The sizeof a char is defined to be one byte. If an implementation has a sizeof(char) != 1 then it isn't compliant with the C++ standard.

Share this post


Link to post
Share on other sites
Quote:
Original post by desertcube
Quote:
Original post by LessBread
To clarify, sizes means number of bytes. The sizeof operator returns the size in bytes of its operand.


<nitpick>
sizeof actually returns the size in number of char's, which generally speaking is one byte, but worth remebering. See MSDN for details.
</nitpick>


It depends on who you ask. The C Book - 5.5. Sizeof and storage allocation. Note the first sentence: The sizeof operator returns the size in bytes of its operand.

Also note that further down the MSDN page chars and bytes are used interchangeably:

Quote:

When the sizeof operator is applied to an object of type char, it yields 1. When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier.


Why did the writer use bytes instead of number of chars? Perhaps because bytes are easier to understand? I could have been pedantic and used the formal definition, but my purpose was to make it easier to understand, not to be in strict conformance with the language specification. There's a huge difference.

// I see that I was in conformance with the spec - but not with Microsoft. Why is it that Microsoft has to put it's own spin on everything?

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by desertcube
<nitpick>
sizeof actually returns the size in number of char's, which generally speaking is one byte, but worth remebering. See MSDN for details.
</nitpick>

The sizeof a char is defined to be one byte. If an implementation has a sizeof(char) != 1 then it isn't compliant with the C++ standard.


sizeof(char) == 1 because sizeof returns the # of chars not the # of bytes. A char is not guaranteed to be 1 byte, just like an integer, short, long or byte are not guaranteed to be specific sizes.

Share this post


Link to post
Share on other sites
The way I read it, MS isn't really putting it's own spin on things. It's just a somewhat awkward subject to describe clearly and it's easy to make it sound different that it actually is.

Share this post


Link to post
Share on other sites
Quote:
Original post by pants
sizeof(char) == 1 because sizeof returns the # of chars not the # of bytes. A char is not guaranteed to be 1 byte, just like an integer, short, long or byte are not guaranteed to be specific sizes.


Really? So what do you think sections 1.7 and 5.3.3 in the standard mean?

Share this post


Link to post
Share on other sites
Quote:
Original post by pants
sizeof(char) == 1 because sizeof returns the # of chars not the # of bytes. A char is not guaranteed to be 1 byte, just like an integer, short, long or byte are not guaranteed to be specific sizes.

In the C++ standard, a byte is defined as the sizeof a char, since sizeof yields the number of bytes and sizeof char is always 1. Even though it may not be what YOU consider a byte, it is a byte by definition.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this