通过函数 C++ 创建结构体数组
我想创建一个创建结构数组的函数。我所做的如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你确实不能以这种方式做你想做的事:
首先,这是使用可变长度数组,严格来说这是 无效的 C++< /a>.例如,我的编译器不允许这样做。
其次,它只会返回一名
Student
。即使它返回了所有这些,它的效率也非常低,因为它必须复制它们,但它没有。你试过这个:
这更糟糕。一旦函数结束,分配给该数组的内存就会被释放。这就是编译器警告您的内容。之后事情似乎进展顺利,但很快就会发生非常糟糕的事情。
大多数经验丰富的 C++ 开发人员都会这样做:
如果您“不允许使用向量”(StackOverflow 总是看到这一点),那么请执行以下操作:
但是一旦完成,您必须
delete[]
该数组使用它。脚注 - 如果您真的感兴趣,可以获取更多信息。
事实上,经验丰富的开发人员会这样做:
现在,向量包含一堆智能指针而不是实际对象,这是制作大型对象数组(或其他容器)的最有效方法。
std::vector
和std::shared_ptr
将自动为您清理所有内存。You really cannot do what you are trying to do in that way:
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:
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:
If you "are not allowed to use vector" (StackOverflow sees that all the time), then do:
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:
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
andstd::shared_ptr
will clean up all the memory for you automatically.