簡體   English   中英

動態分配和數組/指針的分段錯誤

[英]segmentation fault with dynamic allocation and arrays/pointers

我正在進行一項任務,介紹動態分配內存和指針的原理。 我過去制作了一個簡單的程序,它接受了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語句。 看程序是否達到了那么遠,但它從來沒有。

任何幫助是極大的贊賞。 對不起,如果這是有史以來最基本的錯誤。

int **intPoint;

在代碼中的這一點上, intPoint沒有指向任何內容,因為您沒有為其分配值。

*intPoint = new int[num];

然后你取消引用它,但它沒有指向任何東西。

嘗試:

int *intPoint;

intPoint = new int[num];

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

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

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.

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

說,

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM