"&buf_pool->watch[0]"的语法含义是什么?
What's the grammar meaning of '&buf_pool->watch[0]'?
我在这里阅读mysql的innodb缓冲区源代码的buf0buf.cc
代码:
链接到git hub
我得到了这个:
&buf_pool->watch[0]
语句的值是多少?地址吗?或者另一个值?
代码是什么意思?(语法意义)
由于运算符优先,该表达式解析为:
&( (buf_pool->watch)[0] )
为buf_pool
中watch
成员容器的第一个元素的地址。
你可以找到。
首先,让我们以buf_bool
变量为例,查找它的声明。正如您在上面看到的几行,它是一个函数参数:
const buf_pool_t* buf_pool
这意味着我们必须找到buf_pool_t
类型的定义。如果只是进行全文搜索,则不会显示类型定义。但是,搜索"mysql buf_pool_t"会找到http://www.iskm.org/mysql56/structbuf__pool__t.html,这反过来又告诉我们该类型是在一个名为buf0buf.h
的文件中定义的。它也包含在你链接到的源文件中:
#include "buf0buf.h"
它确实包含了我们正在寻找的定义,并且该定义包含一个名为watch
:
struct buf_pool_t{
(…)
buf_page_t* watch;
(…)
};
watch
是指向buf_page_t
的指针
那么如果我们回到你的问题中的语句:
&buf_pool->watch[0]
watch
被解释为指向buf_page_t
数组的第一个元素的指针,watch[0]
是第一个元素本身,寻址操作符返回指向第一个元素的指针。
所以整个语句读作:
指向buf_page_t
数组第一个元素的指针。
奇怪的是,&buf_pool->watch[0]
等于buf_pool->watch
。下面是一个简单的(c++ 11)玩具程序来验证所有这些:
#include <iostream>
#include <typeinfo>
using buf_page_t = int;
struct buf_pool_t {
buf_page_t* watch;
};
int main()
{
const buf_pool_t example = { new buf_page_t[1] };
const buf_pool_t* buf_pool = &example;
std::cout << typeid(&buf_pool->watch[0]).name() << "n";
std::cout << typeid(buf_pool->watch).name() << "n";
std::cout << (&buf_pool->watch[0] == buf_pool->watch) << "n"; // prints 1
}
&buf_pool->watch[0]
是buf_bool
结构体中watch
的成员0的地址。也就是watch
本身。这样解析是因为整个buf_pool->watch[0]
都在&(地址)符号
你可以用下面的代码片段检查:
#include <iostream>
#include <stdio.h>
using namespace std;
struct hello_t
{
int before;
int array[5];
};
int main() {
// your code goes here
struct hello_t hello;
hello.array[0] = 100;
struct hello_t* ptr_hello;
ptr_hello = &hello;
printf("ptr_hello = %Xn", ptr_hello);
printf("&ptr_hello = %Xn", &ptr_hello);
printf("&ptr_hello->before = %Xn", &ptr_hello->before);
printf("&ptr_hello->array[0] = %Xn", &ptr_hello->array[0]);
printf("");
return 0;
}
https://ideone.com/fwDnoz - C++避免重复声明的语法是什么
- 使用基类指针调用基类的值构造函数的语法是什么?
- 这行/语法是什么意思?
- 初始化对象以在 C++08 中作为参数传递的首选语法是什么?
- 将引用绑定到指针的语法是什么?(各种)
- 这个typedef和转换运算符语法是什么意思
- 此语法是什么意思.(::*).
- 将显式指定的函数模板重载作为模板参数传递的正确语法是什么?
- 使用 pybind11 绑定 typedef 类型的正确语法是什么?
- 将此类传递给引用的正确语法是什么?
- 带有约束的可变参数模板的'requires'表达式的语法是什么?
- 在TensorFlow C++中用于feed_dict的语法是什么?
- 调用模板化类内的枚举类枚举器的正确语法是什么
- 返回没有 typedef 的成员函数指针的语法是什么?
- 将强制转换运算符重载到 std::map 的正确语法是什么
- 这种增量语法是什么意思
- 返回结构的方法的正确语法是什么
- C 中的(*)语法是什么意思
- 堆栈(int = 10),这个语法是什么意思(C++)
- variadic模板阵列参考参数的语法是什么?