intptr_t 其实不是指针类型
来源:互联网 发布:江苏快三遗漏数据查询 编辑:程序博客网 时间:2024/04/21 00:34
http://blog.csdn.net/moonvs2010/article/details/7490420
最近在看nginx源码,看到有一个类型intptr_t,没有见过,google了一下,有人说是指针类型,但是看nginx源码中对该类型变量的使用,好像不是指针类型。
- static ngx_int_t
- 667 ngx_get_options(int argc, char *const *argv)
- 668 {
- 669 u_char *p;
- 670 ngx_int_t i;
- 671
- 672 for (i = 1; i < argc; i++) {
- 673
- 674 p = (u_char *) argv[i];
- 675
- 676 if (*p++ != '-') {
- 677 ngx_log_stderr(0, "invalid option: \"%s\"", argv[i]);
- 678 return NGX_ERROR;
- 679 }
- 680
- 681 while (*p) {
- 682
- 683 switch (*p++) {
- 684
- 685 case '?':
- 686 case 'h':
- 687 ngx_show_version = 1;
- 688 ngx_show_help = 1;
- 689 break;
- static ngx_int_t
- 667 ngx_get_options(int argc, char *const *argv)
- 668 {
- 669 u_char *p;
- 670 ngx_int_t i;
- 671
- 672 for (i = 1; i < argc; i++) {
- 673
- 674 p = (u_char *) argv[i];
- 675
- 676 if (*p++ != '-') {
- 677 ngx_log_stderr(0, "invalid option: \"%s\"", argv[i]);
- 678 return NGX_ERROR;
- 679 }
- 680
- 681 while (*p) {
- 682
- 683 switch (*p++) {
- 684
- 685 case '?':
- 686 case 'h':
- 687 ngx_show_version = 1;
- 688 ngx_show_help = 1;
- 689 break;
- 78 typedef intptr_t ngx_int_t;
- 78 typedef intptr_t ngx_int_t;
于是在linux的头文件中查找这个类型的定义,在/usr/include/stdint.h这个头文件中找到了这个类型的定义(不知道怎么在这里插入图片,所以使用文字):
- 117 /* Types for `void *' pointers. */
- 118 #if __WORDSIZE == 64
- 119 # ifndef __intptr_t_defined
- 120 typedef long int intptr_t;
- 121 # define __intptr_t_defined
- 122 # endif
- 123 typedef unsigned long int uintptr_t;
- 124 #else
- 125 # ifndef __intptr_t_defined
- 126 typedef int intptr_t;
- 127 # define __intptr_t_defined
- 128 # endif
- 129 typedef unsigned int uintptr_t;
- 130 #endif
- 117 /* Types for `void *' pointers. */
- 118 #if __WORDSIZE == 64
- 119 # ifndef __intptr_t_defined
- 120 typedef long int intptr_t;
- 121 # define __intptr_t_defined
- 122 # endif
- 123 typedef unsigned long int uintptr_t;
- 124 #else
- 125 # ifndef __intptr_t_defined
- 126 typedef int intptr_t;
- 127 # define __intptr_t_defined
- 128 # endif
- 129 typedef unsigned int uintptr_t;
- 130 #endif
很明显intptr_t不是指针类型,但是上边的一句注释(/* Types for `void *' pointers. */)让人很疑惑。既然不是指针类型,但是为什么说类型是为了“void *”指针?
又查了一下在《深入分析Linux内核源码》中找到了答案,原文描述如下:
尽管在混合不同数据类型时你必须小心, 有时有很好的理由这样做. 一种情况是因为内存存取, 与内核相关时是特殊的. 概念上, 尽管地址是指针, 内存管理常常使用一个无符号的整数类型更好地完成; 内核对待物理内存如同一个大数组, 并且内存地址只是一个数组索引. 进一步地, 一个指针容易解引用; 当直接处理内存存取时, 你几乎从不想以这种方式解引用. 使用一个整数类型避免了这种解引用, 因此避免了 bug. 因此, 内核中通常的内存地址常常是 unsigned long, 利用了指针和长整型一直是相同大小的这个事实, 至少在 Linux 目前支持的所有平台上.
因为其所值的原因, C99 标准定义了 intptr_t 和 uintptr_t 类型给一个可以持有一个指针值的整型变量. 但是, 这些类型几乎没在 2.6 内核中使用
- intptr_t 其实不是指针类型
- intptr_t 其实不是指针类型 .
- intptr_t 其实不是指针类型
- intptr_t 其实不是指针类型
- intptr_t 其实不是指针类型
- intptr_t 其实不是指针类型
- intptr_t 其实不是指针类型 .
- intptr_t 其实不是指针类型 .
- linux 内核 intptr_t 其实不是指针类型 .
- C语言指针转换为intptr_t类型
- C语言指针转换为intptr_t类型
- C语言指针转换为intptr_t类型
- C语言指针转换为intptr_t类型
- C语言指针转换为intptr_t类型
- C语言指针转换为intptr_t类型
- C语言指针转换为intptr_t类型
- intptr_t类型
- intptr_t 类型
- new发生异常的返回值
- 陆海空三军奥运安保部署到位
- 清华谈加60分录取蒋方舟(图)
- ln命令举例详解
- 大学男生浴室里的那些尴尬事
- intptr_t 其实不是指针类型
- 上半年河北城镇居民人均可支配收入6684.9元
- 河北强化奥运安保:20日起进京道路24小时检查
- 河北省公安厅出台奥运安保严肃责任追究八项规定
- 什么样的非处女坚决不能要
- Classy JavaScript with dojo/_base/declare
- 当心真爱掉入“伪爱”漩涡
- tcpdump详解
- 测试:恋爱段数你练到几级?