function-pointers

Why are function pointers and data pointers incompatible in C/C++?

ぃ、小莉子 提交于 2019-11-29 13:37:30
I have read that converting a function pointer to a data pointer and vice versa works on most platforms but is not guaranteed to work. Why is this the case? Shouldn't both be simply addresses into main memory and therefore be compatible? An architecture doesn't have to store code and data in the same memory. With a Harvard architecture, code and data are stored in completely different memory. Most architectures are Von Neumann architectures with code and data in the same memory but C doesn't limit itself to only certain types of architectures if at all possible. Some computers have (had)

Does C++11 std::function limit the number of arguments a function pointer can have?

不问归期 提交于 2019-11-29 12:40:58
问题 I'm using the Visual Studio 11 beta and I'm curious about a compilation error i'm getting storing a std::function object in my class. typedef std::function<void (int, const char*, int, int, const char*)> MyCallback; In my class I have, MyCallback m_callback; This compiles just fine. If I add one more argument to the list it fails. typedef std::function<void (int, const char*, int, int, const char*, int)> MyCallback; The failure is: >c:\program files (x86)\microsoft visual studio 11.0\vc

Cast member function for create_pthread() call

橙三吉。 提交于 2019-11-29 12:09:30
I want to stop the warning server.cpp:823: warning: converting from 'void* (ClientHandler:: )()' to 'void ( )(void )' in the call: pthread_create(th, NULL, (void* (*)(void*)) &ClientHandler::handle, (void *) clientHandler); where handle() is a member function of ClientHandler : void* ClientHandler::handle(); I have difficulties deciphering the function-type message from the compiler. The question is: Should I change the handle() interface? Can I get rid of casting overall? Should I change the cast? To what exactly? Something completely different? Mat You can't do that directly, pointers to

C to Python via Ctypes - Wrapping Struct of Function Pointers to Static Functions

大城市里の小女人 提交于 2019-11-29 11:56:46
I have structs in a C library that are like this. The function pointers in DataFn point to static functions. .h struct Data { int i; int *array; }; typedef struct { bool (* const fn1) (struct Data*, const char *source); .... } DataFn; extern DataFn const DATAFUNC Using objdump, the table only contains DATAFUNC and a few other things from gcc. This is fine in C where calling fn1 would go like DATAFUNC.fn1(..., ...), but how would something like this be wrapped around so fn1 can be called in python w/ ctypes? Example python libc = ctypes.cdll.LoadLibrary("./data.so") print(libc.DATAFUNC) results

Assign C++ instance method to a global-function-pointer?

做~自己de王妃 提交于 2019-11-29 11:45:46
Greetings, My project structure is as follows: \- base (C static library) callbacks.h callbacks.c paint_node.c . . * libBase.a \-app (C++ application) main.cpp In C library 'base' , I have declared global-function-pointer as: in singleheader file callbacks.h #ifndef CALLBACKS_H_ #define CALLBACKS_H_ extern void (*putPixelCallBack)(); extern void (*putImageCallBack)(); #endif /* CALLBACKS_H_ */ in single C file they are initialized as callbacks.c #include "callbacks.h" void (*putPixelCallBack)(); void (*putImageCallBack)(); Other C files access this callback-functions as: paint_node.c #include

C++: how to define a class method as a start routine to thread (with pthread library)

落爺英雄遲暮 提交于 2019-11-29 11:11:20
i have a Base class and Derived class. they have a virtual function- virtual void action() how can i pass it to *pthread_create()* function? example(with errors): class Base{ protected: pthread_t tid; public: virtual void* action() = 0; }; class Derived : public Base{ void* action(); Derived(){ pthread_create(&tid, NULL, &action, NULL); } }; maybe it should be static? i tried lot of combinations but cant find solution.. I ran into this problem a couple months back working on my senior design project. It requires some knowledge of underlying C++ mechanics. The underlying issue is that pointers

Pointers to static methods in Python

风流意气都作罢 提交于 2019-11-29 11:03:15
Why is it that in the following code, using a class variable as a method pointer results in unbound method error, while using an ordinary variable works fine: class Cmd: cmd = None @staticmethod def cmdOne(): print 'cmd one' @staticmethod def cmdTwo(): print 'cmd two' def main(): cmd = Cmd.cmdOne cmd() # works fine Cmd.cmd = Cmd.cmdOne Cmd.cmd() # unbound error !! if __name__=="__main__": main() The full error: TypeError: unbound method cmdOne() must be called with Cmd instance as first argument (got nothing instead) glglgl I like to view this behaviour from the "bottom up". A function in

What is meaning of a pointer to a constant function?

荒凉一梦 提交于 2019-11-29 10:54:40
问题 Pointers can be declared as pointing to mutable (non-const) data or pointer to constant data. Pointers can be defined to point to a function. My coworkers and I were discussing the use of "const" with pointers and the question came up regarding the use of const with function pointers. Here are some questions: What is the meaning of a pointer to a constant function versus a pointer to a non-constant function? Can a function be const? Can a function be non-const (mutable)? What is the proper

How do I refer to std::sin(const valarray<double> &)?

自古美人都是妖i 提交于 2019-11-29 10:47:17
I'm having trouble with some valarray function pointer code: double (*fp)(double) = sin; valarray<double> (*fp)(const valarray<double> &) = sin; The first compiles, the second gives: error: no matches converting function 'sin' to type 'class std::valarray<double> (*)(const class std::valarray<double>&)' This compiles, using the __typeof__ GCC extension. Looks like GCC's valarray uses expression templates to delay calculation of the sinus. But that will make the return type of the sin template not exactly valarray<T> , but rather some weird complex type. #include <valarray> template<typename T>

Calling a function through a function pointer - dereference the pointer or not? What's the difference?

北慕城南 提交于 2019-11-29 10:34:29
I tried both - C and C++ and both work fine. I'm kinda new to function pointers and here's a simple code, that surprised me: #include <assert.h> void sort( int* arr, const int N ); int main () { int arr1[] = { 1, 5, 2, 6, 2 }; int arr2[] = { 1, 5, 2, 6, 2 }; void (*sort_ptr)( int*, const int) = sort; sort_ptr( arr1, 5 ); (*sort_ptr)( arr2, 5 ); assert( arr1[0] == 1 && arr1[1] == 2 && arr1[2] == 2 && arr1[3] == 5 && arr1[4] == 6 ); assert( arr2[0] == 1 && arr2[1] == 2 && arr2[2] == 2 && arr2[3] == 5 && arr2[4] == 6 ); return 0; } void sort( int* arr, const int N ) { // sorting the array, it's