Re: [ROOT] CINT: access to variables of a compiled program?

From: George Heintzelman (gah@bnl.gov)
Date: Mon Oct 23 2000 - 16:47:23 MEST


> Hi Rooters,
> It is easily possible to run macros from within a compiled program. 
> But is it also possible, that these macros have access to the variables of
> the program, and therefore they can modify them? 
> A very unelegant solution to modify an integer variable of the program
> from a macro is the following:
> 
> int i=4;
> char cmd[100];
> sprintf(cmd,"int *i_p = (int *)0x%x;",&i);
> gInterpreter->ProcessLine(cmd);
> gInterpreter->ExecuteMacro("macro.C");
> cerr<<"the modified value is: "<<i<<endl;
> 
> where macro.C contains
> 
> //macro.C
> {
>   *i_p = 123;
> }
> 
> Is there a more elegant way to bind variables of the program to
> the interpreted variables of CINT? Or call interpreted functions on
> variables of the program?

Yes, you can bind compiled variables the same way you make precompiled 
classes available in CINT. In the LinkDef.h file that you are running 
through rootcint, add

#pragma link C++ global i;      // Or whatever your variable is

and you variable will turn up.

> First of all: is it safe? Does CINT use the same representations for ints,
> doubles etc, as the program? Might this be used not only for ints,
> doubles, etc, but also for stl::vector?

For basic types, CINT uses the underlying machine representation. 
However for complex objects, it is not guaranteed that the object 
layout will be the same if you have an interpreted class not linked in 
via the LinkDef mechanism versus the machine's representation. 
Therefore I recommend always using rootcint (or equivalently the script 
compiler) for linking in complex types. This is even possible with 
template classes, though an explicit link is needed for each 
instantiation on the template parameter, and it doesn't seem to work 
with the script compiler

(Note to Masaharu/Philippe: It would be really nice if you could load in a compiled template by doing a script compile of a file with an explicit instantiation of the template, eg:

#include <vector.h>
template class vector<int>;

(as opposed to template <> class vector<int> which is just a name declaration), but this doesn't seem to work right now. It would make pulling in needed templates as you work much easier... In fact, if you wanted to go the extra mile and make it automatically do that, it would be even better (but probably harder, maybe too hard).  
)

George Heintzelman
gah@bnl.gov



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:35 MET