Clarification on volatile variables and function pointers.

Clarification on volatile variables and function pointers.

Post by Michael Ossare » Wed, 08 May 2002 02:09:21



Hi.

It has become apparent that I need to use a pointer to a function. This
is needed for code beauty more than anything. I will have lots of
functions that need to be timed when run. So I figured write a function
called TimeIT that takes another function as its argument and calls that
function whilst,... timing it!

However I have found it hard to actually work with the idea of function
pointers.

Here is a very basic code snippit,

===
#include <iostream>

void caller(int *);
void other(int);

void main(void)
{
   void (*t) (int) = other;
   caller((&t)(1));

Quote:}

int caller(void (*anything)(int))
{
return 1;

Quote:}

void other(int haha)
{
   cout << haha;
Quote:}

===

I receive

function.cc: In function `int main(...)':
function.cc:9: `&t' cannot be used as a function

when compiling with g++ 2.95.

Could someone give me a pointer (sorry bad pun) to how to do this correctly.

Also whilst reading some c++ code I noticed a global variable that was
marked volatile. From what I can see it means that the variable can be
modified by an outside program? could that be confirmed too please :)

Thank you.

--
+---------------------------0x4d-0x69-0x63-0x68-0x61-0x65-0x6c-+
| Difference between a virus and windows ? Viruses rarely fail |
+--------------------------------0x50-0x61-0x72-0x68-0x61-0x6d-+
|        (UNIX * Frustration) / Coffee = Productiveness        |
+---------------------------0x4f-0x73-0x73-0x61-0x72-0x65-0x68-+

 
 
 

Clarification on volatile variables and function pointers.

Post by Mike Wahle » Wed, 08 May 2002 02:55:25



Quote:> Hi.

> It has become apparent that I need to use a pointer to a function. This
> is needed for code beauty more than anything. I will have lots of
> functions that need to be timed when run. So I figured write a function
> called TimeIT that takes another function as its argument and calls that
> function whilst,... timing it!

> However I have found it hard to actually work with the idea of function
> pointers.

> Here is a very basic code snippit,

> ===
> #include <iostream>

> void caller(int *);

This prototype does not match your definition below.

void caller( void(*)(int));

Quote:> void other(int);

> void main(void)

int main(void)

Quote:> {
>    void (*t) (int) = other;
>    caller((&t)(1));

caller(t);

Quote:> }

> int caller(void (*anything)(int))
> {

   anything(42);

Quote:> return 1;
> }

> void other(int haha)
> {
>    cout << haha;

std::cout << haha; //  prints 42

- Show quoted text -

Quote:> }
> ===

> I receive

> function.cc: In function `int main(...)':
> function.cc:9: `&t' cannot be used as a function

> when compiling with g++ 2.95.

> Could someone give me a pointer (sorry bad pun) to how to do this
correctly.

> Also whilst reading some c++ code I noticed a global variable that was
> marked volatile. From what I can see it means that the variable can be
> modified by an outside program? could that be confirmed too please :)

That's correct.  'volatile' tells the compiler that during
any optimization, the object must not be 'optimized away'
which might be the case if it appears that no code uses
or modifies it.

-Mike

 
 
 

Clarification on volatile variables and function pointers.

Post by John Gluc » Wed, 08 May 2002 03:49:30


Hi Mike

volatile means that a variable can change at any time even while a function
is using the variable.

This often happens when the variable refers to an I/O port or when you are
running a realrime system. You could hav something like this;

extern int foo = 1;

while(foo);

In this case you have an endless loop.

However if foo can be modified elsewhere then by doing:

extern volatile int foo = 1;

while(foo);

you are sating that foo needs to be reread between successive loops.
Normally, the compiler see you are using the same variable and since your
code doesn't change it, the varible is simply loaded into a register and
that value is reused without reloading.

John


Quote:> Hi.

> It has become apparent that I need to use a pointer to a function. This
> is needed for code beauty more than anything. I will have lots of
> functions that need to be timed when run. So I figured write a function
> called TimeIT that takes another function as its argument and calls that
> function whilst,... timing it!

> However I have found it hard to actually work with the idea of function
> pointers.

> Here is a very basic code snippit,

> ===
> #include <iostream>

> void caller(int *);
> void other(int);

> void main(void)
> {
>    void (*t) (int) = other;
>    caller((&t)(1));
> }

> int caller(void (*anything)(int))
> {
> return 1;
> }

> void other(int haha)
> {
>    cout << haha;
> }
> ===

> I receive

> function.cc: In function `int main(...)':
> function.cc:9: `&t' cannot be used as a function

> when compiling with g++ 2.95.

> Could someone give me a pointer (sorry bad pun) to how to do this
correctly.

> Also whilst reading some c++ code I noticed a global variable that was
> marked volatile. From what I can see it means that the variable can be
> modified by an outside program? could that be confirmed too please :)

> Thank you.

> --
> +---------------------------0x4d-0x69-0x63-0x68-0x61-0x65-0x6c-+
> | Difference between a virus and windows ? Viruses rarely fail |
> +--------------------------------0x50-0x61-0x72-0x68-0x61-0x6d-+
> |        (UNIX * Frustration) / Coffee = Productiveness        |
> +---------------------------0x4f-0x73-0x73-0x61-0x72-0x65-0x68-+

 
 
 

Clarification on volatile variables and function pointers.

Post by Michael Ossare » Wed, 08 May 2002 06:14:12


Thank you much to both John and Mike.

John's illustration on how volatile works was like a beam of light into
my head :)

In regards to Tony picking me up on my prototypes, i sincerely hope that
wasnt all it was. I spent many hours looking over this code messing with
it and the like. Unfortunately I cannot be bothered to test it now as my
bed is calling, loudly, to me!

However the idea of how function pointers are constructed, defined, used
and abused are still beyond me - I have found this resource
http://www.newty.de/CCPP/FPT/em_fpt.html which i believe is very good -
however I'm finding it hard to follow. I'll keep at it - programming and
living a real life are not a desireable combination! too many pointers
used in my badly programmed brain!
--
+---------------------------0x4d-0x69-0x63-0x68-0x61-0x65-0x6c-+
| Difference between a virus and windows ? Viruses rarely fail |
+--------------------------------0x50-0x61-0x72-0x68-0x61-0x6d-+
|        (UNIX * Frustration) / Coffee = Productiveness        |
+---------------------------0x4f-0x73-0x73-0x61-0x72-0x65-0x68-+