static inline JGPlatformInfo* currentPlatform(){
static JGPlatformInfo* platform = nil;
if (platform == nil){
JGPlatformInfo info = supportedPlatform();
platform = &info;
}
return platform;
}
I am getting inconsistent results with this code (C family) The value of platform changes for no reason and the memory appears to be correct. What could be causing this? Could the inline keyword be affecting this?
Variables you defined are always temporary. That means when you leave scope your variable is going to get destroyed and the pointer to it will be invalid (or worse: it will point to a valid location that it shouldn’t point to). This is where dynamic allocation comes into hand. Here’s how I would achieve the same effect that you are going for:
In your source file:
If you really want this function inline, put this in your header file:
malloc(x)allocates x bytes of memory and returns a pointer to it.