我有一個有趣的行爲,我的程序。它更好地顯示代碼。使用鑄造結構的有趣的行爲
typedef struct PS {
int num;
} PR;
typedef struct PS* PN;
typedef struct {
int num;
int tmp;
} VD;
void F (PN VPtr)
{
register VD* qVPtr = (VD*)VPtr;
// if this is call #2
// qVPtr->tmp already is 8 for VP arg
// qVPtr->tmp already is 16 for VP1 arg
switch(VPtr->num){
case 0:
qVPtr->tmp = 8;
return;
case 1:
qVPtr->tmp = 16;
return;
}
}
int main()
{
PN VP = NULL;
VP = (PN)malloc(sizeof(PR));
VP->num = 0;
F (VP);
PN VP1 = NULL;
VP1 = (PN)malloc(sizeof(PR));
VP1->num = 1;
F (VP1);
F (VP); // call #2 with VP arg
F (VP1); // call #2 with VP1 arg
return 0;
}
在主要功能VP
和VP1
不知道qVPtr
和tmp
場,但根據當地F
功能VPtr
參數有可能獲得的qVPtr->tmp
最後一個值。
你能否詳細解釋這種可能性?