首页 文章

动态分配和数组/指针的分段错误

提问于
浏览
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语句 . 看看程序是否走得那么远,但它从来没有 .

任何帮助是极大的赞赏 . 对不起,如果这是有史以来最基本的错误 .

3 回答

  • 0

    在int或string数组的位置使用std :: vector .

    说,

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

    这样你就可以避免所有的麻烦 . 这很简单而优雅 .

  • 2
    int **intPoint;
    

    在代码中的这一点上, intPoint 没有为它分配一个值 .

    *intPoint = new int[num];
    

    然后你取消引用它,但它没有指向任何东西 .

    尝试:

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

    现在,您要设置 intPoint 的值,使其指向您分配的整数 .

  • 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.
    

相关问题