2017-06-16 36 views
-4

Rao (2012, p. 180, listing 8.9)說:「您創建的10號線的副本的原因是,這樣的循環修改指針正在通過增量運算符(++)。通過新的需要返回原來的指針用來存放完好相應delete[]在第26行需要使用新返回的地址來調用,而不是任何隨機值「。當我修改,修改和刪除原始指針時,是否需要指針副本?

0: #include <iostream> 
1: using namespace std; 
2: 
3: int main() 
4: { 
5: cout << 「How many integers you wish to enter? 「; 
6: int InputNums = 0; 
7: cin >> InputNums; 
8: 
9: int* pNumbers = new int [InputNums]; // allocate requested integers 
10: int* pCopy = pNumbers; 
11: 
12: cout<<「Successfully allocated memory for 「<<InputNums<< 「 integers」<<endl; 
13: for(int Index = 0; Index < InputNums; ++Index) 
14: { 
15: cout << 「Enter number 「<< Index << 「: 「; 
16: cin >> *(pNumbers + Index); 
17: } 
18: 
19: cout << 「Displaying all numbers input: 「 << endl; 
20: for(int Index = 0, int* pCopy = pNumbers; Index < InputNums; ++Index) 
21: cout << *(pCopy++) << 「 「; 
22: 
23: cout << endl; 
24: 
25: // done with using the pointer? release memory 
26: delete[] pNumbers; 
27: 
28: return 0; 
29: } 

是在pCopy真的有必要嗎?我錯過了什麼? 在下面的修改示例中,我不使用它並且delete[]似乎正常工作。

#include <iostream>  
using namespace std; 

int main() 
{ 

    cout << "How many integers do you want? " << endl; 
    int InputN = 0; 
    cin >> InputN; 

    int* pNumbers = new int [InputN]; 

    cout << "allocated memory for " << InputN << " integers" << endl; 

    for (int Idx = 0; Idx < InputN; Idx++) { 
     cout << "enter number for index " << Idx << endl; 
     cin >> *(pNumbers + Idx); 
    } 

    cout << "Display all input numbers: " << endl; 


    for (int Idx = 0; Idx < InputN + 2; ++Idx) { 
     cout << "integer with index: " << Idx << " has value " << *(pNumbers + Idx) << " and pointer: " << pNumbers + Idx << endl; 
     //cout << pNumbers - 50000 << " points to " << *(pNumbers - 50000) << endl; 
     } 

    delete[] pNumbers; 

    cout << "after the delete: " << endl; 
    for (int Idx = 0; Idx < InputN + 2; ++Idx) { 
     cout << "integer with index: " << Idx << " has value " << *(pNumbers + Idx) << " and pointer: " << pNumbers + Idx << endl; 
     //cout << pNumbers - 50000 << " points to " << *(pNumbers - 50000) << endl; 
     } 

    return 0; 
} 
+1

代碼牆請發佈[MCVE] –

+2

是的。由於報價中給出的原因。 – juanchopanza

+2

您的版本不會修改'pNumbers',因此它不需要副本。 –

回答

3

在以下情況

{ 
    type* ptr = new type[size];  // line A 
    /* 
    some code, for example 
    ptr++;      // legal but dangerous/bad 
    */ 
    delete[] ptr;     // line B 
} 

可變ptr的值,即指向的地址,必須在線路AB相同。否則,這是未定義的行爲,並導致崩潰(如果你幸運的話)。在你的第二個代碼清單中,你不使用指針變量來遍歷元素,而是使用索引。所以,原始指針從未改變,並且您滿足條件。

確保滿足此要求有幾種方法。

  1. 您可以聲明指針爲const

    { 
        type * const ptr = new type[size]; // not: type const*ptr 
        // ptr++;       // illegal: would give a compiler error 
        delete[] ptr; 
    } 
    
  2. 您可以使用smart pointer

    { 
        std::unique_ptr<type[]> ptr = new type[size]; 
        /* some code */ 
    } // memory is automatically freed at end of scope of ptr 
    
  3. 可以使用的容器,如std::vector

    { 
        std::vector<type> vec(size); 
        /* some code */ 
    } // memory is automatically freed at end of scope of ptr 
    

的最後一個選項是最方便的,因爲它還允許您更改分配的內存量,或添加元素beyon d最初分配的內存(當矢量自動重新分配和擴展時)。