我有一个带有下面原型的函数,第三个参数(输出变量)是唯一相关的:
int ioman_write_pages(void**, unsigned, HDD_address**, /*rest is irrelevant*/)
HDD_address 类型以防有人问:
typedef union {
unsigned long long address;
struct {
unsigned long long offset : 12;
unsigned long long page_number : 26;
unsigned long long : 0;
} paddress;
} HDD_address;
具有堆分配的调用场景(完美运行):
HDD_address* page = (HDD_address*)malloc( pagecount * sizeof(HDD_address) );
memset( page, 0, pagecount * sizeof(HDD_address) );
ioman_write_pages( data, 3, &page, block, NULL, NULL );
printf( "Pages written to: [%d, %d, %d]\n",
page[0].address >> 9,
page[1].address >> 9,
page[2].address >> 9 );
输出(预期:0、16、24):
Pages written to: [0, 16, 24]
上面的堆分配没有问题。
但是,如果我尝试“页面”的堆栈分配:
HDD_address page[pagecount];
memset( page, 0, pagecount * sizeof(HDD_address) );
ioman_write_pages( data, 3, &page, block, NULL, NULL );
输出,虽然在数据方面毫无意义,但它是一致的:
Pages written to: [166956, 0, 0]
编译上述内容时的编译警告:
note: expected ‘union HDD_address **’ but argument is of type ‘union HDD_address (*)[(sizetype)(pagecount)]’
堆栈分配,“修复”警告:
DD_address page[pagecount];
memset( page, 0, pagecount * sizeof(HDD_address) );
HDD_address* ppage = page;
int r = ioman_write_pages( data, 3, &ppage, block, NULL, NULL );
输出,每次运行都保持一致,但胡说八道:
Pages written to: [1610376, 2856870, 610607]
我完全无法理解这一点,请高人赐教!