Assign content to a function pointer

Reply

Join Date: Jul 2004
Posts: 2
Reputation: Richard Wong is an unknown quantity at this point 
Solved Threads: 0
Richard Wong Richard Wong is offline Offline
Newbie Poster

Assign content to a function pointer

 
0
  #1
Jul 1st, 2004
Goal: I would like to assign the content of a known function to a function pointer in LINUX environment.
Problem: Get segmentation fault when running the function pointer after assignment.

Procedure:
1). declare a funtion pointer.
2). Allocate memory space to the function pointer using the function' malloc'
3). Copy the content of a known function to the function pointer using
'memcpy'. (the interface of the known function is the same as the function pointer).
4). check the memory content of the function pointer using 'memcmp'.
Result: two memory contents match.
5). Run the function pointer
Result: segmentation fault.

Thanks for your attention.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Assign content to a function pointer

 
0
  #2
Jul 1st, 2004
Originally Posted by Richard Wong
Goal: I would like to assign the content of a known function to a function pointer in LINUX environment.
Problem: Get segmentation fault when running the function pointer after assignment.

Procedure:
1). declare a funtion pointer.
2). Allocate memory space to the function pointer using the function' malloc'
3). Copy the content of a known function to the function pointer using
'memcpy'. (the interface of the known function is the same as the function pointer).
4). check the memory content of the function pointer using 'memcmp'.
Result: two memory contents match.
5). Run the function pointer
Result: segmentation fault.
Why not just this:
  • Declare function pointer.
  • Point to the function (assign the pointer).
  • Run the function pointer.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Assign content to a function pointer

 
0
  #3
Jul 1st, 2004
Right, the problem with allocating ram and MOVING code into the ram is that jumps and other address references 'fixed up' by the linker are no longer correct.

Here's what Dave was saying:

typedef int (*MyFunctionPtr)(int foo);

int SomeFunction( int foo )
{
return foo;
}

MyFunctionPtr f;

f = SomeFunction; // points to the function SomeFunction()

printf("%d\n",f(4)); // prints '4'
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC