免费注册 查看新帖 |

Chinaunix

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

[C] 指针与地址 (关于标准ISO/IEC 9899:1999(E)的一些问题) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-29 15:10 |只看该作者 |倒序浏览
10可用积分
恳请大家赐教

问题1: 标准6.2.5
— Apointer type may be derived from a function type, an object type, or an incomplete
type, called the referenced type. A pointer type describes an object whose value
provides a reference to an entity of the referenced type. A pointer type derived from
the referenced type T is sometimes called ‘‘pointer to T’’. The construction of a
pointer type from a referenced type is called ‘‘pointer type derivation’’.
These methods of constructing derived types can be applied recursively.

请问 A pointer type describes an object  中的object指什么,很明显此处的object和前一个“an object type”中的object 不同。


问题2:
标准6.5.3.2
The operand of the unary * operator shall have pointer type.

标准注释
79) If &E is a valid pointer expression (where & is the ‘‘address-of ’’ operator, which generates a pointer to
its operand), the expression (&E)->MOS is the same as E.MOS.

按照上面的规则,如果有 int a; 则&a是一个指针。请问 &a 如何对应问题一中的object?


问题三: 请问C标准中的address 是什么,是不是指针?

先说一下我个人的结论,从标准来看,很多情况下address就是指针,很多情况address不是指针,具体依赖于context。所以说地址就是指针,指针就是地址不能说错。此外,我们知道,指向函数的指针的值有时候不是一个纯粹的地址那么简单,所以C标准中的address有时候含义更宽泛(即便只从值,而不包括类型的角度讨论)。


首先,如果address指的是机器模型中的memory address,显然memory address是无类型的,所以address不是指针。



C标准没有定义给出address的定义。 标准中最关键的段落应该是
6.5.3.2 Address and indirection operators
Semantics
3 The unary & operator returns the address of its operand. If the operand has type ‘‘type’’, the result has type ‘‘pointer to type’’.

此处的address似乎强调的是值,后面的if句才强调类型。


下面继续列出几个我找到的。

(1)标准
3.2
1 alignment
requirement that objects of a particular type be located on storage boundaries with
addresses that are particular multiples of a byte address

此处的address明显指memory address

(2)3.5
1 bit
unit of data storage in the execution environment large enough to hold an object that may
have one of two values
2 NOTE It need not be possible to express the address of each individual bit of an object.

我认为此处的address 也是指memory address。

(3)3.6
1 byte
addressable unit of data storage large enough to hold any member of the basic character
set of the execution environment

addressable 使用的是机器寻址概念。

此外,标准中还出现了addressing 、addressed
、address space等词,这些address应该都是无类型的。


(4)
6.2.4 Storage durations of objects
2 The lifetime of an object is the portion of program execution during which storage is
guaranteed to be reserved for it. An object exists, has a constant address,25) and retains
its last-stored value throughout its lifetime.26) If an object is referred to outside of its
lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when
the object it points to reaches the end of its lifetime.

25) The term ‘‘constant address’’ means that two pointers to the object constructed at possibly different
times will compare equal. The address may be different during two different executions of the same
program.

此处的address 指什么不好下结论。


(5)6.5.2.5
10 EXAMPLE 2 In contrast, in
void f(void)
{
int *p;
/*...*/
p = (int [2]){*p};
/*...*/
}
p is assigned the address of the first element of an array of two ints,

此处的address似乎又有值也又类型


(6)标准注释
83) Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an
address inappropriately aligned for the type of object pointed to, and the address of an object after the
end of its lifetime.

很明显此处的address就应该是pointer

(7)标准6.6
An address constant is a null pointer, a pointer to an lvalue designating an object of static
storage duration, or a pointer to a function designator; it shall be created explicitly using
the unary & operator or an integer constant cast to pointer type, or implicitly by the use of
an expression of array or function type. The array-subscript [] and member-access .
and -> operators, the address & and indirection * unary operators, and pointer casts may
be used in the creation of an address constant, but the value of an object shall not be
accessed by use of these operators.

很明显 address constant 是pointer,但是address constant是一个专门的词汇。能用address constant说明address吗?

(8)6.7.2.1
Within a structure object, the non-bit-field members and the units in which bit-fields
reside have addresses that increase in the order in which they are declared.
此处的address强调的是值

(9)7.19.3

6 The address of the FILE object used to control a stream may be significant; a copy of a
FILE object need not serve in place of the original.

此处的address明显就是pointer

(10)7.20.3 Memory management functions
1 The order and contiguity of storage allocated by successive calls to the calloc,
malloc, and realloc functions is unspecified. The pointer returned if the allocation
succeeds is suitably aligned so that it may be assigned to a pointer to any type of object
and then used to access such an object or an array of such objects in the space allocated
(until the space is explicitly deallocated). The lifetime of an allocated object extends
from the allocation until the deallocation. Each such allocation shall yield a pointer to an
object disjoint from any other object. The pointer returned points to the start (lowest byte
address)

此处的address明显没有类型,只有值。

(11) 7.24.6.4.1
Ifdst is not a null pointer, the pointer object pointed to by src is assigned either a null
pointer (if conversion stopped due to reaching a terminating null character) or the address
just past the last multibyte character converted (if any).
此处的address明显是pointer


还有一些没有列出来,但应该包括了大部分情况。

最佳答案

查看完整内容

>> 请问 A pointer type describes an object 中的object指什么,很明显此处的object和前一个“an object type”中的object 不同。“Object”(对象)是 C 标准中的一个基本的专用术语。与“Object Oriented”(面向对象)中“Object”的含义不一样,在 C 中它指的是一块数据存储区,其定义如下:region of data storage in the execution environment, the contents of which can represent values在中文教材中一般都回避使用“ ...

论坛徽章:
0
2 [报告]
发表于 2008-05-29 15:10 |只看该作者
>> 请问 A pointer type describes an object  中的object指什么,很明显此处的object和前一个“an object type”中的object 不同。

“Object”(对象)是 C 标准中的一个基本的专用术语。与“Object Oriented”(面向对象)中“Object”的含义不一样,在 C 中它指的是一块数据存储区,其定义如下:

region of data storage in the execution environment, the contents of which can represent values

在中文教材中一般都回避使用“对象”这个概念,而是近似地用“变量”来代替它。其实变量只是有名字的对象;还有一类对象是无名的,只能通过指针来访问。

另外,函数虽然也占用一定的存储区,但函数不是对象。

>> 按照上面的规则,如果有 int a; 则&a是一个指针。请问 &a 如何对应问题一中的object?

&a 是一个指针类型的表达式,而不是一个对象。显然,A pointer type describes an object … 一句的描述有其偏颇之处。以前注意到过这个问题,并就此在comp.std.c 上回过一贴:
http://groups.google.co.th/group ... ad/97a395ffd8a246d0 [/url]

>> 请问C标准中的address 是什么,是不是指针?

C 中取址运算的结果是“地址”,所以地址是指针(值),是指针类型,但“地址”本身不是 C 的一种数据类型。
指针值又可细分为指向类型和大小。如果不考虑或者不关心其指向类型,那么 C 中的地址反映了一种对内存地址的映射关系。

>> 很明显 address constant 是pointer,但是address constant是一个专门的词汇。能用address constant说明address吗?

准确地说,是除了空指针(null pointer)之外的地址常量(address constant)才都是指针类型的;空指针的类型取决于具体的实现。

地址常量是地址或指针值的一类特殊存在。当一个指针值满足了地址常量的条件时,由此而名。

>> 1. A pointer type describes an object  中的object指“指针变量的值”

In C concept, value is not object and object is not value. An object can have a value. “A value can come from not only an object, but also a function, a constant or eventually an expression.”

>> 2. int a; 则&a是一个指针,是一个地址,是一个地址常量

对“常量” 概念的理解有误,或者至少说对“地址常量”的理解有误——在尚未弄清楚 a 是一个静态对象与否的情况下,不能断定 &a 是不是一个地址常量。

类似的误解还经常发生在对数组名的理解上。
http://bbs.chinaunix.net/viewthr ... p;page=2#pid5802277[/url]

>> 只告诉你数字1,它既可能是int也可能是long,还可能是unsigned int

在 C 语言中,常量 1 的类型只能是 int。若论数学意义上的 1,在 C 中确实可以有多种类型的值与之对应。

论坛徽章:
0
3 [报告]
发表于 2008-05-29 15:27 |只看该作者

回复 #1 baozhao 的帖子

1. A pointer type describes an object  中的object指“指针变量的值”
2. int a; 则&a是一个指针,是一个地址,是一个地址常量
上面1中的object指“指针变量的值”也是一个地址
3. 关于指针,每个人都有自己的见解,我认为
指针是一种类型,因此,它有自己的sizeof,有自己的运算方式
指针变量可以存放address,指针常量可以表示address
只告诉你数字1,它既可能是int也可能是long,还可能是unsigned int
虽然值相同,但是类型不同,会影响这个“值”的语义和运算方式
同样,address是一个值,但是(char*), (int*)对它的解释却不相同

论坛徽章:
0
4 [报告]
发表于 2008-05-29 15:36 |只看该作者

回复 #2 ypxing 的帖子

A pointer type describes an object  中的object指“指针变量的值”

请问,如何解释下面的whose? value应该是object的属性
A pointer type describes an object whose value
provides a reference

论坛徽章:
1
黑曼巴
日期:2020-02-27 22:54:26
5 [报告]
发表于 2008-05-29 15:46 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
6 [报告]
发表于 2008-05-29 15:47 |只看该作者

回复 #3 baozhao 的帖子

晕,LZ真仔细,举个例子吧
int *pi;
A pointer Type指int *
object 指pi
whose指pi
pi的内容指whose value,即pi的值

论坛徽章:
0
7 [报告]
发表于 2008-05-29 15:49 |只看该作者

回复 #4 c/unix 的帖子

请注意,指针不一定只描述变量。如&a 就是一个指针,请看我的第二个问题

论坛徽章:
0
8 [报告]
发表于 2008-05-29 15:55 |只看该作者

回复 #5 ypxing 的帖子

实际上我知道你的意思,但我必须确认这一点,那么请继续看第二个问题。
   &a对应的object在哪里?
   
   &a只是一个表达式,没有对应的object,它只有value.
   这就是我的困惑。

论坛徽章:
0
9 [报告]
发表于 2008-05-29 15:59 |只看该作者
作为一种类型,有变量,也有常量
int a;
&a可以看作int *类型的一个常量
就像给你一个int类型的常量5一样

原帖由 baozhao 于 2008-5-29 15:55 发表
实际上我知道你的意思,但我必须确认这一点,那么请继续看第二个问题。
   &a对应的object在哪里?
   
   &a只是一个表达式,没有对应的object,它只有value.
   这就是我的困惑。

论坛徽章:
0
10 [报告]
发表于 2008-05-29 16:06 |只看该作者
原帖由 ypxing 于 2008-5-29 15:59 发表
作为一种类型,有变量,也有常量
int a;
&a可以看作int *类型的一个常量
就像给你一个int类型的常量5一样



把&a是否是常量的问题先搁在一边。

5是整数,也有值,但5不是object。 (我所知道的object都是需要空间的,而不是类似汇编中的立即数直接嵌入指令中)

所以,整个问题的关键是object该做何解?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP