size_t or ssize_t

Discussion in 'C Programming' started by Roka100@gmail.com, Feb 16, 2006.

  1. Guest

    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.
    , Feb 16, 2006
    #1
    1. Advertising

  2. "" <> writes:
    > 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>

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Feb 16, 2006
    #2
    1. Advertising

  3. wrote:
    > 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.
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Feb 16, 2006
    #3
  4. Roka Guest

    Nils O. Selåsdal wrote:
    > wrote:
    > > 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.


    Yes, I see. I'm using a PC with Fedora Core 4.
    Thanks
    >
    > > 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.
    Roka, Feb 16, 2006
    #4
  5. "Roka" <> writes:
    > Nils O. Selåsdal wrote:
    >> wrote:
    >> > 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.

    >
    > Yes, I see. I'm using a PC with Fedora Core 4.
    > Thanks
    >>
    >> > I see the range of them are :
    >> > int ( ssize_t ) : -32767~32767
    >> > unsigned int ( size_t ) : 0~65535


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

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Feb 16, 2006
    #5
  6. Roka Guest

    Keith Thompson wrote:
    > "Roka" <> writes:
    > > Nils O. Selåsdal wrote:
    > >> wrote:
    > >> > 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.

    > >
    > > Yes, I see. I'm using a PC with Fedora Core 4.
    > > Thanks
    > >>
    > >> > I see the range of them are :
    > >> > int ( ssize_t ) : -32767~32767
    > >> > unsigned int ( size_t ) : 0~65535

    >
    > 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 ? )



    >
    > --
    > Keith Thompson (The_Other_Keith) <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.
    Roka, Feb 16, 2006
    #6
  7. "Roka" <> writes:
    > Keith Thompson wrote:
    >> "Roka" <> writes:
    >> > Nils O. Selåsdal wrote:
    >> >> wrote:
    >> >> > 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.
    >> >
    >> > Yes, I see. I'm using a PC with Fedora Core 4.
    >> > Thanks
    >> >>
    >> >> > I see the range of them are :
    >> >> > int ( ssize_t ) : -32767~32767
    >> >> > unsigned int ( size_t ) : 0~65535

    >>
    >> 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 ? )


    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.)

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Feb 16, 2006
    #7
  8. Roka Guest

    Keith Thompson wrote:
    > "Roka" <> writes:
    > > Keith Thompson wrote:
    > >> "Roka" <> writes:
    > >> > Nils O. Selåsdal wrote:
    > >> >> wrote:
    > >> >> > 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.
    > >> >
    > >> > Yes, I see. I'm using a PC with Fedora Core 4.
    > >> > Thanks
    > >> >>
    > >> >> > I see the range of them are :
    > >> >> > int ( ssize_t ) : -32767~32767
    > >> >> > unsigned int ( size_t ) : 0~65535
    > >>
    > >> 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 ? )

    >
    > 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.)
    >
    > --
    > Keith Thompson (The_Other_Keith) <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.


    Thank you very much.
    Roka, Feb 16, 2006
    #8
  9. "Roka" <> writes:
    > 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.

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Feb 16, 2006
    #9
  10. wrote:

    > 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.
    Martin Ambuhl, Feb 16, 2006
    #10
  11. On 15 Feb 2006 22:22:38 -0800, "" <>
    wrote:

    >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
    Barry Schwarz, Feb 17, 2006
    #11
  12. Barry Schwarz <> writes:
    > On 15 Feb 2006 22:22:38 -0800, "" <>
    > wrote:
    >> 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?


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

    --
    Keith Thompson (The_Other_Keith) <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.
    Keith Thompson, Feb 17, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Xuuu

    why define size_t ssize_t ?

    William Xuuu, Jan 17, 2005, in forum: C Programming
    Replies:
    7
    Views:
    1,430
    William Xuuu
    Jan 23, 2005
  2. Alex Vinokur
    Replies:
    9
    Views:
    784
    James Kanze
    Oct 13, 2008
  3. kid joe

    ssize_t and size_t

    kid joe, May 19, 2009, in forum: C Programming
    Replies:
    8
    Views:
    8,565
    chunpulee
    May 25, 2009
  4. Alex Vinokur
    Replies:
    1
    Views:
    574
  5. James Harris

    size_t, ssize_t and ptrdiff_t

    James Harris, Oct 12, 2013, in forum: C Programming
    Replies:
    56
    Views:
    668
    Albert van der Horst
    Oct 25, 2013
Loading...

Share This Page