473,834 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's the deal with size_t?

Hello, World!

Reading this group for some time I came to the conclusion that
people here are split into several fractions regarding size_t,
including, but not limited to,

* size_t is the right thing to use for every var that holds the
number of or size in bytes of things.

* size_t should only be used when dealing with library functions.

* size_t should really be a signed type (less warnings)

* size_t is unnecessary (size of object in memory never exceeds
what can be held in an integer).

* size_t is visually unpleasant.

* size_t clutters up / is an uglification of the language
(solving only a theoretical problem).

* size_t usage may be non-portable because it won't be around
anymore in 100 years.

Sooo... what's the real deal with size_t? Where should it be
used/avoided (examples?)

Nov 6 '07 #1
89 5789
Tubular Technician said:
Sooo... what's the real deal with size_t? Where should it be
used/avoided (examples?)
Observe where, how, and why the standard library uses size_t. Then go thou
and do likewise. The standard library is very often (although by no means
always) a reasonable guide to good practice in your own code.

A great many standard library functions use size_t, not only to specify the
size of an object, but also to enumerate objects.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 6 '07 #2
Tubular Technician wrote:
Hello, World!

Reading this group for some time I came to the conclusion that
people here are split into several fractions regarding size_t,
including, but not limited to,

* size_t is the right thing to use for every var that holds the
number of or size in bytes of things.
as far as I'm aware, all of the c-library I/O functions (as well as many
that take "number of items" arguments) all use size_t, so it is best to
match types.
>
* size_t should only be used when dealing with library functions.
That's a lot of library functions ;-)
>
* size_t should really be a signed type (less warnings)
except that size_t is often used to hold values that may be larger than
a signed integer can hold.

there is an ssize_t which is signed
>
* size_t is unnecessary (size of object in memory never exceeds
what can be held in an integer).
size_t is often used to refer to sizes of objects on disk, which can
easily exceed what can be held in a signed integer.

Max size_t is 4GB on 32bit architectures, I'm not entirely sure if that
changes on 64bit architectures, but Linux file sizes can range up to at
least 4GB and I believe that Linux also supports at least 4GB of memory,
so using a signed integer is not sufficient if you want your software to
be portable.
>
* size_t is visually unpleasant.
that's in the eye of the beholder and isn't a technical reason to
use/not use size_t
>
* size_t clutters up / is an uglification of the language
(solving only a theoretical problem).
Again, not a very technical argument.
* size_t usage may be non-portable because it won't be around
anymore in 100 years.
I won't be around in 100 years, so I'm not terribly worried whether it
will or won't be around by then.

I suspect, however, that size_t will not be going away.
>
Sooo... what's the real deal with size_t? Where should it be
used/avoided (examples?)
It should be used whenever you make a call to a library functions which
uses it and, likely, whenever you want to express the size of an object
in bytes.

Jeff
Nov 6 '07 #3
Tubular Technician <no@spam.invali dwrites:
* size_t is the right thing to use for every var that holds the
number of or size in bytes of things.
Only the size of an object held in memory, or a number or size
that can be no greater than the maximum size of an object held in
memory. Thus, size_t is not appropriate for holding the size of
disk file because a disk file can be larger than memory (use
off_t instead).
--
Ben Pfaff
http://benpfaff.org
Nov 6 '07 #4
Tubular Technician <n...@spam.inva lidwrote:
Hello, World!

Reading this group for some time I came to the conclusion
that people here are split into several fractions regarding
size_t, including, but not limited to,

* size_t is the right thing to use for every var that holds
the number of or size in bytes of things.
For memory objects, it's hard to escape that fact.
* size_t should only be used when dealing with library
functions.
Sounds more like a phobia than sense.
* size_t should really be a signed type (less warnings)
How big is -1 bytes?

People get warnings for 2 reasons: bad code and mother hen
clucking compilers. Both are easy to rectify. The later
is easy to ignore.
* size_t is unnecessary (size of object in memory never
exceeds what can be held in an integer).
False for a number of 16-bit int systems that can address
more than 64K of memory. As 64-bit systems accessing more
than 4G of RAM become prevalent, you'll see more
implementations in the same boat because the many will
keep int at 32-bits for backwards compatibility with
programs written by programmers who failed to learn from
their mistakes when 32-bit systems took over 16-bit ones.
* size_t is visually unpleasant.
C is visually unpleasant.

[But size_t is a lot more appealing to me than FILE!]
* size_t clutters up / is an uglification of the language
(solving only a theoretical problem).
No, that's ptrdiff_t.
* size_t usage may be non-portable because it won't be
around anymore in 100 years.
If size_t won't be around, then it'll be because C won't
be around.
Sooo... what's the real deal with size_t? Where should
it be used/avoided (examples?)
Use it where you need to use it. Typical usage is as a size,
count or index of memory objects.

Fact is, it doesn't really matter whether you use size_t
or int. You still have to be conscious of memory limitations
and going beyond the limits of whatever integer you're using.

It's simpler to use size_t in most cases because detecting
wrap around is easy and, unlike int, it _is_ guaranteed to
be able to index any object allocated through normal means.

The only problem with size_t, AFAICS, is that it is not
required to have a rank of unsigned int or above.

--
Peter

Nov 6 '07 #5

"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:05******** *************** *******@bt.com. ..
Tubular Technician said:
>Sooo... what's the real deal with size_t? Where should it be
used/avoided (examples?)

Observe where, how, and why the standard library uses size_t. Then go thou
and do likewise. The standard library is very often (although by no means
always) a reasonable guide to good practice in your own code.
just, be careful of adopting their naming conventions. otherwise, you may
run into, clashes...

my naming conventions often go like this:
<lib>_<name smaller libs or externally usable names.
<part>_<name rarer, usually for older code or if I may at some
point split the code into another lib
<lib>_<part>_<n ame a general convention.

in the above, lib is usually all caps, part is is often mixed case
(FirstLettersAr eCaps), and name is often similar to part.

another convention used in some of my libs (for front-end API functions in
cases where I 'formally' specify the external API, rather than just making a
lib and just using whatever code the lib contains):
<prefix><name >

where prefix is all lower case (usually the lower-case equivalent of lib),
and name is as before (though, in a few cases, I have used all lower case
names, but this has mostly been for 'core' functions intended almost as
extensions or alternatives to the standard library).

usually, only a few front-end functions use these interfaces, with nearly
everything else (internal to the lib) using the previous conventions.
my rules are far more lax for front-end code, but usually this is only a
minor part of my projects.

A great many standard library functions use size_t, not only to specify
the
size of an object, but also to enumerate objects.
yes.

size_t is good, albeit in the past I have traditionally not used it much...

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Nov 6 '07 #6
Jeffrey Stedfast <st******@comca st.netwrote:
Tubular Technician wrote:
Reading this group for some time I came to the conclusion that
people here are split into several fractions regarding size_t,
including, but not limited to,
Hardly. There are most people, who vary from option one to option two,
and then there's Malcolm, who is scared of underlines.
* size_t should really be a signed type (less warnings)

except that size_t is often used to hold values that may be larger than
a signed integer can hold.

there is an ssize_t which is signed
Not in C, there isn't. Maybe in C++.
* size_t is unnecessary (size of object in memory never exceeds
what can be held in an integer).

size_t is often used to refer to sizes of objects on disk,
More likely in memory...
which can easily exceed what can be held in a signed integer.
....for which this is just as true. For the sizes of disk objects, size_t
may not even be enough.
Max size_t is 4GB on 32bit architectures,
Nonsense. size_t may be any unsigned integer type; what's there to stop
an implementation on a "32bit architecture", whatever that means in this
case (and it could mean several things), from making a size_t 64 bits?
After all, if it's a C99 implementation, it already _has_ to make long
long at least that large.
Sooo... what's the real deal with size_t? Where should it be
used/avoided (examples?)

It should be used whenever you make a call to a library functions which
uses it and, likely, whenever you want to express the size of an object
in bytes.
Quite. And it should be avoided if you're a scaredycat who doesn't want
to face up to any other integer types than int and char.

Richard
Nov 6 '07 #7
Richard Bos wrote:
Jeffrey Stedfast <st******@comca st.netwrote:
>>
there is an ssize_t which is signed

Not in C, there isn't. Maybe in C++.
I think it's Posix.

--
Ian Collins.
Nov 6 '07 #8
On Tuesday 06 Nov 2007 6:46 am Tubular Technician <no@spam.invali d>
wrote in article <lg************ @foo.example.co m>:
Hello, World!

Reading this group for some time I came to the conclusion that
people here are split into several fractions regarding size_t,
including, but not limited to,

* size_t is the right thing to use for every var that holds the
number of or size in bytes of things.
Yes.
* size_t should only be used when dealing with library functions.
Why? That's daft.
* size_t should really be a signed type (less warnings)
Not worth it.
* size_t is unnecessary (size of object in memory never exceeds
what can be held in an integer).
There is no such requirement. It is provably false.
* size_t is visually unpleasant.
Weakest of all arguments against it.
* size_t usage may be non-portable because it won't be around
anymore in 100 years.
Why is that?
Sooo... what's the real deal with size_t? Where should it be
used/avoided (examples?)
It's appropriate to hold the sizes of objects, arrays and often to hold
indexes as well. If you really need a signed value then it's, of
course, not appropriate. If you don't mind skirting portability there
is POSIX's ssize_t.

Nov 6 '07 #9
Richard Bos wrote:
Jeffrey Stedfast <st******@comca st.netwrote:
>Tubular Technician wrote:
>>Reading this group for some time I came to the conclusion that
people here are split into several fractions regarding size_t,
including, but not limited to,

Hardly. There are most people, who vary from option one to option two,
and then there's Malcolm, who is scared of underlines.
>>* size_t should really be a signed type (less warnings)
except that size_t is often used to hold values that may be larger than
a signed integer can hold.

there is an ssize_t which is signed

Not in C, there isn't. Maybe in C++.
Ah, this could be an GNUism (I wouldn't be surprised) :)
>
>>* size_t is unnecessary (size of object in memory never exceeds
what can be held in an integer).
size_t is often used to refer to sizes of objects on disk,

More likely in memory...
>which can easily exceed what can be held in a signed integer.

...for which this is just as true. For the sizes of disk objects, size_t
may not even be enough.
true enough, my mistake.
>
>Max size_t is 4GB on 32bit architectures,

Nonsense. size_t may be any unsigned integer type; what's there to stop
an implementation on a "32bit architecture", whatever that means in this
case (and it could mean several things), from making a size_t 64 bits?
sorry, I meant to say "at least", as in, "Max size_t is at least 4GB on
32bit" because it has to be able to at least hold values that large.
After all, if it's a C99 implementation, it already _has_ to make long
long at least that large.
Agreed.
>
>>Sooo... what's the real deal with size_t? Where should it be
used/avoided (examples?)
It should be used whenever you make a call to a library functions which
uses it and, likely, whenever you want to express the size of an object
in bytes.

Quite. And it should be avoided if you're a scaredycat who doesn't want
to face up to any other integer types than int and char.

Richard
Jeff
Nov 6 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

176
8348
by: basecamp | last post by:
just checking the average age of programmers using this group -- thanks
4
2252
by: Alfonzo Morra | last post by:
I've the ff code in cp assignmenent cstor: PB& PB::operator=( const PB& b) { if ( this != &b ) { PB *pb = new PB( b ) ; this = pb ; // <- Compiler barfs here } return *this ; }
4
1443
by: Ron Vecchi | last post by:
I recently picked up a Managed Direct 3d book and the examples are in c++. I've always used C# and wonder if c++ is more suited to Direct 3D programming. Of course I know they both access the same framework so I'm thinking their is no difference. But, it seems all examples either on the net or in books are always in c++, Is this because of tradition seeing that before c#, c++ was the better alternative? Or am I safe to just convert the...
8
2739
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block? (ie, forget the finally block and just end the try/catch and put the code after the try/catch block). Or does the "finally" construct add some additional functionality?
4
2010
by: David Lozzi | last post by:
OK simple question. Whats the default value for an string() array? sub LoadStuff(byval one as integer, byval two as string, optional byval three() as string = ??) Its driving me nuts! Thanks! --
8
1645
by: buc | last post by:
I have a simple combox on the screen that is bound via a datareader to a stored proc in sql that returns a simple string. The code is 'load stored proc then dReader = tmpSQL.SQLcmd.ExecuteReader() combobox1.DataSource = dReader dReader.Read() combobox1.DataTextField = dReader.GetName(0) combobox1.DataBind()
20
2480
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function doesn't write to x, then it doesnt seem like the compiler could care less whether I specify the const part. Quite the opposite, if one uses const liberally and then later goes back and changes the functions, headaches will inevitably occur as...
9
1554
by: Christopera | last post by:
I setup a site that uses a set width main body then installed some divs within the body. In Opera, IE7, and FF it all looks pretty similar, some small problems with IE7 but the site still looks good. In IE6 one of my right floated divs sort of slides to the right a bit further than in the rest of the browsers leaving a nice 5 px gap between the div and another object. Whats the deal?
3
390
by: Thomas 'PointedEars' Lahn | last post by:
Erwin Moller wrote: Just to add a bit more off-topic noise: The World Wide Web is not really a part of the Internet (interconnected networks); it is *an application of* the Internet. The Internet is the hardware, the Web is one kind of software for it; e-mail, for example, is another. Usenet (orig.: Unix User Network) is not a part of the Internet as
0
10790
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10505
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9329
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5624
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5790
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3079
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.