function-pointers

Implement callback function in JNI using Interface

こ雲淡風輕ζ 提交于 2019-12-17 15:51:02
问题 I need to implement callback function in Java using “interface”. I have wrote the application part as MyJavaFunction(int size, m_GetSizeInterface); m_GetSizeInterface is an Interface which contains the callback function GetSize. This GetSize method is override in the application. In JNI I need to call a CPP function having prototype int MyCPPFunction(int size, int (*callback)(int* ID)); How can I pass this GetSize as parameter to MyCPPFunction in JNI? Please help public int GetSize (m

What does this invocation of a char array cast as a function do?

ぐ巨炮叔叔 提交于 2019-12-17 14:53:44
问题 I came across this piece of code: char code[] = "\xb0\x01\x31\xdb\xcd\x80"; int main(int argc, char **argv) { int (*func)(); func = (int (*)()) code; (int)(*func)(); } It is copied from Writing Shellcode for Linux and Windows Tutorial. Could someone explain that what this function invocation (int)(*func)(); is doing? 回答1: It calls a function whose machine code is in the array code . The string contains some machine-level instructions ((three I think, have a look at x86 instruction set). func

CUDA function pointers

邮差的信 提交于 2019-12-17 14:52:12
问题 I was trying to make somtehing like this (actually I need to write some integration functions) in CUDA #include <iostream> using namespace std; float f1(float x) { return x * x; } float f2(float x) { return x; } void tabulate(float p_f(float)) { for (int i = 0; i != 10; ++i) { std::cout << p_f(i) << ' '; } std::cout << std::endl; } int main() { tabulate(f1); tabulate(f2); return 0; } output: 0 1 4 9 16 25 36 49 64 81 0 1 2 3 4 5 6 7 8 9 I tried the following but only got the error Error:

Returning function pointer type

隐身守侯 提交于 2019-12-17 10:15:48
问题 Often I find the need to write functions which return function pointers. Whenever I do, the basic format I use is: typedef int (*function_type)(int,int); function_type getFunc() { function_type test; test /* = ...*/; return test; } However this can get cumbersome when dealing with a large number of functions so I would like to not have to declare a typedef for each one (or for each class of functions) I can remove the typedef and declare the local variable returned in the function as: int (

Most vexing parse confusion

て烟熏妆下的殇ゞ 提交于 2019-12-17 09:57:16
问题 I'm studying C++11 and I stumbled upon uniform initializers. I don't understand the following code which should show the "most vexing parse" ambiguity: #include<iostream> class Timer { public: Timer() {} }; int main() { auto dv = Timer(); // What is Timer() ? And what type is dv? int time_keeper(Timer()); // This is a function right? And why isn't the argument " Timer (*) ()" ? return 0; } 回答1: Here: auto dv = Timer(); You have an object of type Timer called dv that is being copy-initialized

How Can I Pass a Member Function to a Function Pointer?

こ雲淡風輕ζ 提交于 2019-12-17 09:49:17
问题 class Child; class Parent { public: void (*funcPointer)(); void (*funcPointer2)(Parent* _this); void (Child::*funcPointer3)(); }; class Child: public Parent { public: void TestFunc(){ } void Do(){ Parent p; p.funcPointer=TestFunc; // error, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(void)' p.funcPointer2=TestFunc; // error too, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(Parent *)' p.funcPointer3=TestFunc; //this

How do I compare two functions for pointer equality in the latest Go weekly?

不问归期 提交于 2019-12-17 09:23:23
问题 In Go, is there any way to compare two non-nil function pointers to test for equality? My standard of equality is pointer equality. If not, is there any particular reason why pointer equality is not allowed? As of now, if I attempt to do this in the straight-forward way: package main import "fmt" func SomeFun() { } func main() { fmt.Println(SomeFun == SomeFun) } I get ./func-pointers.go:12: invalid operation: SomeFun == SomeFun (func can only be compared to nil) It is my understanding that

How do I compare two functions for pointer equality in the latest Go weekly?

南笙酒味 提交于 2019-12-17 09:21:20
问题 In Go, is there any way to compare two non-nil function pointers to test for equality? My standard of equality is pointer equality. If not, is there any particular reason why pointer equality is not allowed? As of now, if I attempt to do this in the straight-forward way: package main import "fmt" func SomeFun() { } func main() { fmt.Println(SomeFun == SomeFun) } I get ./func-pointers.go:12: invalid operation: SomeFun == SomeFun (func can only be compared to nil) It is my understanding that

How typedef works for function pointers

被刻印的时光 ゝ 提交于 2019-12-17 06:46:22
问题 I think I may be suffering from the dreaded "accidental programmer" disease, at least when it comes to typedefs and function pointers. So I've been experimenting with all kinds of combinations involving these to analyse the results based on all the output I get. But as I kept on trying different combinations, instead of analyzing the results I'm now just lost in process. I'm hoping you guys will help me figure out this mess. First code example typedef void (print)(void); void do_something

static vs extern “C”/“C++”

亡梦爱人 提交于 2019-12-17 06:35:38
问题 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 lucky or... class X { public: static void proxy(int i) {} } makecontext(..., (void (*)(void)) X::proxy, ...); vs extern "C" void proxy(int i) {} makecontext(..., (void (*)(void))