如何动态分配涉及指针的结构
How to dynamically allocate a structure that involves a pointer?
我正在使用包含三个字段的学生结构。然后,我主要声明一个指向学生结构的指针。然后我把这个指针传递给一个函数。此函数计算文件中有多少行,然后动态分配 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++方式,所以我强烈推荐它!
- 如何在 C# 中映射双 C 结构指针?
- 指针或结构的矢量
- MSVC将仅移动结构参数解释为指针
- 如何访问指针结构的成员变量?
- 如何将变量从变量传递到指针结构对象
- 如何在函数中检索指针结构
- C :变量的点地址至指针结构成员的地址
- C++指针结构不会被删除
- 初始化指针结构 - 内存中的外观
- C++指针结构和多线程
- c++中指针结构的运算符重载
- 为什么我不能转发声明指针结构?
- C# 如何定义新的指针结构
- 如何处理大型指针结构
- 我怎么能写一个函数的定义在模板类中声明返回指针结构对象
- 我如何编程指针结构或类变量在c++
- 具有指针结构的类所需的析构函数
- 为什么在非指针结构上进行类型转换会出现语法错误
- 语法:做binary_search (STL)排序向量的指针结构
- /a.检测到Glibc.out: munmap_chunk():无效指针- c++结构体的动态数组