function-pointers

Python ctypes: How to modify an existing char* array

时光总嘲笑我的痴心妄想 提交于 2019-12-19 05:57:23
问题 I'm working on a Python application that makes use of libupnp which is a C library. I'm using CTypes to use the library which is easy enough. The problem I'm having is when I'm registering a callback function for read requests. The function has a prototype of the following form: int read_callback(void *pFileHandle, char *pBuf, long nBufLength); pFileHandle is just some file handle type. pBuf is a writable memory buffer. This is where the data is output. nBufLength is the number of bytes to

static constexpr pointer-to-function, difference between compilers

匆匆过客 提交于 2019-12-19 05:45:41
问题 When answering this question, I tried the following code with gcc (code compiled) and clang (code rejected): typedef long (*func)(int); long function(int) { return 42; } struct Test { static constexpr func f = &function; }; template<func c> struct Call { static void f() { c(0); } }; int main() { Call<Test::f>::f(); } I am not sure which compiler is right, although I think the constexpr initialization of Test::f is ok. The error clang outputs is: error: non-type template argument for template

Function pointers with default parameters in C++

纵饮孤独 提交于 2019-12-18 18:37:15
问题 How does C++ handle function pointers in relation to functions with defaulted parameters? If I have: void foo(int i, float f = 0.0f); void bar(int i, float f); void (*func_ptr1)(int); void (*func_ptr2)(int, float); void (*func_ptr3)(int, float = 10.0f); Which function pointers can I use in relation to which function? 回答1: Both foo() and bar() can only be assigned to func_ptr2 . §8.3.6/2 : A default argument is not part of the type of a function. [Example: int f(int = 0); void h() { int j = f

Tool to decipher C/C++ function pointer typedefs

北城以北 提交于 2019-12-18 13:36:49
问题 I remember once seeing a website, which deciphered complex C++ typedefs including function pointers returning function pointers to functions which return an array with … It would turn such definitions into readable English text: »Pointer to function, returning an array of int-pointers, taking a long and a callback function (taking an int) as parameters«. (something along that lines) typedef int * (*f)(long, void (*)(int))[]; Anybody remember that tool/website? Any hints greatly appreciated

Python, how to pass an argument to a function pointer parameter?

北城以北 提交于 2019-12-18 12:45:09
问题 I only just started learning Python and found out that I can pass a function as the parameter of another function. Now if I call foo(bar()) it will not pass as a function pointer but the return value of the used function. Calling foo(bar) will pass the function, but this way I am not able to pass any additional arguments. What if I want to pass a function pointer that calls bar(42) ? I want the ability to repeat a function regardless of what arguments I have passed to it. def repeat(function,

If ampersands aren't needed for function pointers, why does boost::bind require one?

我与影子孤独终老i 提交于 2019-12-18 12:32:08
问题 I've always believed that function pointers don't require an ampersand: Do function pointers need an ampersand Yet, every example I've seen of using boost::bind shows one, and my compiler - in most situations - gives a typically inscrutable error message if it's omitted. synchronize(boost::bind(&Device::asyncUpdate , this, "ErrorMessage")); // Works synchronize(boost::bind(Device::asyncUpdate , this, "ErrorMessage")); // Fails Am I wrong in assuming that boost::bind 's first parameter is

Implementing a generical 'map' function over arrays in C

不打扰是莪最后的温柔 提交于 2019-12-18 12:26:38
问题 I'm having difficulties implementing a generic 'map' function over arrays. I started with the following draft: void MapArray(void * src, void * dest, void * (f)(void *), size_t n, size_t elem) { unsigned int i = 0, j = 0; void * temp = malloc(elem); for(i = 0; i<n, i++) { temp = (f)((char *) src) + i)); for(j = 0; j < elem; j++) { *(((char *) dest) + i) = *(((char *) temp) + i); } } free(temp); } I understand why it's not correct - i'm casting to (char *) before giving it to 'f' - but i'm now

“this” pointer in C (not C++)

不想你离开。 提交于 2019-12-18 10:46:09
问题 I'm trying to create a stack in C for fun, and came up with the idea of using struct to represent the stack. Then I add function pointers to the struct for push() and pop() operations. So far all is good it seems, but, for the implementation of the push() and pop() functions I need to refer to *this somehow. How can that (can it?) be done? This is my struct struct Stack { int *data; int current_size; int max_size; int (*push)(int); int (*pop)(); }; And as an example here's push int push(int

The Benefits of Using Function Pointers

和自甴很熟 提交于 2019-12-18 10:37:11
问题 I have been programming for a few years now and have used function pointers in certain cases. What I would like to know is when is it appropriate or not to use them for performance reasons and I mean in the context of games, not business software. Function pointers are fast, John Carmack used them to the extent of abuse in the Quake and Doom source code and because he is a genius :) I would like to use function pointers more but I want to use them where they are most appropriate. These days

calling code stored in the heap from vc++

梦想与她 提交于 2019-12-18 10:17:29
问题 Imagine I am doing something like this: void *p = malloc (1000); *((char*)p) = some_opcode; *((char*)p+1) = another_opcode; // for the sake of the example: the opcodes are ok .... etc... How can I define a function pointer to call p as if it was a function? (i'm using VC++ 2008 express). Thanks 回答1: A comment wasn't enough space. Joe_Muc is correct. You should not stuff code into memory obtained by malloc or new . You will run into problems if you change the page properties of pages that