0

I'm currently tweaking some pixel shader code written in C++ of a computer game. I need a way to store a global value (integer or float) in a way that it can be retrieved later (within same process). Since everything seems to be executed "statelessly" in runtime it won't help simply declaring and using a static variable (class variable) as I normally would do in this situation.

Thus I came across the idea of storing my global value to a specific memory address, and get it from there later. My problem is that I have no idea whether this is possible at all and how to do it. I read those questions but didn't find an answer so far:

Create new C++ object at specific memory address?

Pointer to a specific fixed address

Assigning A Specific Memory Address from another program, and changing it's value

Assign a value to a specific address

Is this even possible on Windows Vista or 7, and if so, how? I have no option to include any library but have to achieve anything with the built-in c++ functionality.

If not (or not easily) achievable, are there alternative ways to store some value really globally (not as class variable) such that it can be accessed by other classes/instances within the same process? Maybe some sort of session or application cache like in .NET or Java?

Any help would be appreciated.

Community
  • 1
  • 1
Meltac
  • 1
  • 1
    What do you mean by "seems to be executed statelessly"? And how is "data stored at a specific memory address" not the same thing as any other ordinary global variable? – Karl Knechtel Aug 22 '11 at 07:28
  • I'm probably missing something but isn't global variable a solution? – Drakosha Aug 22 '11 at 07:28
  • Karl, what I mean is that the code seems to be executed not as it is the case in a normal c++ application where the compiled classes are loaded into the heap (or stack? don't remember) and all static variables are also stored there and therefore accessible from all instances of a class. In my case, each class instance seems to run in a separate memory area, and even the class itself with all static variables is loaded for each instance into separate memory, rather than only once for all. Thus, the "static" keyword does not behave as supposed to, it's not static over all the instances. – Meltac Aug 23 '11 at 07:47
  • Drakosha, please see my above comment for what you are missing. Normally, a global variable would be the "default" solution. Unfortunately, not in this case, due to the special memory handling. – Meltac Aug 23 '11 at 08:14

1 Answers1

0

I must admit I didnt get why static won't work in your case, so my suggestion might also not work. Is it possible for you to simply use a singleton like this:

class VariableStorage
{
public:
    int getVar(string name);
    void setVar(string name, int value);

    static VariableStorage* instance();

    //...

protected:
   VariableStorge() { static obj = 0; if(!obj) obj = new VariableStorage(); return obj; }
   // ...
}
pokey909
  • 1,797
  • 1
  • 16
  • 22
  • Thanks, but I'm afraid this won't help in my case because you are using static as well. Or do you mean the pointer indicator (*) should do the trick here? – Meltac Aug 23 '11 at 08:16