Skip to main content
GameDev.net gamedev.net
🔒 Locked

size_t

Started by Simplicity Oct 23, 2005 at 1:57 PM 11 replies 1.5k views
Original Post
Simplicity
Simplicity
What exactly is a size_t data type?
snk_kid
snk_kid
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]
LessBread
LessBread
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
desertcube
desertcube
Quote:
Original post by LessBread
To clarify, sizes means number of bytes. The sizeof operator returns the size in bytes of its operand.



sizeof actually returns the size in number of char's, which generally speaking is one byte, but worth remebering. See MSDN for details.
SiCrane
SiCrane
Quote:
Original post by desertcube

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

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.
LessBread
LessBread
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.



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


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
pants
pants
Quote:
Original post by SiCrane
Quote:
Original post by desertcube

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

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.
SiCrane
SiCrane
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.
Conner McCloud
Conner McCloud
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
SiCrane
SiCrane
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?
ph33r
ph33r
Quote:
Really? So what do you think sections 1.7 and 5.3.3 in the standard mean?


I'm not sure what it says, can you tell me?
Polymorphic OOP
Polymorphic OOP
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.