function-pointers

Accessing the Body of a Function with Lua

I'm going back to the basics here but in Lua, you can define a table like so: myTable = {} myTable [1] = 12 Printing the table reference itself brings back a pointer to it. To access its elements you need to specify an index (i.e. exactly like you would an array) print(myTable ) --prints pointer print(myTable[1]) --prints 12 N...

Casting a function pointer to another type

Let's say I have a function that accepts a void (*)(void*) function pointer for use as a callback: void do_stuff(void (*callback_fp)(void*), void* callback_arg); Now, if I have a function like this: void my_callback_function(struct my_struct* arg); Can I do this safely? do_stuff((void (*)(void*)) &my_callback_function, NULL); I'...

How else to achieve "templated function pointers"?

Is it possible to establish a set of templated function pointers, without the hassle of doing so manually? Here's an example to illustrate what the heck I'm talking about. Let's say I have a frequently-called function "write" of which I have two implementations (write0 and write1) that I'd like to be able to switch between dynamically....

static vs extern "C"

(expert C/C++ question) What is the difference between a static member function and an extern "C" linkage function ? For instance, when using "makecontext" in C++, I need to pass a pointer to function. Google recommends using extern "C" linkage for it, because "makecontext" is C. But I found out that using static works as well. Am I just...

Where exactly do function pointers point?

Given that all the primitive data types and objects have memory allocated, it is intuitively easy to imagine the pointers to these types. But where exactly do function pointers point to? Given that instructions are converted into machine code and reside in memory, should we consider they point to the memory location corresponding to the...

is there a use for &func or class::func in c++ ?

This seems inconsistent. Why do we use &Example::func instead of Example::func? is there a use for Example::func or &exampleFunction? it doesnt seem like we can make a reference to a function so that rules out Example::func. and i cant think of a way to use &exampleFunction since exampleFunction already returns a pointer. #include <iost...

Is it safe to pass function pointers as arguments to dll functions and invoke them from inside of the dll?

I would like to pass some (dll or not) function pointers as arguments to some dll functions and call them from inside of the dll. I wonder if it is safe because I have found an information on http://publib.boulder.ibm.com/infocenter/zos/v1r10/index.jsp?topic=/com.ibm.zos.r10.cbcpx01/fpref.htm that: In DLL code, it is assumed that a func...

Why would one use function pointers to member method in C++?

A lot of C++ books and tutorials explain how to do this, but I haven't seen one that gives a convincing reason to choose to do this. I understand very well why function pointers were necessary in C (e.g., when using some POSIX facilities). However, AFAIK you can't send them a member function because of the "this" parameter. But if you'...

The Benefits of Using Function Pointers

I have been programming for a few years now and have used function pointers in certain cases. What I would like to know is when is it appropriate or not to use them for performance reasons and I mean in the context of games, not business software. Function pointers are fast, John Carmack used them to the extent of abuse in the Quake and...

Defining Function Pointers

I am trying to call the internal Windows NT API function NtOpenProcess. I know calling internal APIs can be a bad idea, but for this particular tool I need the low-level access this API provides. My problem is that to use such an internal API, I need to use Runtime Dynamic Linking, as specified in this article To do that, I need to def...

Run-Time Check Failure #0 loading QueryFullProcessImageName from kernel32.dll

I have an application that needs to run both on WinXP and Vista64. My program requires QueryFullProcessImageName() to work on Vista but not on XP. I try to load QueryFullProcessImageName() (instead of linking statically) via the kernel32.dll so that the same executable can run on both WinXP and Vista. The code that loads it is: //only ...

Can't convert function pointer argument

The error I'm getting: error C2664: 'v8::FunctionTemplate::New' : cannot convert parameter 1 from 'v8::Handle<T> (__cdecl *)(const v8::Arguments &)' to 'v8::InvocationCallback' Relevant definitions: typedef Handle<Value> (*InvocationCallback)(const Arguments& args); template<class C> class V8ScriptClass { public: template<cla...

C : function pointer and typedef problem

I have a C function that takes a function pointer as argument, it's a destructor that I'll call at the end of my program. Here is the prototype of my function : int store_dest(void (*routine)(void *)); I want to store this function pointer into a structure with some other infos. In order to have a nice struct, I want to have a typedef...

Compare two values that have an arbitrary number of bits in C

I have created a dynamic typing system in C in order to create a dictionary that can contain values of different bit widths. The structure of the dynamic object is: typedef struct { void* Pointer; unsigned char Size; } Dynamic; I need to compare two of these Dynamics that hold A2D readings and then compare the difference a...

A Question About Function Pointer In C

There's the following declarations: void qsort(void *lineptr[], int left, int right, int (*comp)(void *, void *)); int numcmp(char *, char *); int strcmp(char *s, char *t); Then, somewhere in the program there is the following call: qsort((void**) lineptr, 0, nlines-1, (int (*)(void*,void*))(numeric ? numcmp : ...

How does casting a function actually work in C?

int foo(char *c) {...} main() { int (*thud)(void *); thud = (int (*)(void *))(foo); } What actually happens during the evaluation of the assignment? There is a difference between the cast type and foo; the cast type is a pointer and foo is a function. So, does the compiler convert what's in '(foo)' into a pointer to foo ...

How can I typedef a function pointer that takes a function of its own type as an argument?

Example: A function that takes a function (that takes a function (that ...) and an int) and an int. typedef void(*Func)(void (*)(void (*)(...), int), int); It explodes recursively where (...). Is there a fundamental reason this can't be done or is there another syntax? It seems to me it should be possible without a cast. I'm really tr...

C++ Function Pointer issue

For some reason trying to pass a pointer to this function to a varadic function produces the following error: 1>c:\... : error C2664: 'PyArg_ParseTuple' : cannot convert parameter 3 from 'int (__cdecl *)(PyObject *,void *)' to '...' 1> Context does not allow for disambiguation of overloaded function PyArg_ParseTuple(args, "O&O&:...

How do function pointers in C work?

I had some experience lately with function pointers in C. So going on with the tradition of answering your own questions, I decided to make a small summary of the very basics, for those who need a quick dive-in to the subject. ...

Warning when using function pointers in C

Hi all! This is actually a non-critical question, but I get this warning most of the time I use function pointers and still couldn't figure out why on my own. Consider the following prototype: typedef void * Pointer; void tree_destroyLineage(Tree greatest_parent, void *dataDestructor(Pointer data)); And so far I can compile my thousan...