473,382 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

why define size_t ssize_t ?


Hi,

This's a way of defining size_t and ssize_t in Linux:

//"linux/types.h"
typedef __kernel_size_t size_t;
typedef __kernel_ssize_t ssize_t;

//"asm/posix_types.h"
typedef unsigned int __kernel_size_t;
typedef int __kernel_ssize_t;

It seems so tricky to me. What benefits do we get from such a tricky
typedef ? better name (size_t is shorter than unsigned int) ?

thanks.

--
William
Nov 14 '05 #1
7 52644
"William Xuuu" <wi**********@163.com> wrote in message
news:87************@163.com...
This's a way of defining size_t and ssize_t in Linux:

//"linux/types.h"
typedef __kernel_size_t size_t;
typedef __kernel_ssize_t ssize_t;

//"asm/posix_types.h"
typedef unsigned int __kernel_size_t;
typedef int __kernel_ssize_t;

It seems so tricky to me. What benefits do we get from such a tricky
typedef ? better name (size_t is shorter than unsigned int) ?


On some 64-bit platforms, 'int' and 'unsigned int' may be 32-bits,
while addresses and even memory sizes could require 64 bits
(e.g. for the size of an allocated memory block > 4GB).
On such platforms, size_t could be typedef'd to a different
type, such as 'unsigned long long'.

Note also that 'size_t' is a typedef required by the ISO C standard
(it must be available if <stddef.h> is included). However, 'ssize_t'
does not exist in the C standard -- the standard 'ptrdiff_t'
typedef is nearly equivalent.

hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Nov 14 '05 #2
William Xuuu wrote:

Hi,

This's a way of defining size_t and ssize_t in Linux:

//"linux/types.h"
typedef __kernel_size_t size_t;
typedef __kernel_ssize_t ssize_t;

//"asm/posix_types.h"
typedef unsigned int __kernel_size_t;
typedef int __kernel_ssize_t;

It seems so tricky to me. What benefits do we get from such a tricky
typedef ? better name (size_t is shorter than unsigned int) ?


On ssize_t I have no comment, since the C language's definition
does not incorporate such a type.

The size_t type is defined by ISO for use by functions such as
malloc and strlen (among others, of course), for representing
size information. It is required to be an unsigned integral
type, but need not be unsigned int. It could equally be
unsigned long or unsigned long long on some systems. It could
even (perhaps a touch pathologically) be unsigned char!

Consider the following code:

size_t len = strlen(s);

If we didn't have size_t, and wanted our code to run on the Zag,
Zeg, Zig, Zog, and Zug compilers, all of which have different
types to represent the size returned by strlen, we'd have to do
something like:

#if _ZAG
unsigned char len = strlen(s);
#elif _ZEG
unsigned short len = strlen(s);
#elif _ZIG
unsigned int len = strlen(s);
#elif _ZOG
unsigned long len = strlen(s);
#elif _ZUG
unsigned long long len = strlen(s);
#else
#error Unsupported platform.
#endif

The obvious step would be to add a typedef to handle this:

#if _ZAG
typedef unsigned char size_type;
#elif _ZEG
typedef unsigned short size_type;
#elif _ZIG
typedef unsigned int size_type;
#elif _ZOG
typedef unsigned long size_type;
#elif _ZUG
typedef unsigned long long size_type;
#else
#error Unsupported platform.
#endif

size_type len = strlen(s);

This is still a nuisance, although not such a bad nuisance, because
we can hide the cpp nonsense in a header.

If only we could persuade each implementor to include a standard
definition for strlen's return type, and put it in, say, stddef.h.
Then we wouldn't have to put all this nonsense in our own header,
and our code would be simpler as a result (and easier to port).

We're in luck. Fortunately, the C Standard actually /requires/
conforming implementations to do this; the name they settled on
was size_t.

I hope that answers your question.
Nov 14 '05 #3
William Xuuu wrote:
Hi,

This's a way of defining size_t and ssize_t in Linux:

//"linux/types.h"
typedef __kernel_size_t size_t;
typedef __kernel_ssize_t ssize_t;

//"asm/posix_types.h"
typedef unsigned int __kernel_size_t;
typedef int __kernel_ssize_t;

It seems so tricky to me. What benefits do we get from such a tricky
typedef ? better name (size_t is shorter than unsigned int) ?

thanks.


Remember to use `size_t` as the type for indexing variables
used to index into C-strings. For example,

#include <stdio.h>

int
main ()
{
size_t index;
char * foo = "Example string.";

for (index = 0; index < (size_t) 5; ++index)
(void) putchar (foo[ index ]);

return 0;
}

Just my two cents. :)

Regards,
Jonathan.

--
"If unsigned integers look like two's complement (signed) integers,
it's because two's complement integers look like unsigned integers."
-Peter Nilsson
Nov 14 '05 #4
Jonathan Burd wrote:

Remember to use `size_t` as the type for indexing variables
used to index into C-strings. For example,

#include <stdio.h>

int
main ()
{
size_t index;
char * foo = "Example string.";
char const *foo = "Example string.";

for (index = 0; index < (size_t) 5; ++index)
Obfuscatory cast. 5 will be converted to size_t anyway.
Admittedly there are inferior compilers that will
issue a warning for signed-unsigned comparison in the
case of (index < 5).

(void) putchar (foo[ index ]);
useless cast. Sorry, but I disagree with the philosophy
of putting (void) before every unused return value. It
adds volumes of useless crap to your source code and
achieves almost nothing. If you want to search your code
for failure to check malloc's return value then you can
grep for malloc, etc.

return 0;
}


Nov 14 '05 #5
infobahn <in******@btinternet.com> writes:
[...]
Consider the following code:

size_t len = strlen(s);

If we didn't have size_t, and wanted our code to run on the Zag,
Zeg, Zig, Zog, and Zug compilers, all of which have different
types to represent the size returned by strlen, we'd have to do
something like:

#if _ZAG
unsigned char len = strlen(s);
#elif _ZEG
unsigned short len = strlen(s);
#elif _ZIG
unsigned int len = strlen(s);
#elif _ZOG
unsigned long len = strlen(s);
#elif _ZUG
unsigned long long len = strlen(s);
#else
#error Unsupported platform.
#endif


Actually, if strlen() returned some unknown unsigned type, we could
just use:

unsigned long long len = strlen(s);

and let the result be converted implicitly. (It gets slightly more
complex if we need to worry about implementations that don't support
long long).

But of course size_t is still a useful thing to have.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
Old Wolf wrote:
Jonathan Burd wrote:
Remember to use `size_t` as the type for indexing variables
used to index into C-strings. For example,

#include <stdio.h>

int
main ()
{
size_t index;
char * foo = "Example string.";

char const *foo = "Example string.";

for (index = 0; index < (size_t) 5; ++index)

Obfuscatory cast. 5 will be converted to size_t anyway.
Admittedly there are inferior compilers that will
issue a warning for signed-unsigned comparison in the
case of (index < 5).
(void) putchar (foo[ index ]);

useless cast. Sorry, but I disagree with the philosophy
of putting (void) before every unused return value. It
adds volumes of useless crap to your source code and
achieves almost nothing. If you want to search your code
for failure to check malloc's return value then you can
grep for malloc, etc.

return 0;
}



Perhaps, splint should be a bit more forgiving in that case.

--
"If unsigned integers look like two's complement (signed) integers,
it's because two's complement integers look like unsigned integers."
-Peter Nilsson
Nov 14 '05 #7
infobahn <in******@btinternet.com> writes:
On ssize_t I have no comment, since the C language's definition
does not incorporate such a type.

The size_t type is defined by ISO for use by functions such as
malloc and strlen (among others, of course), for representing
Hmm, thanks.

So i.e, for portable reasons, some special functions concerning with
memeory or address manipulations need size_t.

[...] We're in luck. Fortunately, the C Standard actually /requires/
conforming implementations to do this; the name they settled on
was size_t.

I hope that answers your question.


--
William
Nov 14 '05 #8

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

Similar topics

2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
11
by: Roka100 | last post by:
Hi, I am using size_t and ssize_t . But I am confused about them. <ssize_t> typedef int __ssize_t; typedef __ssize_t ssize_t; <size_t > typedef unsigned int size_t;
26
by: robertwessel2 | last post by:
In another thread, a poster mentioned the Posix ssize_t definition (signed version of size_t). My initial reaction was to wonder what the point of the Posix definition was when ptrdiff_t was...
39
by: Mark Odell | last post by:
I've always declared variables used as indexes into arrays to be of type 'size_t'. I have had it brought to my attention, recently, that size_t is used to indicate "a count of bytes" and that using...
20
by: ramasubramanian.rahul | last post by:
hi folks i have a peculiar problem. i have to allocate more than size_t consequtive bytes on a system . after i do a malloc .. i am unable to do a realloc because it takes size_t as a new size...
318
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people...
73
by: Yevgen Muntyan | last post by:
Hey, I was reading C99 Rationale, and it has the following two QUIET CHANGE paragraphs: 6.5.3.4: "With the introduction of the long long and extended integer types, the sizeof operator may...
8
by: lubomir dobsik | last post by:
hi, i have seen an interesting thing: #if sizeof((char*)0 - (char*)0) == sizeof(unsigned int) typedef unsigned int size_t; #elif sizeof((char*)0 - (char*)0) == sizeof(unsigned long) typedef...
89
by: Tubular Technician | last post by:
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.