Pointer to Function Failed

B

Bryan Parkoff

I created global variable for pointer to function. Set_pf() function is
to copy Test() function into global variable of pf(). The main() function
starts before Set_pf() function is executed. The Pointer_To_Function
variable copied into pf variable without any problem. pf() function has
memory address to have a pointer to function of Test() function. The
problem is that when Set_pf() function is exited before pf variable is set
to zero automatically that it is not supposed to do. Then, pf() function
inside main() function attempts to execute before crash occured with illegal
memory. Is there a way to fix to prevent pf() variable to set zero
automatically? Here is my source code below.

void (*pf)(void);

void Test(void)

{

int a = 5;

}

void Set_pf(void (*Pointer_To_Function)(void))

{

void (*pf)(void) = Pointer_To_Function;

pf(); // Execute is fine.

// Exit function to set zero to pf() variable. It is not supposed to do.

}



int main()

{

Set_pf(Test);

pf(); // Failed because of zero.

return 0;

}

Bryan Parkoff
 
M

Michael DOUBEZ

Bryan Parkoff a écrit :
I created global variable for pointer to function. Set_pf() function is
to copy Test() function into global variable of pf(). The main() function
starts before Set_pf() function is executed. The Pointer_To_Function
variable copied into pf variable without any problem. pf() function has
memory address to have a pointer to function of Test() function. The
problem is that when Set_pf() function is exited before pf variable is set
to zero automatically that it is not supposed to do. Then, pf() function
inside main() function attempts to execute before crash occured with illegal
memory. Is there a way to fix to prevent pf() variable to set zero
automatically? Here is my source code below.

void (*pf)(void);

void Test(void)

{

int a = 5;

}

void Set_pf(void (*Pointer_To_Function)(void))

{

void (*pf)(void) = Pointer_To_Function;

Here you are shadowing the global pf and using the local pf.

Simply use:
pf=Pointer_To_Function;
pf(); // Execute is fine.

// Exit function to set zero to pf() variable. It is not supposed to do.

No. If you have seen this in a debugger, display also &pf and you will
see that it changes.
 
A

Andre Kostur

I created global variable for pointer to function. Set_pf()
function is
to copy Test() function into global variable of pf(). The main()
function starts before Set_pf() function is executed. The
Pointer_To_Function variable copied into pf variable without any
problem. pf() function has memory address to have a pointer to
function of Test() function. The problem is that when Set_pf()
function is exited before pf variable is set to zero automatically
that it is not supposed to do. Then, pf() function inside main()
function attempts to execute before crash occured with illegal memory.
Is there a way to fix to prevent pf() variable to set zero
automatically? Here is my source code below.

void (*pf)(void);

void Test(void)

{

int a = 5;

}

void Set_pf(void (*Pointer_To_Function)(void))

{

void (*pf)(void) = Pointer_To_Function;


Why are you defining a new variable here? You want:

pf = Pointer_To_Function;
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top