function-pointers

C memcpy() a function

久未见 提交于 2019-12-03 16:03:09
Is there any method to calculate size of a function? I have a pointer to a function and I have to copy entire function using memcpy. I have to malloc some space and know 3rd parameter of memcpy - size. I know that sizeof(function) doesn't work. Do you have any suggestions? Functions are not first class objects in C. Which means they can't be passed to another function, they can't be returned from a function, and they can't be copied into another part of memory. A function pointer though can satisfy all of this, and is a first class object. A function pointer is just a memory address and it

Function pointers in C - address operator “unnecessary”

六月ゝ 毕业季﹏ 提交于 2019-12-03 13:59:13
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 that it is not needed before an array name." Does anyone else feel slightly uncomfortable with this (esp.

Messy function pointer interpretation

我们两清 提交于 2019-12-03 13:28:25
I happen to come across the following function pointer. char (*(*x())[])(); It looks like an array of function pointer in the following format, but I can't see what f -> (*x()) means. How to interpret this messy function pointer? char (*f[])(); ADDED With John Bode's help, I make an example as follows. #include <stdio.h> char foo() { return 'a'; } char bar() { return 'b'; } char blurga() { return 'c'; } char bletch() { return 'd'; } char (*gfunclist[])() = {foo, bar, blurga, bletch}; char (*(*x())[])() { static char (*funclist[4])() = {foo, bar, blurga, bletch}; return &funclist; } int main()

What is the difference between std::function and std::mem_fn

Deadly 提交于 2019-12-03 12:57:37
I am having trouble figuring out the difference between the two function wrappers std::function and std::mem_fn . From the description, it seems to me that std::function does everything std::mem_fn does and more. In which instance would one use std::mem_fn over std::function ? You can't really compare std::function with std::mem_fn . The former is a class template whose type you specify, and the latter is a function template with unspecified return type. There really isn't a situation in which you'd actually consider one versus the other. A better comparison might be between mem_fn and std:

How to define a general member function pointer

时间秒杀一切 提交于 2019-12-03 12:39:38
I have created a Timer class that must call a callback method when the timer has expired. Currently I have it working with normal function pointers (they are declared as void (*)(void), when the Elapsed event happens the function pointer is called. Is possible to do the same thing with a member function that has also the signature void (AnyClass::*)(void)? Thanks mates. EDIT: This code has to work on Windows and also on a real-time OS (VxWorks) so not using external libraries would be great. EDIT2: Just to be sure, what I need is to have a Timer class that take an argument at the Constructor

What's the difference between a pointer, and a pointer variable?

こ雲淡風輕ζ 提交于 2019-12-03 12:15:34
问题 I know this is very basic but it is little bit confusing to me. I've read: a pointer is nothing more than an address , and a pointer variable is just a variable that can store an address . When we store the address of a variable i in the pointer variable p , we say that p points to i . int i, *p = &i; p points to i . To gain access to the object that a pointer points to, we use the * (indirection) operator. If p is a pointer then *p represents the object to which p currently points. Now I am

Where exactly do function pointers point?

。_饼干妹妹 提交于 2019-12-03 12:00:32
Given that all the primitive data types and objects have memory allocated, it is intuitively easy to imagine the pointers to these types. But where exactly do function pointers point to? Given that instructions are converted into machine code and reside in memory, should we consider they point to the memory location corresponding to the start of the functions instructions? We face many errors in pointers due to illegal memory access. Is it the case that errors occur when function pointers point to data memory instead of instruction memory? Function pointer also point into memory, the only

c++ pointers to operators

空扰寡人 提交于 2019-12-03 11:29:57
I want to write a pointer in c++ (or in c++0x), that will points to a operator of a class lets say A or B. Is there any method to do it? Of course there is a syntax like int (A::*_p) (); but it doesn't solve this problem. I want to make general pointer, not specifying the base class for it - only pointer for "operator function" #include <thread> #include <iostream> using namespace std; class A { public: int operator()() { return 10; } }; class B { public: int operator()() { return 11; } }; int main() { A a; int (*_p) (); _p = a.operator(); cout << _p(); B b; _p = b.operator(); cout << _p(); }

Static call graph generation for the Linux kernel

夙愿已清 提交于 2019-12-03 08:56:11
问题 I'm looking for a tool to statically generate a call graph of the Linux kernel (for a given kernel configuration). The generated call graph should be "complete", in the sense that all calls are included, including potential indirect ones which we can assume are only done through the use of function pointers in the case of the Linux kernel. For instance, this could be done by analyzing the function pointer types: this approach would lead to superfluous edges in the graph, but that's ok for me.

How do I get the name of the calling function?

烈酒焚心 提交于 2019-12-03 08:44:38
问题 I am using gnu tool chain. How can I, at run time, find caller of a function? i.e for example function B() gets called by many functions using function pointers. Now, whenever B gets called, I want to print the callers name. I need this for debugging a certain issue. 回答1: If you're using GNU, you can use the backtrace functions. There's an example of the use on that man page. 回答2: The code location of the call to your function is kept by gcc in the __builtin_return_address() intrinsic. To