urgent...sniff a variable in C????

M

Master

Hi all,

Please have a look at the following code snippet.

int a;
a=10;
a=30;

What i require here is a sniffer program or any kind of stuff which
keeps on watching the memory allocated for the variable 'a'. As soon
as a new value is assigned ie., the value of the variable 'a' is
changed, i should know about that change. if we consider the above
code snippet i should be able to get notifications 2 times during the
assignment.

Please think over it and help me out.



ENUM
I have an enumerated data type for line style. It consists of all the
possible line styles (single, double, dotted, dashed, etc) I want to
display the list of enumerated item names for user selection,
DYNAMICALLY. Simply saying, i want to get the enumerated item name
dynamically. How to do this? Thank you
 
J

jacob navia

Master said:
Hi all,

Please have a look at the following code snippet.

int a;
a=10;
a=30;

What i require here is a sniffer program or any kind of stuff which
keeps on watching the memory allocated for the variable 'a'. As soon
as a new value is assigned ie., the value of the variable 'a' is
changed, i should know about that change. if we consider the above
code snippet i should be able to get notifications 2 times during the
assignment.

Please think over it and help me out.

This can be achieved with a debugger.
You set a watch point for the memory location where the address
changes and you get notified when it changes.

lcc-win32 has such a debugger
ENUM
I have an enumerated data type for line style. It consists of all the
possible line styles (single, double, dotted, dashed, etc) I want to
display the list of enumerated item names for user selection,
DYNAMICALLY. Simply saying, i want to get the enumerated item name
dynamically. How to do this? Thank you

#include <stdio.h>
enum colors {red,green,blue};

struct colorDescriptions {
enum colors value;
char *name;
} ColorDescriptions[] = {
{red,"red"},
{green,"green"},
{blue,"blue"},
{0,NULL}
};

void showColor(enum colors color)
{
struct colorDescriptions *p = ColorDescriptions;
while (p->name)
if (p->value == color) {
printf("%s\n",p->name);
return;
}
p++;
}
printf("unknown color\n");
}

int main(void)
{
showColor(green);
}
 
J

Jens.Toerring

Master said:
Please have a look at the following code snippet.
int a;
a=10;
a=30;
What i require here is a sniffer program or any kind of stuff which
keeps on watching the memory allocated for the variable 'a'. As soon
as a new value is assigned ie., the value of the variable 'a' is
changed, i should know about that change. if we consider the above
code snippet i should be able to get notifications 2 times during the
assignment.

That's what is normally done with the help of a debugger by setting
a watch point. In that case you get instant notification of changes
of the memory location. If you want to do something like that from
within a C program you're out on your own as far as clc is concerned.
While it might be possible to do that on some systems for certain
kinds of variables there's no support for such things in standard C
and you will have to resort to extremely system dependend methods.
On other systems you probably will have to write your own kind of
debugger under which you then run your program. Again how to do that
is as system dependend as you can get.
ENUM
I have an enumerated data type for line style. It consists of all the
possible line styles (single, double, dotted, dashed, etc) I want to
display the list of enumerated item names for user selection,
DYNAMICALLY. Simply saying, i want to get the enumerated item name
dynamically. How to do this? Thank you

The names you used in the enumeration don't exist in the compiled
program anymore - because there's nothing dynamical about them
wherever they happened to be in the source code they got replaced
by the number associated with the name. All you can do is create
an additional array of strings with these names and somehow do a
mapping from the constants the names in the enumeration stand for
to the indices of the corresponding strings in that array.

Regards, Jens
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top