function-pointers

Replace a function pointer in a shared library with ctypes

时光怂恿深爱的人放手 提交于 2020-07-28 04:49:12
问题 I am trying to replace an existing function pointer in a shared library with a callback defined in Python, through ctypes. The source of the shared library in C: #include <assert.h> #include <stdio.h> void (*plot)(); int c_main(int argc, void** argv) { printf("plot is %p\n", (void*)plot); assert(plot != NULL); plot(); return 0; } The source of the Python script: from sys import platform from pathlib import Path import ctypes import _ctypes FUNCTYPE = ctypes.WINFUNCTYPE if platform == 'win32'

How can I expose C++ function pointers in C?

不羁岁月 提交于 2020-07-03 07:06:13
问题 I have two types of function pointers defined in my C++ that look like this: typedef void(*CallbackFn)(bool, std::string, py::array_t<uint8_t>&); typedef std::function<void(std::string)> LogFunction; Class Core{ ... void myfunc1(LogFunction lg1, CallbackFn callback, int x, std::string y); }; and I want to be able to expose them in C but I can't seem to find a way to do so. My first try was to cast these as void* and then recast them back to their actual type. but this seems like a bad idea.

How can I expose C++ function pointers in C?

≯℡__Kan透↙ 提交于 2020-07-03 07:05:31
问题 I have two types of function pointers defined in my C++ that look like this: typedef void(*CallbackFn)(bool, std::string, py::array_t<uint8_t>&); typedef std::function<void(std::string)> LogFunction; Class Core{ ... void myfunc1(LogFunction lg1, CallbackFn callback, int x, std::string y); }; and I want to be able to expose them in C but I can't seem to find a way to do so. My first try was to cast these as void* and then recast them back to their actual type. but this seems like a bad idea.

function pointer with Eigen

為{幸葍}努か 提交于 2020-06-29 03:38:17
问题 I am pretty competent with Python, but I'm pretty new to C++ and things like pointers. I try to write some codes for solving ODE with the Eigen package for linear algebra (I will need to deal with lots of matrices later, so I plan to start with it). I have the following code for RK4 and they work: #include "../eigen-eigen-b3f3d4950030/Eigen/Dense" using namespace Eigen; VectorXd Func(const VectorXd& a) { // equations for solving simple harmonic oscillator Vector2d ans; ans(0) = a(1); // dy/dt

function pointer with Eigen

大城市里の小女人 提交于 2020-06-29 03:37:49
问题 I am pretty competent with Python, but I'm pretty new to C++ and things like pointers. I try to write some codes for solving ODE with the Eigen package for linear algebra (I will need to deal with lots of matrices later, so I plan to start with it). I have the following code for RK4 and they work: #include "../eigen-eigen-b3f3d4950030/Eigen/Dense" using namespace Eigen; VectorXd Func(const VectorXd& a) { // equations for solving simple harmonic oscillator Vector2d ans; ans(0) = a(1); // dy/dt

Overwriting function pointer raises error: non-object type is not assignable

五迷三道 提交于 2020-06-28 05:05:39
问题 I'm currently working on a demo to monkey patch c function calls, the idea is that you have a shared c file (eg: lib.c ), which has one exported function called void foo() in the header file lib.h . Currently I'm trying to do the following: #include <stdio.h> #include "lib.h" void (*foo_original)() = NULL; void foo_patch() { puts("Before foo!"); (*foo_original)(); puts("Before foo!"); } int main() { foo_original = &foo; foo = &foo_patch; // Somewhere else in the code foo(); } However this

Casting an address of subroutine into void pointer

佐手、 提交于 2020-06-27 17:09:04
问题 Is it okay to cast function location with void pointer though function pointers size is not always the same as opaque pointer size? I already did search about opaque pointers , and casting function pointers . I found out function pointers and normal pointers are not the same on some systems. void (*fptr)(void) = (void *) 0x00000009; // is that legal??? I feel I should do this void (*fptr)(void)= void(*)(void) 0x00000009; It did work fine , though I expected some errors or at least warnings I

Casting an address of subroutine into void pointer

删除回忆录丶 提交于 2020-06-27 17:07:07
问题 Is it okay to cast function location with void pointer though function pointers size is not always the same as opaque pointer size? I already did search about opaque pointers , and casting function pointers . I found out function pointers and normal pointers are not the same on some systems. void (*fptr)(void) = (void *) 0x00000009; // is that legal??? I feel I should do this void (*fptr)(void)= void(*)(void) 0x00000009; It did work fine , though I expected some errors or at least warnings I

Why can function pointers be `constexpr`?

不打扰是莪最后的温柔 提交于 2020-06-24 07:06:22
问题 How does the compiler know where in memory the square root will be before the program is executed? I thought the address would be different everytime the program is executed, but this works: constexpr double(*fp)(double) = &sqrt; cout << fp(5.0); Is it because the address is relative to another address in memory? I don't think so because the value of fp is large: 0x720E1B94. 回答1: How does the compiler know where in memory the square root will be before the program is executed? The tool chain

Is it undefined behavior to “trash” a return value by casting the function pointer to a void function then calling it?

一曲冷凌霜 提交于 2020-06-17 00:09:43
问题 Say, something like this: int SayHelloThenReturnTen(void) { puts("Hello world"); return 10; } Then later: ((void(*)(void))SayHelloThenReturnTen)(); Is that safe? Is it safe to "trash" the return value by casting to a void pointer? Is this safe and cross platform, or is this undefined behavior ? 回答1: Is that safe? It is safe to cast a function pointer to any function pointer type. Casting the result back to the original type will yield a pointer value equal to the the original pointer. It is