• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 145
  • Last Modified:

dynamic variable accesss...

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?
0
bobthe
Asked:
bobthe
2 Solutions
 
grg99Commented:
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;


     
0
 
efnCommented:
>  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.
0
 
bobtheAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now