Answer the question
In order to leave comments, you need to log in
Where does the line go?
class CFListElement {
public:
String Name;
CFListElement *Next;
CFListElement(String name) {
Name = name;
}
};
class CFuncLibrary {
public:
CFListElement *root;
CFListElement *last;
void Add(String name) {
CFListElement T(name);
if(!last) {
root = &T;
} else {
last->Next = &T;
}
last = &T;
Con.print("'"+last->Name+"' = {"+String((long)&(last->Name))+"}"); //Строка на месте
}
CFListElement *Search(String Name) {
CFListElement *T = root;
Con.print("'"+last->Name+"' = {"+String((long)&(last->Name))+"}"); //Строки нет
}
}
Answer the question
In order to leave comments, you need to log in
What can be wrong?
root = &T;
in the Add function, you assigned the pointer the address of a temporary object that was destroyed when the function exited. Therefore, if you first call Add, then the line is there, because the object on the stack is alive, and if you then call Search, then there is no line, because the object is destroyed, undefined behavior, and the program may crash every other time. CFListElement T(name);
with CFListElement *T = new CFListElement(name);
and fix & / * further down the code to see if this is the case or not. After that, deal with the lifetime of objects and rewrite everything as it should.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question