function-pointers

C# P/Invoke: Marshalling structures containing function pointers

Sorry for the verbose introduction that follows. I need insight from someone knowing P/Invoke internals better than I do. Here is how I'm marshalling structures containing function pointers from C to C#. I would like to know whether it's the cleanest and/or most efficient way of doing it. I'm interfacing with a native DLL coded in C th...

Function pointers for winapi functions (stdcall/cdecl)

Please could someone give me a few tips for creating function pointers for MS winapi functions? I'm trying to create a pointer for DefWindowProc (DefWindowProcA/DefWindowProcW) but getting this error: LRESULT (*dwp)(HWND, UINT, WPARAM, LPARAM) = &DefWindowProc; error C2440: 'initializing' : cannot convert from 'LRESULT (__stdcall *)(H...

boost::bind with null function pointers

If the function pointer embedded in a boost::bind return object is NULL/nullptr/0, I need to take action other than calling it. How can I determine if the object contains a null function pointer? Addenda I don't believe I can use and compare boost::functions as the boost::bind return object is used with varying call signatures in a te...

Executing machine code in memory

I'm trying to figure out how to execute machine code stored in memory. I have the following code: #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { FILE* f = fopen(argv[1], "rb"); fseek(f, 0, SEEK_END); unsigned int len = ftell(f); fseek(f, 0, SEEK_SET); char* bin = (char*)malloc(len); ...

c function pointers

This program sorts lines alphabetically/numerically depending on the arguments passed to main. And im working on this exercise from k&R now: Add the option -f to fold upper and lower case together, so that case distinctions are not made during sorting; for example, a and A compare equal. Is what i wrote in my_strcmp good? And will it w...

Template deduction and function pointers

How does the compiler know the correct type for this code: class Base { protected: typedef View * ViewType; typedef boost::function<ViewType ()> ActionType; typedef boost::unordered_map<std::string, ActionType> ActionMapType; ActionMapType actions; template <class ControllerType> inline void addAction(std::st...

Function pointer and normal function

What are the differences between normal function and function pointer. One which I know is in case you are using a library in your software stack which gives you only function pointers then you can fill in the pointers for use later. ...

Passing C function pointers between two python modules

I'm writing an application working with plugins. There are two types of plugins: Engine and Model. Engine objects have an update() method that call the Model.velocity() method. For performance reasons these methods are allowed to be written in C. This means that sometimes they will be written in Python and sometimes written in C. The p...

C Function Pointer Behavior

For a class, I'm writing a simple crypt function. Everything works as expected: int crypt(char *key, int (*callback)(int, char*, int, int)) { int c, i = 0, n = strlen(key); while((c = fgetc(stdin)) != EOF) { // only change the char if it's printable if(c >= 32 && c <= 126) c = callback(c, key, n, ...

What happens if I cast a function pointer, changing the number of parameters

I'm just beginning to wrap my head around function pointers in C. To understand how casting of function pointers works, I wrote the following program. It basically creates a function pointer to a function that takes one parameter, casts it to a function pointer with three parameters, and calls the function, supplying three parameters. I ...

Using a STL map of function pointers

Hello, I developed a scripting engine that has many builtin functions, so far to call any function code just went into a if .. else if .. else if wall checking the name but I would like to develop a more efficient solution. Shoul I use a hashmap with strings as keys and pointers as values. How could I do it by using an STL map? EDIT: ...

Is it correct C99 that you don't need to specify arguments in function pointer declarations in structs?

I have written the following C99 code and was wondering about the struct declaration. In it i declare two function pointers which ultimately point to the two push/pop methods in the main code. In the function pointer declarations i've ommited the arguments and the program compiles ok. Is this correct? I'm sure i've read that the argument...

How to define a general member function pointer

Hello, I have created a Timer class that must call a callback method when the timer has expired. Currently I have it working with normal function pointers (they are declared as void (*)(void), when the Elapsed event happens the function pointer is called. Is possible to do the same thing with a member function that has also the signat...

Get a pointer to the current function in C (gcc) ?

Hi, is there a magic variable in gcc holding a pointer to the current function ? I would like to have a kind of table containing for each function pointer a set of information. I know there's a _func_ variable containing the name of the current function as a string but not as a function pointer. This is not to call the function th...

Why use function pointers in a struct in Objective-C?

Hey folks, I just read this http://stackoverflow.com/questions/2172887/use-c-struct-in-objective-c question, and I was wondering why anyone would want to use function pointers in structs. Wouldn't wrapping the functions in a class be equivalent? ...

EventHandler, event, delegate based programming in Python any example would appreciate?

Basically I'm a C# developer, I know the way C# does, EventHandler, delegate, even... but whats the best way to implement it on Python. ...

How do you declare a const array of function pointers?

Firstly, I've got functions like this. void func1(); void func2(); void func3(); Then I create my typedef for the array: void (*FP)(); If I write a normal array of function pointers, it should be something like this: FP array[3] = {&func1, &func2, &func3}; I want to make it a constant array, using const before "FP", but I've got...

Generic member function pointer help

Hey, I've got a question about general member function pointers. I'm trying to achieve something similar to the following question How to define a general member function pointer Essentially what I want to be able to do is to register a member function pointer that accepts a generic Event object as its argument with a specific event typ...

What is a good method to have a common interface to code without incurring the cost of dynamic lookup?

I am writing some code for handling data. There are a number of groups of processing functions that can be chosen by the user that are then applied to the dataset. I would like to implement all these groups in separate places, but since they all take the same parameters and all do similar things I would like for them to have a common int...

Storing function pointers

The following uses a simple function pointer, but what if I want to store that function pointer? In that case, what would the variable declaration look like? #include <iostream> #include <vector> using namespace std; double operation(double (*functocall)(double), double wsum); double get_unipolar(double); double get_bipolar(double); ...