R
R
Rinsewind2020-10-25 01:14:11
css
Rinsewind, 2020-10-25 01:14:11

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))+"}"); //Строки нет
    }
}


What can be wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
display: block, 2016-04-19
@dzenn

photoswipe.com

A
Adilet Zhuman, 2016-04-19
@adik_zhuman

lightbox gallery
dimsemenov.com/plugins/magnific-popup

J
jcmvbkbc, 2020-10-25
@Rinsewind

What can be wrong?

It can be anything, because you brought a piece of code, but did not write how you use it, that in one case there is a line, and in the other it is not.
But I can look into my magic ball and tell you that on the line 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.
You can replace 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 question

Ask a Question

731 491 924 answers to any question