R
R
Rinsewind2018-12-23 13:23:09
Arduino
Rinsewind, 2018-12-23 13:23:09

Why is there garbage in the variable?

Good afternoon!
There is a structure that stores a couple of lines:

struct KeyValue {
  String Key;
  String Value;
  KeyValue* Next;
};

Variable stores a variable of type KeyValue;
KeyValue* Last = &Variable;
KeyValue N = KeyValue { Name, Val, nullptr};
Last->Next = &N;
Last = Last->Next;

Why is there garbage in Last->Key after executing this code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vanyamba-electronics, 2018-12-25
@Rinsewind

Because the variable N is local, and ceases to exist after the line:
You should do this:

KeyValue* Last = &Variable;
KeyValue* pN = new KeyValue { Name, Val, nullptr};
Last->Next = pN;
Last = Last->Next;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question