size_t

Started by
11 comments, last by Polymorphic OOP 18 years, 5 months ago
What exactly is a size_t data type?
Advertisement
size_t is defined as the data type that is returned by the sizeof operator.
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]
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.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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>
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.
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?
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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.
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.
sizeof returns the number of bytes, but a byte isn't necessarily eight bits. By definition, a char occupies a single byte, however long that may be.

CM

This topic is closed to new replies.

Advertisement