How to write a program to track the value of a certain variable at run-time using debug features???

T

trungthanh78

Hello everyone,

I'm totally new to the group and I would like to learn from you. Thank
you in advance!

I need to write a program to track whether a mathematical function has
changed during run-time or not. The program should work with any
mathematical function provided by users.

Let's take an example in the C language:
//===================================================
//The users define the two parameters a, b as two global variables.
The users can define as many parameters as possible and my program
does not know about this!!!
int a;
int b;

//below is the prototype for our mathematical function
void f(vector x);

//at first, an user wants f = 1x+1y, so he sets the value of a, b
accordingly
a=b=1;

double value=f(x); //calculate the value of function f

//now the user want f = 1x +2y, so he set b = 2
b =2;

//and invoke the function f again
value=f(x);
//=================================================

As can be seen from the code above, the function f has changed from f
= x+y to f=x+2y and my program needs to track this at run-time (The
function f, along with its parameters (a and b), will be integrated
with my program).

However, the problem is that my program will NOT KNOW which function
to be provided by the users and which/ how many parameters (defined as
global variables) the function may take

I think that one solution for the problem might be:

1. List all global variables currently used in the program
2. Find out which global variables are being used by f (I can do that
by parsing the source code file of f)
3. Tracking the value of all these global variables to see whether
they have been changed outside of f or not. If they have, then
possibly that f has been changed too.

Now the question is: How to list all global variables and how to track
the value of a certain global variable at run-time???

I wonder whether the existing debugger of windows/Visual Studio can
provide me with the necessary APIs to answer the question above?

Thank you so much for your help

Best regards,

Thanh
 
G

Guest

I need to write a program to track whether a mathematical function has
changed during run-time or not. The program should work with any
mathematical function provided by users.

One of the first chapters of 'The C++ programming language' has an example
of a calculator that does what you want.
Personally, I think the example is an example of bad programming, since it
involves a recursive loop that weaves its way through a codepath of n
different functions.

Still, it might give you an idea on how to tackle the problem.

Btw, this sounds like a homework assignment.
If so, remember that Learning by copying != learning.
 
C

classicalmania

One of the first chapters of 'The C++ programming language' has an example
of a calculator that does what you want.
Personally, I think the example is an example of bad programming, since it
involves a recursive loop that weaves its way through a codepath of n
different functions.

Still, it might give you an idea on how to tackle the problem.

Dear Bruno,

Thank you very much for your help. I will take a look at the book.
Btw, this sounds like a homework assignment.
If so, remember that Learning by copying != learning.
Thanks again for the reminder, but actually it is not my assignment.
As a part of my research, I have written an algrithm to solve
numerical functions which users provide as black boxes. Over time they
may change their funcions without noticing my algorithm, so I need a
method to track the changes.

Best regards,

Thanh.
 
C

classicalmania

One of the first chapters of 'The C++ programming language' has an example
of a calculator that does what you want.

Dear Bruno,

I have taken a look at the book. Unfortunately it is not what I want.
The example implement a calculator when user can input different
functions.

Actually I have written an algorithm to solve numerical
functions which are considered as black boxes. The algorithm will be
integrated by users with the code of their numerical functions so
that
their program can solve the functions.

What I want is a method to track whether the parameters of a given
function has changed or not. Users may also are not awared of the
change of the value of parameter, hence the only way to track it is to
periodically check whether the values have changed or not.


The problem is that my algorithm does not know anything about the
source code of users' function. It means that my algorithm does not
know neither the content of the function nor the variables that the
function may use and/or modify.
 
C

classicalmania

One possible solution, as suggested by Martin Irman from
microsoft.public.vc.debugger is to use C++ wrapper class. When used
along with template class this solution can help us to check each time
a parameter change. Something like this:

template <class PRM_TYPE>
class Parameter
{
private:
PRM_TYPE _prm;
public:
PRM_TYPE operator = (const PRM_TYPE& prm)
{
return (_prm = prm);
//some code to handle the change here ...
}
//...
};

However, this solution still require users to do the following:
1. Either use Parameter as the base class for the parameters that they
will use in the function
2. Or, use Parameter as the base class for all parameters. In this
case, we will still need to parse the code to find which parameter
being used in the function.

As a result, I was considering this the last resort and looking for
another alternative...
As I see, the VC debugger can show the list of global variables and
their value at run-time, so I wonder whether it is possible for me to
re-use some API from the debugger.


If it is impossible, then probably the solution above is the best
choice.

Thank you very much

Best regards,

Thanh.
 
N

Nathan Mates

As a result, I was considering this the last resort and looking for
another alternative...
As I see, the VC debugger can show the list of global variables and
their value at run-time, so I wonder whether it is possible for me to
re-use some API from the debugger.

No. Your exe's code, and the code executed in these user-specified
functions *MUST* be kept 100% separate. See my other email --
comingling the two is going to lead to a lot of pain, security problems,
and the like.

Nathan Mates
 
C

classicalmania

Dear Nathan,

Again, thank you very much for your advices and your useful links.

No. Your exe's code, and the code executed in these user-specified
functions *MUST* be kept 100% separate. See my other email --
comingling the two is going to lead to a lot of pain, security problems,
and the like.

They should be separated in case of developing commercial and/or real-
world applications. However, in my research community (where my
algorithm would likely be applied to), it is acceptable to combine the
two different bit of codes like this, because they don't care much to
the reliability of code at this stage. Code optimization will only be
the task of the next stage, if the research results became fruitful
and applicable.

Best regards,

Thanh.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top