[英]How does memory work in declaration of structure which uses self-referential structure pointer in C/C++?
In declaration of structure in C/C++, we have to use a self-referential structure pointer instead of a structure itself.在C/C++的结构声明中,我们必须使用自引用结构指针而不是结构本身。
// compile error // 编译错误
struct stack {
int overflow;
stack p;
}
struct stack {
int overflow;
stack* p;
}
One brings about the error but the other doesn't under the same condition( declaration ) I'm curious about the operation of memory areas when use stack* p;
一个带来了错误,但另一个没有在相同的条件下(声明)我很好奇memory区域在使用
stack* p;
during the declaration and how to make it possible.在声明期间以及如何使其成为可能。
This这个
struct stack {
int overflow;
stack p;
}
Tries to contains itself, so how big should it be?试图包含自己,那么它应该有多大? With one copy of itself it would look line this
有了它自己的一个副本,它看起来像这样
int ov;
{
int ov;
stack p;
}
but that stack p needs to be expanded - so we get但是堆栈 p 需要扩展 - 所以我们得到
int ov;
{
int ov;
{
int ov;
stack p;
}
}
but that stack p needs to be expanded - so we get但是堆栈 p 需要扩展 - 所以我们得到
int ov;
{
int ov;
{
int ov;
{
int ov;
stack p;
}
}
}
but that stack p needs to be expanded - so we get...... forever.但是那个堆栈 p 需要扩展 - 所以我们得到......永远。
Look at another way, how many int overflows should the struct contain?换个角度看,struct应该包含多少个int溢出?
The pointer one works like this指针一是这样工作的
int overflow;
stack *p;
The end.结束。 The struct is of a well defined size an integer and a pointer
该结构具有明确定义的大小 integer 和一个指针
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.