Skip to main content
gamedev.net
🔒 Locked

size_t?

Started by nickwinters Mar 10, 2004 at 1:59 AM 11 replies 4.1k views
Original Post
nickwinters
nickwinters
I''m looking through some source code, and noticed a lot of use of size_t. What is it? After searching google, I can see that it''s used a lot, but no info on what it is or what it''s used for. And is there an equivalent in C#? I''m attempting to convert the code to C# so it can be used with the rest of my .NET program. Thanks. -Nick
daerid
daerid
As far as I''ve learned, size_t is defined as the type that sizeof() returns.


daerid | Legends | Garage Games | Spirit | Hapy | Boost | Python | Google
"Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool
daerid@gmail.com
aboeing
aboeing
I''m pritty sure size_t is defined as the machines word size. (as in the real meaning of word - ie: the ''size'' of the register, eg, 4 bytes, 32 bit on a pentium 4, and 8 bytes, 64bit on an opteron)

So I dont think treating a size_t as an unsigned long in c# will work. - I''m also not familiure with C#.
jperalta
jperalta
There is no equivalent of the size_t data type in C#.
DrPizza
DrPizza
size_t is an integer large enough to describe the size of all possible objects.

In practice, that makes it the sizeof a pointer; its closest C# equivalent would probably be IntPtr.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
superdeveloper
superdeveloper
in VC++, I believe size_t is equivalent to a LONGLONG (64 bit).

Is this correct?

I continually must typecast it into an (unsigned int) so I get no warnings for loss of precision (even though I KNOW my size is never that 4 gigs)...
psykr
psykr
In VC6, size_t is typedef''d as an unsigned long, 32 bits.
hlivingstone
hlivingstone
so much mystery surrounds this data type, i am ammused
quasar3d
quasar3d
#ifndef _SIZE_T_DEFINED
#ifdef _WIN64
typedef unsigned __int64 size_t;
#else
typedef _W64 unsigned int size_t;
#endif
#define _SIZE_T_DEFINED
#endif

My Site
Fruny
Fruny
quote:
Original post by superdeveloper
in VC++, I believe size_t is equivalent to a LONGLONG (64 bit).

Is this correct?

I continually must typecast it into an (unsigned int) so I get no warnings for loss of precision (even though I KNOW my size is never that 4 gigs)...


That''s because you''ve got the "64-bit portability" warning enabled. On a 64-bit platform, size_t will be 64 bits, which will be larger than unsigned int. An explicit cast silences that warning, but doesn''t make the code portable.

size_t is both the type returned by sizeof, and the type of array size/array index parameters.


“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 (C programming language co-inventor)
"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
superdeveloper
superdeveloper
hmm better turn that off then...

Is 64-bit compatibility on by default? I don''t ever remeber modyfing that setting, but I use fread all the time, which returns a 64 bit size_t..
Fruny
Fruny
quote:
Original post by superdeveloper
hmm better turn that off then...

Is 64-bit compatibility on by default? I don''t ever remeber modyfing that setting, but I use fread all the time, which returns a 64 bit size_t..


Yes, it''s on by default.

Note that your size_t *is* 32 bit, unless you''re targetting a 64 bit machine - it''s just warning you that this is a type whose size would change. It''s a portability warning, not a cross-compiling option.


“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 (C programming language co-inventor)
"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

Topic Locked

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

Sign in to reply to this topic.