function-pointers

Create function in memory

北战南征 提交于 2019-12-10 10:52:15
问题 I learned about function pointers recently in class and I was wondering if you could assign a function pointer to a block of memory allocated by a program, fill the memory block with assembly commands (hex values of op codes), then call the memory block using the function pointer. I don't know much about function pointers, but I'm guessing you can't assign them wherever you want in memory, they need to point to a function. If that's true, how can you create a function in memory to be called?

Templates, Function Pointers and C++0x

那年仲夏 提交于 2019-12-10 02:41:49
问题 One of my personal experiments to understand some of the C++0x features: I'm trying to pass a function pointer to a template function to execute. Eventually the execution is supposed to happen in a different thread. But with all the different types of functions, I can't get the templates to work. #include <functional> int foo(void) {return 2;} class bar { public: int operator() (void) {return 4;}; int something(int a) {return a;}; }; template <class C> int func(C&& c) { //typedef typename std

Should lambda decay to function pointer in templated code?

浪尽此生 提交于 2019-12-10 02:36:50
问题 I read somewhere that a lambda function should decay to function pointer if the capture list is empty. The only reference I can find now is n3052. With g++ (4.5 & 4.6) it works as expected, unless the lambda is declared within template code. For example the following code compiles: void foo() { void (*f)(void) = []{}; } But it doesn't compile anymore when templated (if foo is actually called elsewhere): template<class T> void foo() { void (*f)(void) = []{}; } In the reference above, I don't

Function pointers for winapi functions (stdcall/cdecl)

坚强是说给别人听的谎言 提交于 2019-12-09 15:34:02
问题 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 *)(HWND,UINT,WPARAM,LPARAM)' to 'LRESULT (__cdecl *)(HWND,UINT,WPARAM,LPARAM)' I can't figure out what I need to use because I am not used to the MS ascii/wide

What causes compiler to warn for unused functions?

China☆狼群 提交于 2019-12-09 14:14:42
问题 Simple setup: There are n prototypes for functions and implementations of the functions. There is one big array of function pointers. Each function is listed in this array. Some still cause -Wunused-function when compiling with gcc. Code: void foo1(void); void foo2(void); void bar1(void); void bar2(void); /* and their implementations */ void (*functions[])(void) = { foo1, foo2, bar1, bar2 }; This is what the setup looks like (just an example)! One of this foo/bar functions now causes a

Function pointers in C - address operator “unnecessary”

蓝咒 提交于 2019-12-09 12:36:18
问题 Using qsort in C we pass in a comparison function e.g. int cmp(const void*, const void*); the protoype of qsort expects a int (* )(const void* , const void*) so we call: qsort(..., cmp); but it is equally valid to call: qsort(..., &cmp); and this is what we would have to do if we passed in a static member-function in C++. Kernighan & Ritchie (2nd Edition, 5.11 "Pointers To Functions" p119) states that "since [cmp] is known to be a function, the & operator is not necessary, in the same way

What good is a function pointer inside a struct in c?

百般思念 提交于 2019-12-09 11:46:36
问题 I guess using a function pointer inside a struct has something to do with encapsulating a function within a structure...? If so, then how exactly is this achieved?? And what good does it bring to have a function pointer inside a struct rather than simply defining the function? 回答1: function pointers inside structures are the base of object-programming in C (see http://www.planetpdf.com/codecuts/pdfs/ooc.pdf ). It is really really for medium-to-large C projects. An example: Header: typedef

How is pointer to member function implemented in C++?

半城伤御伤魂 提交于 2019-12-09 10:50:07
问题 The pointer to member function in c++ is in three parts: Offset Address/index virtual? Offset is used for Pointer adjustment when derived object is called using base pointer. How is this offset implemented? Is it pointer to some table, one table for each derived class and the table contains entries of the form (base X, offset) ? Also, where can I get more info about this? 回答1: First you should note that a C++ method can be implemented (and is normally implemented) just as a regular function

CUDA kernel with function pointer and variadic templates

人盡茶涼 提交于 2019-12-09 03:58:53
问题 I am trying to design a cuda framework which would accept user functions and forward them to the kernel, through device function pointers. CUDA can work with variadic templates (-stc=c++11) and so far so good. However, I hit a problem when the kernel calls the device function pointer. Apparently the kernel runs with no problem, but the GPU usage is 0%. If I simply replace the callback pointer with the actual function then GPU usage is 99%. The code here is very simple and the large loop range

Interview : function pointers vs switch case

柔情痞子 提交于 2019-12-08 17:45:07
问题 During my Interview, I was asked to implement a state machine for a system having 100 states where each state in turn has 100 events, I answered 3 following approaches: if-else switch-case function pointers If-else is obviously not suited for such a state machine, hence main comparison was between switch-case vs function pointers, here is the comparison as per my understanding: Speed wise both are almost same. Switch-case is less modular than function-pointers Function-pointers has more