What is size_t?

Started by
25 comments, last by SiCrane 18 years, 8 months ago
I've seen it everywhere, and I have no idea what it is. Im guessing it's a structure that comes with some library like iostream...
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Advertisement
size_t in C and ::std::size_t in C++ are typedefs of built-in unsigned integral types which are defined to be the datatype which a sizeof expression yields. There are several standard files you can include in C and C++ to have access to size_t, but most-commonly it is recommended that you include <stddef.h> in C or <cstddef> in C++.
size_t is a type introduced by cstddef, it is an unsigned integer type that is the return type of the sizeof operator.
Wait, so your saying... I still don't get it. Pointers hold memory adresses, not values... But? DO I have to use size_t for sizeof()? God I'm lost...
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Was this type really worth creating for the sake of sizeof() or is its use more widespread?

ace
Is it just me or did a post lose its way somewhere in there?

Anyway, in answer to dbzprogrammer -- a pointer, in C++, is one of the many kinds of variables that exist, and the purpose of a variable (in C++) is to hold a value. The kinds of values that pointers hold are numerical addresses into memory, but they are values nonetheless. This is why you can do shifty things like pointer arithmetic with them.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
Quote:Was this type really worth creating for the sake of sizeof() or is its use more widespread?


What would you have sizeof return? Creating a type like that is the typesystem equivalent of using a pointer to indirectly refer to a variable. It is a Good Thing.

Quote:Is it just me or did a post lose its way somewhere in there?


He's replying to a post that has been deleted.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by ace_lovegrove
Was this type really worth creating for the sake of sizeof() or is its use more widespread?

Well, in theory it should be an unsigned integer of pointer size. Also, in theory the pointer size should equal the processor word size (16 bits on a 16-bit processer, 32 bits on a 32-bit processor, 64 bits on a 64-bit processor). If that assumption is correct, it would be quite useful in many scenarios.
But if size_t is actually an unsigned int then why not have sizeof() return an unsigned integer. Or is it simply because at some point a redefinition of size_t might happen and therefore anyone who has coded it properly will be fine?

Edit: Another post crept in, yeah i see what you mean ^^
ace
Arg, I know that already... They hold memory adress values, but from what im understanding size_t's don't. Plus when you do arithmatic you basically add a couple bytes until your offset is fullfiled
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"

This topic is closed to new replies.

Advertisement