Struct Initialization in Simple IDE
Domanik
Posts: 233
My struct "COORD" will not initialize unless I have a 1ms pause in the function. Or I copy a value to a global "Fval". I suspect it is initialized but something throws away the pointer to my struct before it's passed back to my calling routine. If so why does a 1ms pause or global activity make a difference. It's a mystery to me; I've spent a couple of hours trying all different combinations. The reason I don't simply put in a 1ms pause work-around, is the next time it may slip through unnoticed. Thanks for looking.
#include "simpletools.h" typedef struct {float x,y,z;} COORD; float Fval=-1; int main() { float xcoord=-1; print("go\n"); COORD pa, *coord_fn(); // print(" &pa = %04x \n", &pa); pa = *coord_fn(); // pa.y = 21; // print(" &pa = %04x \n", &pa); xcoord = pa.x; print("\n\n xcoord = %4.0f %4.0f \n", xcoord, pa.y); } COORD *coord_fn() { COORD pb;// = { 31, 32, 33}; pb = (COORD){ 31, 32, 33}; // Fval = pb.x; // pause(1); // print(" &pb = %04x ", &pb); return &pb; }
Comments
The before and after states of the structures can be observed using SimpleIDE's Run with Terminal button.
This is under-the-hood of the approach some simple libraries use to create multiple instances of the same process running in different cogs. All the process settings are stored by structure, and each instance of the same code running in different cogs looks to a different structure for its settings.
When you run the code, the result will be the same as the screen capture from the previous post. The only difference is that the structure was created dynamically and is referenced by memory address throughout.
P.S.
The malloc function is part of stdlib.h, which simpletools includes.
C programmers prefer to check the result to find out if there was enough memory. So you might see a main function that looks more like this:
They were there all right, that is, as long as there was a way for code outside the function to "observe" the function's struct value. For example if a global or a print was used within the function or using one of the values or any number of references to the struct values. But if my external reference went away so did my data even though the struct was declared and initialized. So it became like looking in the box to see if the cat is dead or alive or both? However, by knowing the future struct address in main I got around this and could look at data without the system knowing what I was up to. Then it was possible to apply different conditions in the function to observe the cat (and the data too).
My conclusion is that the compiler is very clever to optimize code and, in this case, when it determined data was not used outside the function it wouldn't set aside memory or initialize the values. When there was any kind of external reference the values would appear again. It may even be the function never existed?
I tried to simulate the pause with a for loop. the count was 9,000,000,000 - and another within that. There was no noticeable delay and no data appeared. However the pause(1) was observable outside the function therefore the function was not ignored. That was interesting.
Although returning data with a local struct is not advisable I thought I could get away with it since the data "had" to be there, and I would snatch it before it disappeared.
Andy, Thank you for an excellent presentation of using structures. And kuroneko, you are right, it is best to assume a local structure only exists while in context and avoid trouble. Dave I haven't tried your suggestion but will. Thank you for taking the time to post.
Dom...