dynamic variable accesss...

Posted on 2005-04-12
Last Modified: 2010-04-02
I have no idea if this is possible, but I would think that it is (even if indirectly, such as by direct memory access, which would be less safe).

I code models for research groups. For every model there are a number of input parameters describing the input population that are read from a text file, but they're never all the same for all the models. I'd like to write a class that would take care of reading in the input variables (from the file) and setting the appropriate variables in the corresponding program to the input values. The tricky part is I'd like to be able to simply declare variables in the main program that are named the same as the ones in the input file and have the class I mentioned set those variables directly, but without having to change the class at all. Basically, to have the class dynamicall know which variables to set based on the read in names, just assuming that I have appropriately declared them in the main program (an error if they aren't declared is fine). Right now I read them into a map, setting the key to the variable name that I read in, and then manually set each variable value from the map value (since reading from the map is slow, and speed is the issue here, not memory). So I'd like to skip the manually setting part and have the class just "know" from the names that are read in. Is this possible? Is there any kind of low-level function to get a pointer to a variable based on a string name or something?
Question by:bobthe
    LVL 22

    Assisted Solution

    If you make the variables static globals, then you can set up a table of variables and their addresses, something like:

    typedef struct { char VarName[30]; float * VarAddress; VDesc * Next }   VDesc;

    VDesc * VHead = NULL;

    void RegisterVar( char * Name; float * Where )
         VDesc * vp = new VDesc;
         VDesc->Next = VHead;
        strncpy( VDesc->VarName, sizeof( VDesc->VarName), Name );
        VDesc->VarAddress = Where;
        VHead = VDesc;

    .... then at runtime you can search for your read-in name in this list;

    LVL 15

    Accepted Solution

    >  Is there any kind of low-level function to get a pointer to a variable based on a string name or something?

    No.  Variable names don't exist at run time, except perhaps as debugging information which is not accessible to the application programmer.

    Your approach with the map seems reasonable to me.  If performance is really the problem, someone here might be able to help you if you provide some more information, such as how many variables there are, what data type you are using for the map key, how fast it runs now, and how fast you want it to run.

    Author Comment

    Thanks for the responses both of you.

    grg99: A nice idea, but basically the same thing I mentioned about using a map<variablename,value> but in the other direction.

    efn: Good to know about the variable names. I definately remember learning that at one point but it never came up since college, so I completely forgot (silly me).

    Speed of reading in the variables isn't an issue. The real issue is really the fact that these models will be used after my time on the grants is finished and I want to make it as easy as possible for the researchers to make minor updates without having to understand the intricacies of most of the code (it's more complicated than just reading in individual values, but for a programmer it's not much more complicated). Anyhow, that's what documentation is for I guess!

    Thanks for taking the time to respond. Although it looks like my current method is probably the best for now, I'll split you two the points for taking the time to respond.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now