This 的地址在执行期间发生变化。为什么?
Address of This changes during execution. why?
如你所见,从我的输出结果来看,this的地址在我执行过程中发生了变化,我想我可以利用指针比较来实现树系统而不用在 Node 中有一个子列表,我希望能够比较 nodo 列表中每个元素的父级以获得更多功能。但它的主要问题是指针地址更改,任何人都可以帮助我理解我所缺少的东西。
struct Nodo{
Nodo* parent=0;
const char* identity;
Component* component;
Nodo()=default;
Nodo(const char* uid){
identity=uid;
}
Nodo(Nodo* ptr,const char* uid){
parent=ptr;
identity=uid;
std::cout << "\n Address given to " << uid << " " << ptr <<std::endl;
}
void Add(const char* uid,std::vector<Nodo>& objects){
std::cout << "\n Add call in " << identity << " address sent "<< this <<std::endl;
objects.emplace_back(Nodo(this,uid));
}
void GrapthUI(std::vector<Nodo>& nodes){
ImGui::PushID(this);
if(ImGui::TreeNode(identity)){
ImGui::TreePop();
ImGui::Indent();
for(int indx=0; indx<nodes.size(); indx++){
if(&nodes[indx]!=this){
if(nodes[indx].parent==this){
nodes[indx].GrapthUI(nodes);
}
}
}
ImGui::Unindent();
}
ImGui::PopID();
}
}
std::vector<Nodo> node;
Main(){//in c++ file.
node.emplace_back(Nodo("root"));
node[0].Add("Airplane",node);
node[0].Add("Ball",node);
node[1].Add("Car",node);
}
输出:
Add call in [ root ] address sent 0C8FCF88
Address given to [ Airplane ] 0C8FCF88
Add call in [ root ] address sent 0C920C68
Address given to [ Ball ] 0C920C68
Add call in [ Airplane ] address sent 0C916DE4
Address given to [ Car ] 0C916DE4
我希望 Airplane 和 Ball 的父指针具有相同的地址 [0C8FCF88] 的 Root 但它是不同的。我在这里看到了一个与此类似的同名 post,但它对我没有帮助,也不完全涉及我的问题。
如果向向量中添加一个元素并且生成的大小超过其当前容量,则向量需要分配更多内存。它分配一个新的内存块并将所有现有元素移动到该内存块中,然后添加新元素。然后释放旧内存块。
如果你想有稳定的地址,你可以使用std::vector<std::unique_ptr<Nodo>>
。
这不是问题。这种行为是正常的。 std::vector 当你开始分配一块特定维度的内存时。如果分配的内存足够,则向向量添加元素时,地址相同(还要注意如果内存足够但接近限制,执行可能会重新定位整个向量),如果内存不足,所有向量都重新定位到更大的新 space 内存中,并且释放了先前的内存区域。当您从向量中删除一个元素时,也会发生这种行为,该元素会自行调整其大小以节省内存。
If the new size() is greater than capacity() then all iterators and
references (including the past-the-end iterator) are invalidated.
Otherwise only the past-the-end iterator is invalidated.
如你所见,从我的输出结果来看,this的地址在我执行过程中发生了变化,我想我可以利用指针比较来实现树系统而不用在 Node 中有一个子列表,我希望能够比较 nodo 列表中每个元素的父级以获得更多功能。但它的主要问题是指针地址更改,任何人都可以帮助我理解我所缺少的东西。
struct Nodo{
Nodo* parent=0;
const char* identity;
Component* component;
Nodo()=default;
Nodo(const char* uid){
identity=uid;
}
Nodo(Nodo* ptr,const char* uid){
parent=ptr;
identity=uid;
std::cout << "\n Address given to " << uid << " " << ptr <<std::endl;
}
void Add(const char* uid,std::vector<Nodo>& objects){
std::cout << "\n Add call in " << identity << " address sent "<< this <<std::endl;
objects.emplace_back(Nodo(this,uid));
}
void GrapthUI(std::vector<Nodo>& nodes){
ImGui::PushID(this);
if(ImGui::TreeNode(identity)){
ImGui::TreePop();
ImGui::Indent();
for(int indx=0; indx<nodes.size(); indx++){
if(&nodes[indx]!=this){
if(nodes[indx].parent==this){
nodes[indx].GrapthUI(nodes);
}
}
}
ImGui::Unindent();
}
ImGui::PopID();
}
}
std::vector<Nodo> node;
Main(){//in c++ file.
node.emplace_back(Nodo("root"));
node[0].Add("Airplane",node);
node[0].Add("Ball",node);
node[1].Add("Car",node);
}
输出:
Add call in [ root ] address sent 0C8FCF88
Address given to [ Airplane ] 0C8FCF88
Add call in [ root ] address sent 0C920C68
Address given to [ Ball ] 0C920C68
Add call in [ Airplane ] address sent 0C916DE4
Address given to [ Car ] 0C916DE4
我希望 Airplane 和 Ball 的父指针具有相同的地址 [0C8FCF88] 的 Root 但它是不同的。我在这里看到了一个与此类似的同名 post,但它对我没有帮助,也不完全涉及我的问题。
如果向向量中添加一个元素并且生成的大小超过其当前容量,则向量需要分配更多内存。它分配一个新的内存块并将所有现有元素移动到该内存块中,然后添加新元素。然后释放旧内存块。
如果你想有稳定的地址,你可以使用std::vector<std::unique_ptr<Nodo>>
。
这不是问题。这种行为是正常的。 std::vector 当你开始分配一块特定维度的内存时。如果分配的内存足够,则向向量添加元素时,地址相同(还要注意如果内存足够但接近限制,执行可能会重新定位整个向量),如果内存不足,所有向量都重新定位到更大的新 space 内存中,并且释放了先前的内存区域。当您从向量中删除一个元素时,也会发生这种行为,该元素会自行调整其大小以节省内存。
If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated. Otherwise only the past-the-end iterator is invalidated.