The size_t and ssize_t of UNIX programming

Source: Internet
Author: User

http://blog.csdn.net/lalor/article/details/7426184

First of all, I'm pretty sure and sure to tell you that ssize_t is a signed integer, equivalent to int on a 32-bit machine, equivalent to a long int on a 64-bit machine, is there any difference between it and the Long data type? is actually the same. size_t is an unsigned ssize_t, that is unsigned long/unsigned int (under 32 bits), different compilers or systems may differ, mainly because int and long are the same on 32-bit machines. In 64 bit no test, but see Baidu Encyclopedia, size_t in 64 bit is 64 bit, then the correct definition of size_t should be typedef unsigned long size_t.


ssize_t:


A while ago, on the UNIX program design, asked the teacher said ssize_t exactly what data type, the teacher said, you directly with the good, really want to know what data type, can go to types.h to see, go back after looking for a bit did not find on the give up, today write code and encountered this problem, I think it is not the way, or have to figure out what is the type. Of course, for ssize_t exactly what data type, do not know the problem is not big, but for the control of strong and curious people, always want to know, and I believe, know that there will always be good, write code when the heart bottom.

Find steps:

1. This is done when the header file is included: #include <sys/types.h> Execute locate types.h jump out a bunch of files, which is why I first tried to fail. First execute the Locate sys/types.h, you can find the Types.h storage location.

2. Open Types.h, search ssize_t, get the following results

#ifndef __ssize_t_defined
typedef __ssize_t SSIZE_T;
# define __ssize_t_defined

3. Search __ssize_t can not find any valuable information, observe the file found that the file also contains other header files, that is #include <bits/types.h> open the bits/types.h again.

4. Search __ssize_t found such a statement: __std_type __ssize_t_type __ssize_t; Search __std_type_ssize again found such a sentence:

/* We want __extension__ before typedef ' s that use nonstandard base types
such as ' long ' in C89 mode. */
# define __std_type __extension__ typedef

That is, __std_type __ssize_t_type __ssize_t; This statement is actually a typedef __ssize_t_type __ssize_t, so the point is to find __ssize_t_type, search __size _t_type, there is no valuable information, similar to step 3, we found this statement: #include <bits/typesizes.h>, open typesizes.h file, search __ssize_t_type, Very easy to find a # define __ssize_t_type __sword_type, that is to say the first __std_type __ssize_t_type __ssize_t; This statement is in fact typedef __sword_type __SSIZE_T,TYPESIZE.H file can not find __sword_type, so we returned to the bit/types.h, the result finally appeared.

5. The following line of statements is easily found in bit/types.h.

#if __wordsize = = 32
# define __squad_type __quad_t
# define __uquad_type __u_quad_t
# define __sword_type int
# define __uword_type unsigned int
# define __slong32_type Long int
# define __ulong32_type unsigned long int
# define __s64_type __quad_t
# define __u64_type __u_quad_t
/* We want __extension__ before typedef ' s that use nonstandard base types
such as ' long ' in C89 mode. */
# define __std_type __extension__ typedef
#elif __wordsize = = 64
# define __squad_type Long int
# define __uquad_type unsigned long int
# define __sword_type Long int
# define __uword_type unsigned long int
# define __slong32_type int
# define __ulong32_type unsigned int
# define __s64_type Long int
# define __u64_type unsigned long int
/* No need to mark the typedef with __EXTENSION__. */
# define __STD_TYPE typedef
#else
# error
#endif

We can almost guess what __wordsize means, it's defined in # include <bits/wordsize.h>, which means that the computer system is a few. From the above we know that in 32-bit computer systems, ssize_t is an int, accounting for 4 bytes, in 64-bit computer systems, ssize_t is a long int, accounting for 8 bytes.


size_t:


Halo, size_t Information incredibly find a half-day. size_t We all know, is unsigned integer, under/usr/include/i386-linux-gnu/sys/stddef.h see so few statements:

/* __size_t is a typedef in FreeBSD 5!, must not trash it. */
#else
#define __size_t
#endif
#ifndef __size_type__
#define __SIZE_TYPE__ Long unsigned int
#endif
#if! (Defined (__gnug__) && defined (size_t))
typedef __size_type__ SIZE_T;
#ifdef __beos__
typedef long ssize_t;

To be sure, size_t is an unsigned integer, as long, or int, possibly different compilers have different definitions, I do not have a 64-bit machine here, can not be verified. This also verifies that the ssize_t we have mentioned above is actually a long.

The following is a little additional information that proves that I have found the right one. :-)

A basic, unsigned integer of the C + + type. It is the result type returned by the sizeof operator. The size of the type is selected, so that it can be stored in the maximum size of any type of array that is theoretically possible. On 32-bit systems for size_t will take 32-bit and 64-bit-64-bit.

Reference: http://baike.baidu.com/view/3236587.htm

Data type:ssize_tThis data type was used to represent the sizes of blocks so can be read or written in a single operation. It is similar to size_t, but must be a signed type.Reference: Http://www.delorie.com/gnu/docs/glibc/libc_239.html

The size_t and ssize_t of UNIX programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.