943,772 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 9113
  • C RSS
Jul 1st, 2004
0

Assign content to a function pointer

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Richard Wong is offline Offline
2 posts
since Jul 2004
Jul 1st, 2004
0

Re: Assign content to a function pointer

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 1st, 2004
0

Re: Assign content to a function pointer

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'
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Recursive prime number f(x)
Next Thread in C Forum Timeline: Need help on a program (function bug)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC