Page 1 of 1

How Do We Define size_t?

Posted: Mon Sep 28, 2009 10:47 am
by Creature
Hello,

I was wondering on how to define size_t. I've been confused a number of times now and know that it might vary from OS to OS, or even compiler to compiler. I first decided to typedef size_t as unsigned long (32-bits) on x86 and unsigned long long (64-bits) on x64, which was fine by the compiler. x86 GCC tells me that operator new and such should take unsigned long (as size_t), whilst x64 GCC complains that I should be using unsigned int (as size_t) for my operators. I'm now a bit confused. As you can see, I originally intended to use size_t everywhere I knew would be extended in 64-bits (such as memory addresses and such), so they would become 64-bits values on x64, but apparently GCC thinks differently.

What would be the best way (or, what would you do, if it's a case of preference) to have a size_t "typedeffed" from?

Thanks,
Creature

Re: How Do We Define size_t?

Posted: Mon Sep 28, 2009 11:31 am
by Combuster
The C99 Standard wrote:size_t
which is the unsigned integer type of the result of the sizeof operator
(...)
The types used for size_t and ptrdiff_t should not have an integer conversion rank
greater than that of signed long int unless the implementation supports objects
large enough to make this necessary.
Make your choice :wink:

Re: How Do We Define size_t?

Posted: Mon Sep 28, 2009 3:12 pm
by Owen
Erm...

#include <stddef.h>?

Re: How Do We Define size_t?

Posted: Mon Sep 28, 2009 4:01 pm
by skwee
I define it as unsigned int.

Re: How Do We Define size_t?

Posted: Mon Sep 28, 2009 7:12 pm
by AndrewAPrice
Creature wrote:I was wondering on how to define size_t. I've been confused a number of times now and know that it might vary from OS to OS, or even compiler to compiler.
Most compilers have a default bunch of preprocessor symbols defined to say what architecture, OS, and compiler you're using. Test them, see what works, and if all else fails resort to unsigned int.

Re: How Do We Define size_t?

Posted: Mon Sep 28, 2009 11:39 pm
by Solar
Usually the compilers either provide appropriate symbols you can define size_t to, or they provide the freestanding headers themselves (float.h, iso646.h, limits.h, stdarg.h, stdbool.h, stddef.h, stdint.h).

It should be seldom necessary to define any of these things yourself. (Yes, I do so in PDCLib, but that is because I want to provide a generic base that is independant of what the compiler does. Usually you can rely on what the environment gives you.)

Re: How Do We Define size_t?

Posted: Tue Sep 29, 2009 5:28 am
by qw
In a 64-bits address space, I think size_t should be 64-bits as well. Unfortunately, the compiler may define sizeof differently.

Roel

Re: How Do We Define size_t?

Posted: Tue Sep 29, 2009 7:27 am
by Solar
In the end, it's the compiler defining the "correct" type of size_t (see the snippet from the standard documentation that Combuster posted). If you disagree, you have to get a different compiler. 8)

Re: How Do We Define size_t?

Posted: Tue Sep 29, 2009 8:31 am
by 01000101
I use "unsigned long" as the type for size_t, I've seen quite a few implementations that do that as well. But, I've also come across ones that use "unsigned int" instead.

Re: How Do We Define size_t?

Posted: Tue Sep 29, 2009 8:50 am
by Creature
I see, I don't have 64-bits support yet, but do hope so to have in the future (might also switch to 64-bits only, if I ever get it to work when I have the time).

Thanks for your feedback ;),
Creature

Re: How Do We Define size_t?

Posted: Tue Sep 29, 2009 9:46 am
by quok
If you're using GCC, just use GCC's built-in type. But make sure to provide something for size_t if GCC isn't being used (if you care to support that).

Code: Select all

#if defined(__GNUC__) && defined(__SIZE_TYPE__)
typedef __SIZE_TYPE__   __my_size_t;
#else
typedef unsigned long   __my_size_t;
#endif

typedef __my_size_t     size_t;

Re: How Do We Define size_t?

Posted: Tue Sep 29, 2009 10:46 am
by eddyb
Combuster wrote:
The C99 Standard wrote:size_t
which is the unsigned integer type of the result of the sizeof operator
(...)
The types used for size_t and ptrdiff_t should not have an integer conversion rank
greater than that of signed long int unless the implementation supports objects
large enough to make this necessary.
Make your choice :wink:
May I suggest something(just tested and it's working):

Code: Select all

typedef typeof(sizeof(int)) size_t;

Re: How Do We Define size_t?

Posted: Tue Sep 29, 2009 12:54 pm
by Solar
typeof() is not part of C99, and as such just as compiler-specific as using a compiler symbol...

And "I just tested it, it works" has fathered more bugs than any other error in judgement...

Re: How Do We Define size_t?

Posted: Wed Sep 30, 2009 4:38 am
by Creature
quok wrote:If you're using GCC, just use GCC's built-in type. But make sure to provide something for size_t if GCC isn't being used (if you care to support that).

Code: Select all

#if defined(__GNUC__) && defined(__SIZE_TYPE__)
typedef __SIZE_TYPE__   __my_size_t;
#else
typedef unsigned long   __my_size_t;
#endif

typedef __my_size_t     size_t;
I think this will certainly come in handy. I'm not planning to ever switch to another compiler (at the time, anyway). The only other compiler I use frequently is the VC++ compiiler, but as it doesn't support (and I'm not planning to convert to PE) ELF, I'm going to stick with GCC.

Thanks again,
Creature