How Do We Define size_t?

Programming, for all ages and all languages.
Post Reply
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

How Do We Define size_t?

Post 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
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How Do We Define size_t?

Post 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:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: How Do We Define size_t?

Post by Owen »

Erm...

#include <stddef.h>?
User avatar
skwee
Member
Member
Posts: 73
Joined: Mon Jan 12, 2009 3:11 am

Re: How Do We Define size_t?

Post by skwee »

I define it as unsigned int.
TCP/IP: Connecting people...
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: How Do We Define size_t?

Post 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.
My OS is Perception.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: How Do We Define size_t?

Post 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.)
Every good solution is obvious once you've found it.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: How Do We Define size_t?

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: How Do We Define size_t?

Post 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)
Every good solution is obvious once you've found it.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: How Do We Define size_t?

Post 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.
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: How Do We Define size_t?

Post 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
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: How Do We Define size_t?

Post 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;
eddyb
Member
Member
Posts: 248
Joined: Fri Aug 01, 2008 7:52 am

Re: How Do We Define size_t?

Post 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;
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: How Do We Define size_t?

Post 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...
Every good solution is obvious once you've found it.
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: How Do We Define size_t?

Post 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
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
Post Reply