2015-10-26 25 views
0

我讀buf0buf.cc代碼的MySQL的InnoDB緩衝源代碼在這裏:'&buf_pool-> watch [0]'的語法含義是什麼?

link from git hub

而且我得到了這一點:

&buf_pool->watch[0]

什麼是語句的值?地址?或另一個值? ?

是什麼代碼的意思(語法意義)

+0

什麼*你*想表達呢?爲什麼你這麼想? –

+0

@JoachimPileborg,語法層面的代碼的含義對我來說已經足夠了。 – Al2O3

回答

5

由於operator precedence,這個表達式進行分析,如:

&((buf_pool->watch)[0]) 

在英語中,值是watch的第一個元素的地址成員容器buf_pool

+0

不應該是'&(buf_pool-> watch [0])'?解析'(buf_pool-> watch)[0]'將有下標作用於'struct buf_pool_t *'類型,而在'buf_pool-> watch [0]'中'它應該對'watch'類型有影響。 –

+0

'buf_pool-> watch'將返回一個緩衝區。因此,在返回後索引它是可以的。這個「翻譯」是有效的。 – erip

2

你可以找到。

首先,我們來看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 
} 
0

&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 = %X\n", ptr_hello); 
    printf("&ptr_hello = %X\n", &ptr_hello); 
    printf("&ptr_hello->before = %X\n", &ptr_hello->before); 
    printf("&ptr_hello->array[0] = %X\n", &ptr_hello->array[0]); 

    printf(""); 

    return 0; 
} 

https://ideone.com/fwDnoz

相關問題