如何动态分配涉及指针的结构

How to dynamically allocate a structure that involves a pointer?

本文关键字:指针 结构 动态分配      更新时间:2023-10-16

我正在使用包含三个字段的学生结构。然后,我主要声明一个指向学生结构的指针。然后我把这个指针传递给一个函数。此函数计算文件中有多少行,然后动态分配 Student 数组,使其大小与文件中的行数相同,然后将数据读入数组。我坚持动态分配 Student 数组,因为函数参数涉及指向结构的指针。

因为函数参数是指向结构的指针,所以如果我执行pointer = new Student[record];,它会起作用吗?我不知道这是否是您动态分配 Student 数组的方式,其大小与文件中的行数相同。函数参数中的指针让我感到困惑。

struct Student
{
string name;
double gpa[NUM_OF_QUARTERS];
double average;
};
bool fillArr(Student* pointer);
int main()
{
Student* ptr;
if (!fillArr(ptr))
    return 1;
return 0;
}
bool fillArr(Student* pointer)
{
ifstream infile;
infile.open("student_records.txt");
if (!infile)
{
    cout << "Error opening filen";
    return false;
}   
int record = 0;
string str;
while(getline(infile, str))
    ++record;
cout << "number of lines in the file " << record << endl;
infile.clear();
infile.seekg(0, ios::beg);
pointer = new Student[record];  // Is this how you would dynamically allocate the Student array?
// after dynamically allocating Student array, read in the data from the file
}

你的方法会部分有效,但我宁愿为此使用std::vector<Student>。如果你想真的很花哨,你可以使用 std::vector::emplace_back() 函数来避免先构造一个Student,然后用 std::vector::push_back() 将其复制到std::vector中的开销。你可以在这里找到一个非常好的描述和示例,它使用类型 President 而不是 Student .

如果要在 main() 中使用新创建的数组,则应bool fillArr(Student *&pointer)签名。

如果将ptr变量"by-value"传递给fillArr(),则main()中的ptr将不会更改,因为pointer in fillArr() 包含函数调用时的 ptr 值。这发生在函数签名上

bool fillArr(Student *pointer)

如果您改为"按引用"传递它,则 fillArr() 中的pointer将引用您在 main() 中传递的变量。这发生在建议的函数签名中

bool fillArr(Student *&pointer)

您在此处使用的方法将无法正常工作,因为您正在按值传递指针。如果要继续此操作,请更改函数,使其通过引用获取指针:

bool fillArr(Student*& ptr) {
   ...
}

如果不这样做,则只会更改原始指针副本指向的位置,而不会更改原始指针本身指向的位置。

也就是说,我认为不使用std::vector代替动态分配的数组,使事情变得比需要的要困难得多。使用std::vector要容易得多:

std::vector<Student> fillArr() {
    ifstream infile("student_records.txt");
    if (!infile) {
      /* error handling */
    }
    std::vector<Student> result;
    for (string line; getline(infile, line); ) {
         Student curr = /* initialize curr based on line */
         result.push_back(curr);
    }
    return result;
}

这样可以避免原始指针,仅读取文件一次,不会冒提供的指针为 null 的风险,并且不需要显式内存管理。这是做事C++方式,所以我强烈推荐它!