size_t or ssize_t

R

Roka100

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;

That is:
ssize_t = int
size_t = unsigned int

I see the range of them are :
int ( ssize_t ) : -32767~32767
unsigned int ( size_t ) : 0~65535

My question is : Is that necessary to use ssize_t ? ( size can be minus
? )

Thanks.
 
K

Keith Thompson

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;

That is:
ssize_t = int
size_t = unsigned int

I see the range of them are :
int ( ssize_t ) : -32767~32767
unsigned int ( size_t ) : 0~65535

My question is : Is that necessary to use ssize_t ? ( size can be minus
? )

size_t is defined in <stddef.h>; it's an unsigned type, the result of
the sizeof operator. It won't necessarily be unsigned int (it could
be unsigned long, for example), and it will often have a range greater
than 0..65535.

ssize_t is not defined in standard C. It's a POSIX extension, so you
shouldn't use it if you care about absolute portability.

<OFF_TOPIC>
ssize_t, if it's defined, is the signed equivalent of size_t.
Presumably it should be used only if you need to store negative
values. The Open Group Base Specification says it's "Used for a count
of bytes or an error indication."
</OFF_TOPIC>
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Hi,
I am using size_t and ssize_t . But I am confused about them.
That is:
ssize_t = int
size_t = unsigned int
On your particular machine. Could be something different on
other machines.
I see the range of them are :
int ( ssize_t ) : -32767~32767
unsigned int ( size_t ) : 0~65535

My question is : Is that necessary to use ssize_t ? ( size can be minus
? )
necessary in what context ?

size_t comes from standard C,is an unsigned type used for sizes of objects.

ssize_t comes from posix, is a signed type used for a count of bytes or
an error indication.
 
K

Keith Thompson

Roka said:
Yes, I see. I'm using a PC with Fedora Core 4.
Thanks

Then the maximum value of size_t on your system is almost certainly
2147483647, not 65535.
 
R

Roka

Keith said:
Then the maximum value of size_t on your system is almost certainly
2147483647, not 65535.
Thanks.
But I checked /usr/lib/gcc/i386-redhat-linux/4.0.0/include/stddef.h
size_t is long unsigned int ( 32bit , 0~4294967295 )
and
ssize_t is long ( 32bit, -2147483647~2147483647)

Is that somewhere wrong? ( long unsigned int is NOT euqal to unsigned
long int ? )
 
K

Keith Thompson

Roka said:
Thanks.
But I checked /usr/lib/gcc/i386-redhat-linux/4.0.0/include/stddef.h
size_t is long unsigned int ( 32bit , 0~4294967295 )
and
ssize_t is long ( 32bit, -2147483647~2147483647)

Is that somewhere wrong? ( long unsigned int is NOT euqal to unsigned
long int ? )

Oops, my mistake. What I meant to say is that the maximum value of
size_t is 4294967295, not 65535.

(BTW, the minimum value of ssize_t on your system is -2147483648, not
-2147483647.)
 
R

Roka

Keith said:
Oops, my mistake. What I meant to say is that the maximum value of
size_t is 4294967295, not 65535.

(BTW, the minimum value of ssize_t on your system is -2147483648, not
-2147483647.)

Thank you very much.
 
K

Keith Thompson

Roka said:
Keith Thompson wrote: [...]
Oops, my mistake. What I meant to say is that the maximum value of
size_t is 4294967295, not 65535.

(BTW, the minimum value of ssize_t on your system is -2147483648, not
-2147483647.)
[...]

Thank you very much.

You're welcome.

When you post a followup, please delete any quoted text that isn't
relevant; just leave enough so your followup makes sense on its own.
In particular, don't quote a signature unless you're actually
commenting on it.
 
M

Martin Ambuhl

My question is : Is that necessary to use ssize_t ? ( size can be minus
? )


Since ssize_t is not a standard type in C (but size_t is), it would seem
that ssize_t is not necessary.
 
B

Barry Schwarz

Hi,
I am using size_t and ssize_t . But I am confused about them.

<ssize_t>
typedef int __ssize_t;

Names beginning with __ are reserved for the implementation.
typedef __ssize_t ssize_t;

Why the two step? typedef int ssize_t would work just fine.
<size_t >
typedef unsigned int size_t;

Why are you redefining something that is already defined in a standard
header?
That is:
ssize_t = int
size_t = unsigned int

I see the range of them are :
int ( ssize_t ) : -32767~32767
unsigned int ( size_t ) : 0~65535

Possibly true for your system but not a portable assumption.
My question is : Is that necessary to use ssize_t ? ( size can be minus
? )

If there were an object or type whose size is negative, how much
memory would it occupy? Can any object or type be smaller than char
whose size is guaranteed to by +1.


Remove del for email
 
K

Keith Thompson

Barry Schwarz said:
Names beginning with __ are reserved for the implementation.


Why the two step? typedef int ssize_t would work just fine.


Why are you redefining something that is already defined in a standard
header?

I think he was quoting from his system's headers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top