function-pointers

How to format a function pointer?

喜你入骨 提交于 2019-11-26 13:21:17
Is there any way to print a pointer to a function in ANSI C? Of course this means you have to cast the function pointer to void pointer, but it appears that's not possible?? #include <stdio.h> int main() { int (*funcptr)() = main; printf("%p\n", (void* )funcptr); printf("%p\n", (void* )main); return 0; } $ gcc -ansi -pedantic -Wall test.c -o test test.c: In function 'main': test.c:6: warning: ISO C forbids conversion of function pointer to object pointer type test.c:7: warning: ISO C forbids conversion of function pointer to object pointer type $ ./test 0x400518 0x400518 It's "working", but

How to call through a member function pointer?

安稳与你 提交于 2019-11-26 13:18:52
I'm trying to do some testing with member function pointer. What is wrong with this code? The bigCat.*pcat(); statement doesn't compile. class cat { public: void walk() { printf("cat is walking \n"); } }; int main(){ cat bigCat; void (cat::*pcat)(); pcat = &cat::walk; bigCat.*pcat(); } More parentheses are required: (bigCat.*pcat)(); ^ ^ The function call ( () ) has higher precedence than the pointer-to-member binding operator ( .* ). The unary operators have higher precedence than the binary operators. 来源: https://stackoverflow.com/questions/12189057/how-to-call-through-a-member-function

C function pointer casting to void pointer

不问归期 提交于 2019-11-26 12:28:49
I am trying to run the following program but getting some strange errors: File 1.c: typedef unsigned long (*FN_GET_VAL)(void); FN_GET_VAL gfnPtr; void setCallback(const void *fnPointer) { gfnPtr = *((FN_GET_VAL*) (&fnPointer)); } File 2.c: extern FN_GET_VAL gfnPtr; unsigned long myfunc(void) { return 0; } main() { setCallback((void*)myfunc); gfnPtr(); /* Crashing as value was not properly assigned in setCallback function */ } Here the gfnPtr() is crashing on 64-Bit suse linux when compiled with gcc. But it successfully calling gfnPtr() VC6 and SunOS. But if I change the function as given below

c++/cli pass (managed) delegate to unmanaged code

风流意气都作罢 提交于 2019-11-26 12:19:23
How do I pass a function pointer from managed C++ (C++/CLI) to an unmanaged method? I read a few articles, like this one from MSDN , but it describes two different assemblies, while I want only one. Here is my code: 1) Header (MyInterop.ManagedCppLib.h): #pragma once using namespace System; namespace MyInterop { namespace ManagedCppLib { public ref class MyManagedClass { public: void DoSomething(); }; }} 2) CPP Code (MyInterop.ManagedCppLib.cpp) #include "stdafx.h" #include "MyInterop.ManagedCppLib.h" #pragma unmanaged void UnmanagedMethod(int a, int b, void (*sum)(const int)) { int result = a

Do distinct functions have distinct addresses?

百般思念 提交于 2019-11-26 11:46:13
Consider these two functions: void foo() {} void bar() {} is it guaranteed that &foo != &bar ? Similarly, template<class T> void foo() { } is it guaranteed that &foo<int> != &foo<double> ? There are two linkers I know of that fold function definitions together. MSVC aggressively COMDAT folds functions, so two functions with the same implementation can be turned into one function. As a side effect, the two functions share the same address. I was under the impression that this was illegal, but I cannot find where in the standard it is made illegal. The Gold linker also folds functions, with both

Why can&#39;t I cast a function pointer to (void *)?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 11:27:43
问题 I have a function that takes a string, an array of strings, and an array of pointers, and looks for the string in the array of strings, and returns the corresponding pointer from the array of pointers. Since I use this for several different things, the pointer array is declared as an array of (void *), and the caller should know what kind of pointers are actually there (and hence what kind of a pointer it gets back as the return value). When I pass in an array of function pointers, however, I

Where are member functions stored for an object?

我是研究僧i 提交于 2019-11-26 11:12:34
问题 I\'m experimenting with C++ to understand how class/structures and their respective objects are laid out in memory and I understood that each field of a class/structure is an offset into their respective object (so I can have a member variable pointer). I don\'t understand why, even if I can have member function pointers, the following code doesn\'t work: struct mystruct { void function() { cout << \"hello world\"; } int c; }; int main() { unsigned int offset_from_start_structure = (unsigned

Does Function pointer make the program slow?

浪子不回头ぞ 提交于 2019-11-26 10:26:18
问题 I read about function pointers in C. And everyone said that will make my program run slow. Is it true? I made a program to check it. And I got the same results on both cases. (measure the time.) So, is it bad to use function pointer? Thanks in advance. To response for some guys. I said \'run slow\' for the time that I have compared on a loop. like this: int end = 1000; int i = 0; while (i < end) { fp = func; fp (); } When you execute this, i got the same time if I execute this. while (i < end

C++ Using Class Method as a Function Pointer Type

爷,独闯天下 提交于 2019-11-26 09:57:59
问题 In a C lib, there is a function waiting a function pointer such that: lasvm_kcache_t* lasvm_kcache_create(lasvm_kernel_t kernelfunc, void *closure) where lasvm_kernel_t is defined as: typedef double (*lasvm_kernel_t)(int i, int j, void* closure); Now, if I send a method defined in a class to lasvm_kcache_create: double cls_lasvm::kernel(int i, int j, void *kparam) ... lasvm_kcache_t *kcache=lasvm_kcache_create(&kernel, NULL); I get: \"cannot convert ‘double (cls_lasvm:: )(int, int, void )’ to

Get function pointer from std::function when using std::bind

断了今生、忘了曾经 提交于 2019-11-26 09:44:36
问题 I\'m trying to use std::function in conjunction with std::bind , but I\'m having some problems. This works: #include <functional> #include <iostream> void print() { std::cout << 2; } int main() { std::function<void ()> foo = print; (*foo.target<void (*)()>())(); //prints 3 } This crashes at the second line of main : #include <functional> #include <iostream> void print (int i) { std::cout << i; } int main() { std::function<void ()> foo = std::bind (print, 2); (*foo.target<void (*)()>())(); } I