I have 0 experience programming in C++ or for Arduino, however I know what I'm doing in Python. It's been a huge learning experience so far, but I just can't understand what I'm doing wrong here. Hopefully you guys can point out the obvious mistake I'm making.
What I want is to define a global LinkedList which I can write to and read from in functions. Writing and reading within the same function seems to work fine, however outside of it, it doesn't return the expected values.
The two arrays are local to processBytes() so by the time loop() reads the pointers (.type and .id) out of the Model, they point to where the data used to be. The pointers stop being valid when the arrays go out of scope at the end of processBytes().
You could make the arrays static, but then every Model would share the same arrays. The data should either be in your struct or the pointers in your struct should point to static or dynamically allocated data.
Thanks for the explanation, I though that something like that was going on. Like I said, I have 0 experience in this: how would I point the pointers to the static or dynamic allocated data?
The idea is that I either create or update (or remove) objects based on the serial response. So I need a dynamic list of objects (with a max of course) that I can update.
In this example it's just 1 byte, but in my project there will be 2 for these attributes, and there is a settings attribute which has a variable length. I could remove the pointers though.
Hmm, weird, the code in the thread is the same as the wokwi link, if I replace lines 21, 22, and 23 with your suggestion, I get the error I shared earlier.
I noticed that if I make type and id just 1 byte (no pointers), and I create a struct with {0x0A, 0x0B} it all works, tried it out in a new wokwi: https://wokwi.com/projects/342451779865150036
@jfjlaros your suggestion does work if the array size is pre-defined, however will be a third attribute which can be variable in length. Must be something silly I don't know about yet regarding pointers.