免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 17752 | 回复: 5
打印 上一主题 下一主题

u32 __u32 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-19 20:59 |只看该作者 |倒序浏览
看到内核中有的 .h 文件中有下面的定义

  1. typedef unsigned int __u32;
  2. typedef unsigned int u32;
复制代码

我不明白
1、程序为什么有的用 u32,而有的却用 __u32 呢?
2、为什么不直接 unsigned int xxx 或者 unsigned char xxx,而用 u32 u8 定义变量呢?

[ 本帖最后由 platinum 于 2005-12-20 11:13 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2005-12-19 22:40 |只看该作者
引自LDD 第10章

Assigning an Explicit Size to Data Items

Sometimes kernel code requires data items of a specific size, either to match predefined binary structures[39] or to align data within structures by inserting "filler'' fields (but please refer to "Data Alignment" later in this chapter for information about alignment issues).

    [39]This happens when reading partition tables, when executing a binary file, or when decoding a network packet.

The kernel offers the following data types to use whenever you need to know the size of your data. All the types are declared in <asm/types.h>, which in turn is included by <linux/types.h>:

    u8;   /* unsigned byte (8 bits) */
    u16;  /* unsigned word (16 bits) */
    u32;  /* unsigned 32-bit value */
    u64;  /* unsigned 64-bit value */

These data types are accessible only from kernel code (i.e., _ _KERNEL_ _ must be defined before including <linux/types.h>). The corresponding signed types exist, but are rarely needed; just replace u with s in the name if you need them.

If a user-space program needs to use these types, it can prefix the names with a double underscore: _ _u8 and the other types are defined independent of _ _KERNEL_ _. If, for example, a driver needs to exchange binary structures with a program running in user space by means of ioctl, the header files should declare 32-bit fields in the structures as _ _u32.


It's important to remember that these types are Linux specific, and using them hinders porting software to other Unix flavors. Systems with recent compilers will support the C99-standard types, such as uint8_t and uint32_t; when possible, those types should be used in favor of the Linux-specific variety. If your code must work with 2.0 kernels, however, use of these types will not be possible (since only older compilers work with 2.0).


基本就是说为了一些需要精确位数的数据类型独立于平台(包括硬件,系统和编译器)。


  1. #if PLATFORM_INT_SIZE!=32
  2.     #if PLATFORM_LONG_SIZE==32
  3.         typedef long u32;
  4.     #elif PLATFORM_LONG_LONG_SIZE==32
  5.          typedef long long u32;
  6.     #endif
  7. #else
  8.    typedef int u32;
  9. #endif

  10.    int a=sizeof(u32);
复制代码


这样可以保证a的值肯定是4...

[ 本帖最后由 zalem 于 2005-12-19 22:41 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2005-12-20 08:54 |只看该作者
谢谢 zalem,分析得很透彻,我终于明白原来上大学时老师说的“一个 WORD 一般情况下是 16 bit,至少在 PC 机中是这样”的意思了

那么,为什么不用 unsigned int 而用 u16/u32 定义呢?是不是因为网络编程的环境比较特殊,必须严格按多少 bit 算?
还有,为什么有的程序里面用 u32,有的是 __u32 呢?二者难道不一样吗?为何要单独分开呢?

论坛徽章:
0
4 [报告]
发表于 2005-12-20 09:27 |只看该作者
u32给用户用的,__u32在内核用的,不知道对不对?

论坛徽章:
0
5 [报告]
发表于 2005-12-20 10:45 |只看该作者
原帖由 圆点坐标 于 2005-12-20 09:27 发表
u32给用户用的,__u32在内核用的,不知道对不对?

恰恰相反,你仔细看看zalem的回贴。

论坛徽章:
0
6 [报告]
发表于 2005-12-20 11:13 |只看该作者
我明白了,谢谢大家 ^_^
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP