通过函数 C++ 创建结构体数组

发布于 2025-01-10 21:11:25 字数 1176 浏览 0 评论 0原文

我想创建一个创建结构数组的函数。我所做的如下:

struct Student {
    char studentNames[128]; 
    unsigned int FN; 
    short selectiveDisciplinesList[10]; 
    unsigned short countOfSelectiveDisciplines; 
    bool hasTakenExams;
};
    
void fillInStudentInfo(Student &student) {...}

Student createStudentsArray() {
    unsigned int countOfStudents;
    std::cout << "Enter the number of students You are going to write down: " << std::endl;
    std::cin >> countOfStudents;
    Student studentsArray[countOfStudents];

    for (int i = 0; i < countOfStudents; ++i) {
        fillInStudentInfo(studentsArray[i]);
    }

    return *studentsArray;
}

但是,在 main() 中,我分配:

Student studentArray = createStudentsArray();

我无法访问每个不同结构的成员 (fe studentArray[i].something)。

问题从何而来?

如果我这样做:

Student * createStudentsArray() {
    ...
    return studentsArray;
}

with

Student * studentArray = createStudentsArray();

My IDE 会警告我:

与返回的局部变量关联的堆栈内存地址

但我似乎能够访问每个结构的成员。这是一件坏事吗?

I want to create a function that creates an array of structures. What I have done is the following:

struct Student {
    char studentNames[128]; 
    unsigned int FN; 
    short selectiveDisciplinesList[10]; 
    unsigned short countOfSelectiveDisciplines; 
    bool hasTakenExams;
};
    
void fillInStudentInfo(Student &student) {...}

Student createStudentsArray() {
    unsigned int countOfStudents;
    std::cout << "Enter the number of students You are going to write down: " << std::endl;
    std::cin >> countOfStudents;
    Student studentsArray[countOfStudents];

    for (int i = 0; i < countOfStudents; ++i) {
        fillInStudentInfo(studentsArray[i]);
    }

    return *studentsArray;
}

However, when in main(), I assign:

Student studentArray = createStudentsArray();

I can not access the members of each different struct (f.e. studentArray[i].something).

Where does the issue come from?

If I do it like this:

Student * createStudentsArray() {
    ...
    return studentsArray;
}

with

Student * studentArray = createStudentsArray();

My IDE warns me:

Address of stack memory associated with local variable returned

But I do seem to be able to access the members of each struct. Is that a bad thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅黛梨妆こ 2025-01-17 21:11:25

你确实不能以这种方式做你想做的事:

Student createStudentsArray() {
    ...
    Student studentsArray[countOfStudents];
    ...
    return *studentsArray;
}

首先,这是使用可变长度数组,严格来说这是 无效的 C++< /a>.例如,我的编译器不允许这样做。

其次,它只会返回一名 Student。即使它返回了所有这些,它的效率也非常低,因为它必须复制它们,但它没有。

你试过这个:

Student *createStudentsArray() {
    ...
    Student studentsArray[countOfStudents];
    ...
    return studentsArray;
}

这更糟糕。一旦函数结束,分配给该数组的内存就会被释放。这就是编译器警告您的内容。之后事情似乎进展顺利,但很快就会发生非常糟糕的事情。

大多数经验丰富的 C++ 开发人员都会这样做:

std::vector<Student> createStudentsArray() {
    ...
    std::vector<Student> students(countOfStudents);
    ...
    return students;
}

如果您“不允许使用向量”(StackOverflow 总是看到这一点),那么请执行以下操作:

Student *createStudentsArray() {
    ...
    Student *studentsArray = new Student[countOfStudents];
    ...
    return studentsArray;
}

但是一旦完成,您必须delete[]该数组使用它。

脚注 - 如果您真的感兴趣,可以获取更多信息。

事实上,经验丰富的开发人员会这样做:

std::vector<std::shared_ptr<Student>> createStudentsArray() {
    ...
    std::vector<std::shared_ptr<Student> students(countOfStudents);
    for (int......)
    {
        students.push_back(std::make_shared<Student>());
        ...
    }
    return students;
}

现在,向量包含一堆智能指针而不是实际对象,这是制作大型对象数组(或其他容器)的最有效方法。 std::vectorstd::shared_ptr 将自动为您清理所有内存。

You really cannot do what you are trying to do in that way:

Student createStudentsArray() {
    ...
    Student studentsArray[countOfStudents];
    ...
    return *studentsArray;
}

Firstly, this is using a variable length array, which strictly speaking is invalid C++. My compiler, for example, doesn't allow it.

Second, it will only return one Student. Even if it returned them all, its very inefficient because it would have to copy them all, but it doesn't.

You tried this:

Student *createStudentsArray() {
    ...
    Student studentsArray[countOfStudents];
    ...
    return studentsArray;
}

This is even worse. The memory allocated to that array is released once the function ends. This is what the compiler warned you about. Things seemed to work afterwards, but Very Bad Things(tm) will happen very soon afterwards.

Most experienced C++ devs will do this:

std::vector<Student> createStudentsArray() {
    ...
    std::vector<Student> students(countOfStudents);
    ...
    return students;
}

If you "are not allowed to use vector" (StackOverflow sees that all the time), then do:

Student *createStudentsArray() {
    ...
    Student *studentsArray = new Student[countOfStudents];
    ...
    return studentsArray;
}

But you must then delete[] that array once you are finished using it.

Footnote - for extra info, if you are really interested.

In fact, the experienced devs would do this:

std::vector<std::shared_ptr<Student>> createStudentsArray() {
    ...
    std::vector<std::shared_ptr<Student> students(countOfStudents);
    for (int......)
    {
        students.push_back(std::make_shared<Student>());
        ...
    }
    return students;
}

Now, the vector contains a bunch of smart pointers rather than the actual objects, that is the most efficient way to make arrays (or other containers) of largish objects. std::vector and std::shared_ptr will clean up all the memory for you automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文