How Do We Define size_t?
How Do We Define size_t?
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
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.
- Combuster
- 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?
Make your choiceThe 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.
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: How Do We Define size_t?
Erm...
#include <stddef.h>?
#include <stddef.h>?
- AndrewAPrice
- Member
- Posts: 2299
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
Re: How Do We Define size_t?
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.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.
My OS is Perception.
Re: How Do We Define size_t?
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.)
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.
Re: How Do We Define size_t?
In a 64-bits address space, I think size_t should be 64-bits as well. Unfortunately, the compiler may define sizeof differently.
Roel
Roel
Re: How Do We Define size_t?
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.
Every good solution is obvious once you've found it.
Re: How Do We Define size_t?
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.
Website: https://joscor.com
Re: How Do We Define size_t?
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
Thanks for your feedback ,
Creature
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
Re: How Do We Define size_t?
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?
May I suggest something(just tested and it's working):Combuster wrote:Make your choiceThe 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.
Code: Select all
typedef typeof(sizeof(int)) size_t;
Re: How Do We Define size_t?
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...
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.
Re: How Do We Define 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.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;
Thanks again,
Creature
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.