Hi I have broken this code out of a larger code segment, basically I am using struct to define multiple sensors types and store the values. One particular value sTopic stores the value initially but then loses it, all the other values remain intact. To avoid confusion with any sensor information I just used millis() to give me a varying reading for the value.
The_Cleaner:
tTopic is only used to contruct the topic vairable before writing it to mySensor.sTopic, it is mySensor.sTopic that is losing its value.
You don't write "it" to mySensor.sTopic; that's a pointer so you're just assigning the address of tTopic.
As gfvalvo noted, once setup() is done, tTopic ceases to mean anything and the memory to which the pointer points can (and likely is) being used for other things.
Blackfin:
You don't write "it" to mySensor.sTopic; that's a pointer so you're just assigning the address of tTopic.
As gfvalvo noted, once setup() is done, tTopic ceases to mean anything and the memory to which the pointer points can (and likely is) being used for other things.
Forgive my ignorance but does mySensor.sTopic = tTopic; not store the value of tTopic into mySensor.sTopic?
And why do I see the correct value returned when I use Serial.println(mySensor.sTopic); if the value is not stored?
The_Cleaner:
Forgive my ignorance but does mySensor.sTopic = tTopic; not store the value of tTopic into mySensor.sTopic?
No.
In your structure you have sTopic declared as:
const char* sTopic
which is basically saying that "sTopic" is a pointer (*) to a character (char). In addition, you've also declared it as "const" which is technically not changeable (do you get any warnings from the compiler if you turn on verbose messages...)
Anyway, the assignment mySensor.sTopic = tTopic just assigns the address of tTopic to the sTopic member.
And why do I see the correct value returned when I use Serial.println(mySensor.sTopic); if the value is not stored?
The Serial.printlns that occur within setup() -- while tTopic is still valid, will work fine.
When you leave setup(), the character array tTopic goes out of scope and its memory just becomes free for other use. It's actually kind of dangerous (from a coding and predictable operation perspective) to dereference what is now an invalid pointer.
@blackfin, thankyou for the detailed response, I have programmed for many years but never in C so this is a learning curve, and I appreciate the effort put into replying. I was aware of of using const char and it not bing changeable, and that was the desire.
@gfvalva, thankyou for your suggestion, I tried before to predefine the sizes in my struct and could not get it to work, most likely bad fromatting on my part, however, using your format, I now get the error "use of deleted function 'Sensor::Sensor()' for my line of code struct Sensor mySensor;
I was unsure about the public: definition and as it did not seem to break anything I had left it in place, I will remove it.
You need to post a complete code. If your full code is too large and full of unrelated stuff, post an MCVE. This is the smallest possible complete code that reproduces the EXACT compiler error you're seeing.
@blackfin, I am using ESP32 board, Arduino IDE 1.8.10
@gfvalvo, the code is the same as the inital post except for changing the struct definition per yor suggection, I have not changed the rest of the code but have recopied it all below
You can technically use malloc() to assign heap (i.e. global) memory to the strings you want and leave only pointers in your structure. For example, in the above:
malloc should return a pointer to a block of heap of type char reserved with a size of the length of "Temperature" plus one (for the NULL termination...) and assigns it to the sTopic member of the struct.
If the call fails, it will return NULL. You probably want to assert a condition in this even to halt program execution or otherwise "safe" the system.
If the return value is not NULL, it's a valid pointer to the char array in the heap. That will exist outside the scope of setup() and you can use it globally.
Note: Some folks will probably bellyache about the "dangers" of using malloc() in small-memory devices and to an extent, they'd have a point if you're doing lots of malloc/free calls that can quickly fragment and chew up heap space.
If you just need to do this once at setup(), you're probably safe.
Thank-you, I will look more into your suggestion if memory becomes an issue.
I have 12 sensors some of which will not alsways be used (depending on where the setup is deployed) but I wanted to be able to specify via variables whether to include the code, this part I have working.
And thanks to the help from yourself and @gfvalvo my struct is now correctly defined and working.