| | |
Assign content to a function pointer
![]() |
•
•
Join Date: Jul 2004
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
•
•
•
•
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.
- 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
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'
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'
![]() |
Similar Threads
- How to call a class member function via function pointer in map [C++] (C++)
- DialogBox() function and function pointer in class scope (C++)
- How to get adress of class function. (Function pointer) (C++)
- struct and member function pointer (C)
- putting the address of a function pointer into a struct (C)
Other Threads in the C Forum
- Previous Thread: Recursive prime number f(x)
- Next Thread: Need help on a program (function bug)
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






