2013-09-27 61 views
0

我正在介紹一個分配,它是介紹內存和指針的動態分配的原則。過去我做了一個簡單的程序,接受5個名字和5個分數,然後使用一個選擇排序來降序排列。現在我的任務是回到同一個程序並詢問用戶他們想輸入多少分數,然後使用指針動態分配必要的內存量。這是我第一次使用指針和這些概念,所以我仍然試圖把它全部弄清楚。動態分配和數組/指針的段錯誤

我的代碼編譯,但我儘快得到一個分段錯誤,因爲我進入了多少成績我想輸入(這是程序要求的第一件事)

我確信任何整數在我如何調用和聲明函數的過程中存在一些錯誤,因此如果我只是拼命改變任何東西,請讓我知道,但現在我不明白爲什麼我的程序崩潰的地方崩潰了。 這裏是我的代碼

#include <iostream> 
using namespace std; 

void initializeData(string *names[], int *scores[], int num); 
void displayData(string *names[], int *scores[], int num); 
void sortData(string *names[], int *scores[], int num); 

int main() 
{ 
int num; 
int **intPoint; 
string **strPoint; 

cout << "How many scores would you like to enter?: "; 
cin >> num; 

cout << " core dumped? "; 

*intPoint = new int[num]; 
*strPoint = new string[num]; 

initializeData(strPoint,intPoint,num); 
sortData(strPoint,intPoint,num); 
displayData(strPoint,intPoint,num); 

return 0; 
} 

void initializeData(string *names[], int *scores[], int num) 
{ 
     for(int i=0;i<num;i++) 
     { 
       cout << "Please input the name for score: " << i+1 << ": " << endl; 
       cin >> *(names[i]); 
       cout << "Please input the score for player: " << i+1 << ": " << endl; 
       cin >> *(scores[i]); 
     } 
} 

void sortData(string *names[], int *scores[], int num) 
{ 
int minIndex,minValue,x; 
string stringTemp; 

      for(int i = 0;i<(num-1);i++) 
      { 
       minIndex = i; 
       minValue = *(scores[i]); 

       for(x= i+1;x<num;x++) 
       { 
         if(*(scores[x]) > minValue) 
         { 
          minValue = *(scores[x]); 
          minIndex = x; 
         } 
       } 

       *(scores[minIndex])=*(scores[i]); 
       *(scores[i]) = minValue; 

       stringTemp = *(names[minIndex]); 
       *(names[minIndex]) = *(names[i]); 
       *(names[i]) = stringTemp; 
     } 
} 

void displayData(string *names[], int *scores[], int num) 
{ 
cout << "Top scorers: " << endl; 
     for(int i=0;i<num;i++) 
     { 
       cout << names[i] <<": "; 
       cout << scores[i] << endl; 
     } 
} 

和我的電流輸出: 你會多少分數想進入?:10 分割故障(核心轉儲)

這恰好不論什麼詮釋我放在那裏。我在 cin < < num之後放了一條cout語句;看看該節目是否有這麼遠但它從來沒有。

任何幫助,非常感謝。對不起,如果這是有史以來最基本的錯誤。

回答

2
int **intPoint; 

在你的代碼這一點,intPoint因爲你還沒有賦予它的值不指向任何東西。

*intPoint = new int[num]; 

然後你解除引用它,但它不指向任何東西。

嘗試:

int *intPoint; 

intPoint = new int[num]; 

現在要設置intPoint,使其指向你分配的整數。

+0

謝謝。我試圖使用指針不太指向的值,對吧?我不太瞭解雙指針,所以我真的沒有使用它們的業務,但不知何故,我怎麼得到的東西編譯。一旦我改變了,我可以回去改變我的所有函數,讓它們的參數中的數組指向指向數組的指針。這聽起來正確嗎? –

+1

我意識到這是遲到了,但因爲intPoint *是指針,所以說「你將intPoint的*值*設置爲一個指向整數的指針......」是不準確的。它實際上應該說「intPoint的*值*到新地址數組的地址*」 – ray

+0

@ray謝謝。固定。 –

0

您得到分段錯誤的原因是因爲您取消引用了未初始化的指針。

int **intPoint; // intPoint is declared a pointer to a 'pointer to an int'; 
       // but currently it points to nothing 
*intPoint = new int[num]; // *intPoint "dereferences" intPoint, i.e., assigns w/e it 
          // pointed to (which is nothing) to a pointer. 

像其他人一樣,你不需要雙指針。

int *intPoint; // intPoint is a pointer to an int 
intPoint = new int[num]; // notice how we didn't dereference intPoint. 
         // all we did was assign to our newly minted memory. 
+0

我在閱讀了很多關於如何在嘗試此操作之前不要這樣做,並且無論如何都不要這樣做。當它對我來說這樣拼寫出來對我來說更有意義。謝謝。 –

0

在int或string的數組中使用std :: vector。

說,

std::vector<int> scores; 
    std::vector<string> names; 

這樣就可以避開所有的麻煩。這簡單而優雅。