1

I want to create a dynamic array of pointers that each one of them points to a struct. In the program there is an option to add structs and if the counter reaches the last the value of the array, the array expands.

struct student
{
    string id;
    string name;
};

int N=5;
int counter=0;
student **big=new student *[N]; //a ptr to an array of ptr's.

void add_student (int &counter,student **big)
{
    int i;

    if (counter==0)
    {
        for (i=0; i<N; i++)
        {
            big[i]=new student; 
        }
    }

    if (counter==N)
    {
        N+=5;
        student **temp=new student *[N];
        for (i=counter-1; i<N; i++)
        {
            temp[i]=new student;
        }

        for (i=0; i<counter; i++)
        {
            temp[i]=big[i];
        }

        delete [] big;
        big=temp;
    }

    cout<<"Enter student ID: "<<endl;
    cin>>(*big)[counter].id;

    cout<<"Enter student name: "<<endl;
    cin>>(*big)[counter].name;

    counter++;
}

When I run the program it crashes after I try to add more than one student. Thanks!

4

2 回答 2

0

我刚试过。该错误是因为您没有正确处理指向结构的指针。将指针传递给指向某事物的指针意味着该函数不仅可以更改它所指向的事物的指针地址。所以在函数中声明一个指向指针的指针是可以的,但是将全局 p 声明为 p 到 s 没有多大意义。使用 &ptr 可以达到同样的效果。for p to p to s 即把指针的地址传递给函数。我做了一些更改,但我不确定它是否有效。我将在 4/5 小时后重试,并详细检查问题。暂时请满足以下条件。(下面可能有一些错误,所以要小心)

    struct student
    {
      string id;
      string name;
    };

    int N=5;
    int counter=0;
    student *big=new student[N]; //a ptr to an array of ptr's.

    void add_student (int &counter,student **ppBig)
    {
     int i;

    if (counter==0)
    {
     for (i=0; i<N; i++)
        *ppBig[i]=new student; 
    }

if (counter==N)
{
    N+=5;
    student *temp=new student [N];
    for (i=counter-1; i<N; i++)
        temp[i]=new student;

    for (i=0; i<counter; i++)
        temp[i]=*ppBig[i];

    delete[] *ppBig;
    ppBig=temp;
}


cout<<"Enter student ID: "<<endl;
cin>>(*big)[counter].id;

cout<<"Enter student name: "<<endl;
cin>>(*big)[counter].name;

counter++;

}

于 2013-10-15T15:04:04.720 回答
0

试试这个代码。主要问题是您在(*big)[counter].id没有有效内存的情况下写入。在我下面的函数中,首先创建一个学生对象,然后写入。

PS:我没有测试过代码,如果有问题请告诉我。

struct student {
  string id;
  string name;
};

int N=5;
int counter=0;
student **big = new student *[N]; //a ptr to an array of ptr's.

// Variable big and counter is global, no need to pass as argument.
void add_student (student *new_student) {
    // Resize if needed
    if (counter==N) {
        int i;

        student **temp=new student *[N+5];

        // Copy from the old array to the new
        for (i=0; i<N; i++) {
            temp[i]=big[i];
        }

        // Increase maximum size
        N+=5;

        // Delete the old
        delete [] big;
        big=temp;
    }

    // Add the new student
    big[counter] = new_student; 

    counter++;
}

// Function called when we should read a student
void read_student() {
    student *new_student = new student;

    cout<<"Enter student ID: "<<endl;
    cin>>new_student->id;

    cout<<"Enter student name: "<<endl;
    cin>>new_student->name;

    // Call the add function
    add_student (new_student);
}
于 2013-10-15T15:02:12.543 回答