C语言中的'objects'是什么?
What are 'objects' in C language?
在 Jens Gustedt 的《现代 C》一书中,作者做出了以下陈述:
In the following subsections, we will introduce the syntactical aspects (grammar) and three different semantic aspects: declarative parts (what things are), definitions of objects (where things are), and statements (what things are supposed to do).
The declarations of i and A declare variables , which are named items that allow us to store values. They are best visualized as a kind of box that may contain a “something” of a particular type:
Conceptually, it is important to distinguish the box itself (the object), the specification (its type), the box contents (its value), and the name or label that is written on the box (the identifier). In such diagrams, we put ?? if we don’t know the actual value of an item.
Generally, declarations only specify the kind of object an identifier refers to, not what the concrete value of an identifier is, nor where the object it refers to can be found. This important role is filled by a definition.
关于上述陈述的问题:
作者的第一个和第三个陈述暗示定义指定了可以找到标识符引用的对象的位置。 'where' 在这里是什么意思,特别是如何在定义中指定标识符的具体值 same/different 从标识符引用的对象所在的位置?
从陈述 2 中,'Conceptually, it is important to distinguish the box itself (the object), the specification (its type), the box contents (its value), and the name or label that is written on the box (the identifier).' 是什么意思?为什么 'box' 是一个对象而不是 'box' 中的值?
对象是可以存储值的地方。这是该语言使用的术语。
形式上,一个对象是一个
region of data storage in the execution environment, the contents of which can represent values
https://port70.net/~nsz/c/c11/n1570.html#3.15p1
本书作者使用了“盒子”这个词,这在非正式讨论中是完全合适的。
Q.1:第一点的作者将定义和声明称为相似的东西,因为这两个概念紧密联系在一起(但我认为有区别他会在后面的章节中解释清楚,他只是确保你已经开始了,暂时没有深入了解更多细节)。
深入浅出'where'指记忆
如果你继续学习 C,你会发现 'Storage classes' 的概念,其中变量的声明将告诉编译器将值存储在内存位置(物理上)。可以是RAM(Random Access Memory),也可以是Register(处理器的硬件存储单元)。
int a; // declaration(only specify what type of value it can store but does not specifies actual value)
int a = 123; //both declaration & defination (specifies the type & actual value i,e 123)
当执行 int a = 123
时,编译器将在 RAM 中分配 4 个字节的内存(假设您的 OS/compiler 是 64 位)。因为你的房子有一个地址,每个内存块也有一个地址,让我们假设分配内存的起始字节地址是 101,因此它需要 101...104.
Q.2:分配内存的四个字节,其内部值为123,广义上称为对象。你有一所房子,有 4 个房间和一个地址。但不要忘记,您也可以将您的房子命名为 'dexter's home'。所以这里 'a'
是 name/label 你可以用来在编写 C 代码时方便地引用内存的特定位置。
注意,作者将这四个字节框全部视为一个框,double
实际上需要八个字节(在 64 位上),但作者为了简单画了一个框。
区分很重要
- 盒子本身(对象)
- 规范(其类型)
- 盒子里的东西(它的价值)
- 以及写在盒子上的名称或标签(标识符)
那么地址呢?是不是少了一行like
- 盒子的位置(它的地址)
我没有看到调用变量对象有多大优势,而调用对象框则更少。
Gustedt给出了一些很好的解释和例子,但是他不是很系统。
定义和地址:
我猜他是这么想的:
int *p = malloc(...);
此处定义决定了“数据存储区域”。
首先,如果你是一个完全的初学者,你不需要担心所有这些肮脏的细节——简单地说,把一个对象想象成一个变量。初学者看这里就可以了,后面的回答都是进阶话题
C (C17 3.15) 中 object 的正式定义:
object
region of data storage in the execution environment, the contents of which can represent values
What does 'where' mean here
这意味着根据定义,一个对象被分配了一个内存位置 - 至少是一个相对位置,稍后由链接器转换为绝对地址。
in particular how is specifying a concrete value of an identifier in definition same/different from where the object the identifier refers to is?
From statement 2, what does 'Conceptually, it is important to distinguish the box itself (the object), the specification (its type), the box contents (its value), and the name or label that is written on the box (the identifier).' mean? Why's the 'box' an object here and not the value in the 'box'?
要一次回答所有这些问题,有几种方法可以在 C 中创建对象。最明显的方法是 int i=0;
,其中对象同时获得类型、值和标识符.但在动态分配的情况下,情况有所不同。
取int* p = malloc(sizeof *p)
.
malloc
returns 指向未命名对象的指针。对象的名称不是 p
,因为那是一个 指针的名称(它本身也是一个对象)——对象本身没有相应的标识符。
- 按照 C 的类型规则,返回的地址没有声明的类型,所以对象还没有类型。
- 此时对象的值也是不确定的。
在这种情况下,在我们 *p = 1;
之前,对象既没有类型也没有值。之后,对象的类型为int
,值为1
,因为我们在进行写访问时使用了int
。这个动态分配的例子是一个有点高级的主题的一部分,这个主题被称为 有效类型,与指针别名有关。 Gustedt 在撰写本章时很可能已经想到了该特定规则。
在 Jens Gustedt 的《现代 C》一书中,作者做出了以下陈述:
In the following subsections, we will introduce the syntactical aspects (grammar) and three different semantic aspects: declarative parts (what things are), definitions of objects (where things are), and statements (what things are supposed to do).
The declarations of i and A declare variables , which are named items that allow us to store values. They are best visualized as a kind of box that may contain a “something” of a particular type:
Conceptually, it is important to distinguish the box itself (the object), the specification (its type), the box contents (its value), and the name or label that is written on the box (the identifier). In such diagrams, we put ?? if we don’t know the actual value of an item.
Generally, declarations only specify the kind of object an identifier refers to, not what the concrete value of an identifier is, nor where the object it refers to can be found. This important role is filled by a definition.
关于上述陈述的问题:
作者的第一个和第三个陈述暗示定义指定了可以找到标识符引用的对象的位置。 'where' 在这里是什么意思,特别是如何在定义中指定标识符的具体值 same/different 从标识符引用的对象所在的位置?
从陈述 2 中,'Conceptually, it is important to distinguish the box itself (the object), the specification (its type), the box contents (its value), and the name or label that is written on the box (the identifier).' 是什么意思?为什么 'box' 是一个对象而不是 'box' 中的值?
对象是可以存储值的地方。这是该语言使用的术语。
形式上,一个对象是一个
region of data storage in the execution environment, the contents of which can represent values
https://port70.net/~nsz/c/c11/n1570.html#3.15p1
本书作者使用了“盒子”这个词,这在非正式讨论中是完全合适的。
Q.1:第一点的作者将定义和声明称为相似的东西,因为这两个概念紧密联系在一起(但我认为有区别他会在后面的章节中解释清楚,他只是确保你已经开始了,暂时没有深入了解更多细节)。
深入浅出'where'指记忆
如果你继续学习 C,你会发现 'Storage classes' 的概念,其中变量的声明将告诉编译器将值存储在内存位置(物理上)。可以是RAM(Random Access Memory),也可以是Register(处理器的硬件存储单元)。
int a; // declaration(only specify what type of value it can store but does not specifies actual value)
int a = 123; //both declaration & defination (specifies the type & actual value i,e 123)
当执行 int a = 123
时,编译器将在 RAM 中分配 4 个字节的内存(假设您的 OS/compiler 是 64 位)。因为你的房子有一个地址,每个内存块也有一个地址,让我们假设分配内存的起始字节地址是 101,因此它需要 101...104.
Q.2:分配内存的四个字节,其内部值为123,广义上称为对象。你有一所房子,有 4 个房间和一个地址。但不要忘记,您也可以将您的房子命名为 'dexter's home'。所以这里 'a'
是 name/label 你可以用来在编写 C 代码时方便地引用内存的特定位置。
注意,作者将这四个字节框全部视为一个框,double
实际上需要八个字节(在 64 位上),但作者为了简单画了一个框。
区分很重要
- 盒子本身(对象)
- 规范(其类型)
- 盒子里的东西(它的价值)
- 以及写在盒子上的名称或标签(标识符)
那么地址呢?是不是少了一行like
- 盒子的位置(它的地址)
我没有看到调用变量对象有多大优势,而调用对象框则更少。
Gustedt给出了一些很好的解释和例子,但是他不是很系统。
定义和地址:
我猜他是这么想的:
int *p = malloc(...);
此处定义决定了“数据存储区域”。
首先,如果你是一个完全的初学者,你不需要担心所有这些肮脏的细节——简单地说,把一个对象想象成一个变量。初学者看这里就可以了,后面的回答都是进阶话题
C (C17 3.15) 中 object 的正式定义:
object
region of data storage in the execution environment, the contents of which can represent values
What does 'where' mean here
这意味着根据定义,一个对象被分配了一个内存位置 - 至少是一个相对位置,稍后由链接器转换为绝对地址。
in particular how is specifying a concrete value of an identifier in definition same/different from where the object the identifier refers to is?
From statement 2, what does 'Conceptually, it is important to distinguish the box itself (the object), the specification (its type), the box contents (its value), and the name or label that is written on the box (the identifier).' mean? Why's the 'box' an object here and not the value in the 'box'?
要一次回答所有这些问题,有几种方法可以在 C 中创建对象。最明显的方法是 int i=0;
,其中对象同时获得类型、值和标识符.但在动态分配的情况下,情况有所不同。
取int* p = malloc(sizeof *p)
.
malloc
returns 指向未命名对象的指针。对象的名称不是p
,因为那是一个 指针的名称(它本身也是一个对象)——对象本身没有相应的标识符。- 按照 C 的类型规则,返回的地址没有声明的类型,所以对象还没有类型。
- 此时对象的值也是不确定的。
在这种情况下,在我们 *p = 1;
之前,对象既没有类型也没有值。之后,对象的类型为int
,值为1
,因为我们在进行写访问时使用了int
。这个动态分配的例子是一个有点高级的主题的一部分,这个主题被称为 有效类型,与指针别名有关。 Gustedt 在撰写本章时很可能已经想到了该特定规则。