2012-07-30 74 views
2

我有以下LLVM代码。奇怪的是StoreInst类型的si变量在if块之外的新指令被分配后立即变为null(0),而我已经在外部范围声明了它。这里发生了什么?指针立即变为NULL

 Value *OldVal = NULL; 
     StoreInst* si = NULL; 

     if (...) 
     { 
      if (...) 
      { 
       .... 

       if (...) 
       { 
        ... 
        StoreInst* si = new StoreInst(...); 
        errs() << "si = " << si << "\n"; // Get some address here 
       } 
       errs() << "-->SI = " << si << "\n"; // Here I get NULL, why? 
      } 
      ... 
     } 

我得到这样的输出,

si = 0x1822ba0 
-->SI = 0x0 

回答

9

StoreInst* si = new StoreInst(...); - 你隐藏在previos名si这里

当范围结束} - 你看到另一个指针的值

下面是你做的一个例子:

int val = 0; //first val 
{ 
    int val = 10; //other val (let's call it second) 
    cout << val; //second val 
} // second val is destroyed here 
cout << val; //first val 

我在为简单起见,本例中使用int。其实它可以是任何类型

+0

啊对,我没有看到。 – pythonic 2012-07-30 11:05:54