intptr_t 其实不是指针类型

来源:互联网 发布:江苏快三遗漏数据查询 编辑:程序博客网 时间:2024/04/21 00:34

http://blog.csdn.net/moonvs2010/article/details/7490420

 

 

最近在看nginx源码,看到有一个类型intptr_t,没有见过,google了一下,有人说是指针类型,但是看nginx源码中对该类型变量的使用,好像不是指针类型。

[cpp] view plaincopy
  1. static ngx_int_t  
  2.  667 ngx_get_options(int argc, char *const *argv)  
  3.  668 {  
  4.  669     u_char     *p;  
  5.  670     ngx_int_t   i;  
  6.  671   
  7.  672     for (i = 1; i < argc; i++) {  
  8.  673   
  9.  674         p = (u_char *) argv[i];  
  10.  675   
  11.  676         if (*p++ != '-') {  
  12.  677             ngx_log_stderr(0, "invalid option: \"%s\"", argv[i]);  
  13.  678             return NGX_ERROR;  
  14.  679         }  
  15.  680   
  16.  681         while (*p) {  
  17.  682   
  18.  683             switch (*p++) {  
  19.  684   
  20.  685             case '?':  
  21.  686             case 'h':  
  22.  687                 ngx_show_version = 1;  
  23.  688                 ngx_show_help = 1;  
  24.  689                 break;  

[cpp] view plaincopy
  1. static ngx_int_t  
  2.  667 ngx_get_options(int argc, char *const *argv)  
  3.  668 {  
  4.  669     u_char     *p;  
  5.  670     ngx_int_t   i;  
  6.  671   
  7.  672     for (i = 1; i < argc; i++) {  
  8.  673   
  9.  674         p = (u_char *) argv[i];  
  10.  675   
  11.  676         if (*p++ != '-') {  
  12.  677             ngx_log_stderr(0, "invalid option: \"%s\"", argv[i]);  
  13.  678             return NGX_ERROR;  
  14.  679         }  
  15.  680   
  16.  681         while (*p) {  
  17.  682   
  18.  683             switch (*p++) {  
  19.  684   
  20.  685             case '?':  
  21.  686             case 'h':  
  22.  687                 ngx_show_version = 1;  
  23.  688                 ngx_show_help = 1;  
  24.  689                 break;  
其中ngx_int_t的定义如下:

[cpp] view plaincopy
  1. 78 typedef intptr_t        ngx_int_t;  

[cpp] view plaincopy
  1. 78 typedef intptr_t        ngx_int_t;  


于是在linux的头文件中查找这个类型的定义,在/usr/include/stdint.h这个头文件中找到了这个类型的定义(不知道怎么在这里插入图片,所以使用文字):

                           

[cpp] view plaincopy
  1. 117 /* Types for `void *' pointers.  */  
  2. 118 #if __WORDSIZE == 64  
  3. 119 # ifndef __intptr_t_defined  
  4. 120 typedef long int        intptr_t;  
  5. 121 #  define __intptr_t_defined  
  6. 122 # endif  
  7. 123 typedef unsigned long int   uintptr_t;  
  8. 124 #else  
  9. 125 # ifndef __intptr_t_defined  
  10. 126 typedef int         intptr_t;  
  11. 127 #  define __intptr_t_defined  
  12. 128 # endif  
  13. 129 typedef unsigned int        uintptr_t;  
  14. 130 #endif  

[cpp] view plaincopy
  1. 117 /* Types for `void *' pointers.  */  
  2. 118 #if __WORDSIZE == 64  
  3. 119 # ifndef __intptr_t_defined  
  4. 120 typedef long int        intptr_t;  
  5. 121 #  define __intptr_t_defined  
  6. 122 # endif  
  7. 123 typedef unsigned long int   uintptr_t;  
  8. 124 #else  
  9. 125 # ifndef __intptr_t_defined  
  10. 126 typedef int         intptr_t;  
  11. 127 #  define __intptr_t_defined  
  12. 128 # endif  
  13. 129 typedef unsigned int        uintptr_t;  
  14. 130 #endif  

很明显intptr_t不是指针类型,但是上边的一句注释(/* Types for `void *' pointers. */)让人很疑惑。既然不是指针类型,但是为什么说类型是为了“void *”指针?

又查了一下在《深入分析Linux内核源码》中找到了答案,原文描述如下

尽管在混合不同数据类型时你必须小心, 有时有很好的理由这样做. 一种情况是因为内存存取, 与内核相关时是特殊的. 概念上, 尽管地址是指针, 内存管理常常使用一个无符号的整数类型更好地完成; 内核对待物理内存如同一个大数组, 并且内存地址只是一个数组索引. 进一步地, 一个指针容易解引用; 当直接处理内存存取时, 你几乎从不想以这种方式解引用. 使用一个整数类型避免了这种解引用, 因此避免了 bug. 因此, 内核中通常的内存地址常常是 unsigned long, 利用了指针和长整型一直是相同大小的这个事实, 至少在 Linux 目前支持的所有平台上.

因为其所值的原因, C99 标准定义了 intptr_t 和 uintptr_t 类型给一个可以持有一个指针值的整型变量. 但是, 这些类型几乎没在 2.6 内核中使用

原创粉丝点击