2014-10-05 81 views
-4

我正在學習cpp(我希望我沒有嚇到你已經),我明白指針或引用是什麼,我想我得到什麼對指針的引用是。刪除一個指針引用使程序崩潰

我已經寫了崩潰代碼爲delete[] ranking;

open(file)只是一個簡單的函數打開包含字符串和3點的整數其進入zuzel結構每行一個txt文件:

void read_file(std::ifstream & file, zuzel *& ranking) 
{ 
    if(!open(file)) 
    return; //exit function if reading a file failed 

    int size = 1; 

    while(!file.eof()) 
    { 
     zuzel * update = new zuzel[size]; 

     if(ranking != NULL) memcpy(update, ranking, (size-1)*sizeof(*ranking)); //copy existing contents 

     file >> update[size - 1].nazwa >> update[size - 1].zawodnicy >> update[size - 1].mecze >> update[size - 1].punkty;// add a new team 

     delete[] ranking; //delete old data 
     std::cout << "tst"; //just to see if it crashes 
     ranking = update; 

     size++; 
    } 
} 

int main() 
{ 
    zuzel * rank; 
    std::ifstream file; 
    read_file(file, rank); 
    return 0; 
} 

我發現,你不應該刪除,你沒有新的東西,但例如代碼不會崩潰:

void funk(int *& a) 
{ 
    delete[] a; 
} 

int main() 
{ 
    int a[3] = {3, 4, 6}; 
    int * p = a; 
    funk(p); 
    return 0; 
} 

我該如何解決這個問題?爲了一些簡單的解釋,我很擔心它爲什麼會這樣做。

+3

「不會崩潰」並不意味着代碼是有效的。 – Mat 2014-10-05 11:53:16

+1

'我發現你不應該刪除一些你沒有新的東西,但是例如那個代碼不會崩潰'不能保證像這樣的錯誤的C++代碼會崩潰。 – PaulMcKenzie 2014-10-05 11:59:29

+1

'我正在學習cpp'然後我建議你學會使用'std :: vector'而不是編寫低級C來模擬一個動態數組。 – PaulMcKenzie 2014-10-05 12:01:58

回答

2

這將是足夠寫

zuzel * rank = 0; 

至於第二個代碼片段,它已不確定的行爲。

1
zuzel * rank; 
//somewhere in read_file: 
delete[] ranking; //ranking is a reference to pointer; 
       //in your case it's referernce to uninitialized pointer rank 

因爲rank沒有初始化,它指向的內存未知的部分,你的代碼只需tryes到delete[]反正部分...

你需要做的設定指針通過解決0使用下列操作之一:

zuzel * rank=nullptr; //in C++11 
zuzel * rank=NULL; //before C++11 
zuzel * rank=0; //also legal, but I not recommend this 

0爲任何指針的特殊價值,這意味着它指向什麼。

當您將指針設置爲0delete[]delete時,它們不會執行任何操作。

1

由於您正在學習。請接受這個例子,你可能更喜歡這樣做。你會發現它的異常安全和使用指針避免(並因此避免了需要存儲管理)

main.cpp中:

#include <iostream> 
#include <vector> 
#include <fstream> 
#include <string> 
#include <algorithm> 
#include <iterator> 

struct zuzel 
{ 
    std::string nazwa, zawodnicy, mecze, punky; 
}; 

std::ostream& operator<<(std::ostream& os, const zuzel& z) 
{ 
    return os << z.nazwa << ", " 
    << z.zawodnicy << ", " 
    << z.mecze << ", " 
    << z.punky; 
} 

std::vector<zuzel> read_zuzels(std::string filename) 
{ 
    std::vector<zuzel> results; 

    std::ifstream f(filename.c_str()); 
    f.exceptions(std::ios::badbit); 

    while(!f.eof()) 
    { 
     // add a new team 
     zuzel new_zuzel; 
     f >> new_zuzel.nazwa 
     >> new_zuzel.zawodnicy 
     >> new_zuzel.mecze 
     >> new_zuzel.punky; 

     results.push_back(new_zuzel); 
    } 
    return results; 
} 

using namespace std; 


int main() 
{ 
    vector<zuzel> zuzels = read_zuzels("input.txt"); 
    copy(zuzels.begin(), zuzels.end(), ostream_iterator<zuzel>(cout, "\n")); 
    return 0; 
} 

input.txt中:

foo1 bar1 baz1 bazzer1 
foo2 bar2 baz2 bazzer2 

輸出:

Compiling the source code.... 
$g++ main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1 

Executing the program.... 
$demo 
foo1, bar1, baz1, bazzer1 
foo2, bar2, baz2, bazzer2 
+1

謝謝,我將不得不閱讀向量 – krzym1 2014-10-05 12:45:15

+0

學習C++時,您可以做的最好的事情就是閱讀標準庫。下面是一個很好的開始:http://en.cppreference.com/w/ 祝你在C++旅程中 - 自從C++ 11以來,它絕對是這個星球上最有趣和最富有成效的語言(恕我直言)。 – 2014-10-05 13:10:45