function-pointers

How to implement a “private/restricted” function in C?

ⅰ亾dé卋堺 提交于 2019-12-03 02:42:30
问题 I was asked a very interesting question during a C interview: How can you implement a function f() in such a way that it can only be called from a particular g() function. If a function other than g() tries to call f() it would result in a compiler error. At first, I though this could be done with function pointers and I could get close to blocking the call at runtime. But I was not able to think of a compile time strategy. I don't even know if this is possible using ansi C. Does anyone have

Static call graph generation for the Linux kernel

无人久伴 提交于 2019-12-02 22:48:02
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. ncc seems to implement this idea, however I didn't succeed in making it work on the 3.0 kernel. Any

Declaring a function pointer returning an array

做~自己de王妃 提交于 2019-12-02 22:39:07
问题 For practice, I'm trying to : Declare fubar to be a pointer to a function that takes a pointer to a char and returns a pointer to an array of 24 elements where each element is a pointer to a struct foo . My logic is: -fubar is a pointer to a function taking a char pointer: (*fubar)(char*) -...returning a pointer to an array of 24 elems of where each elem is a struct foo: (struct foo *)(*fubar)(char*)[24] Is my logic correct? 回答1: After fixing the syntax error and removing the parentheses

How do I get the name of the calling function?

房东的猫 提交于 2019-12-02 22:33:36
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. If you're using GNU, you can use the backtrace functions. There's an example of the use on that man page. The code location of the call to your function is kept by gcc in the __builtin_return_address() intrinsic. To retrieve the name for that, you have to parse the program's symbol table; while that is possible to do, via dladdr()

Pass instance method as function pointer to C Library

会有一股神秘感。 提交于 2019-12-02 20:34:28
I am writing an Objective-C application that uses a C Library. The issue which i am currently facing is that the C Library has a structure where some field are function pointers later used as callbacks. How can i convert an Objective-C instance method to a function pointer and pass it to the library? You will need to provide the C callback function within the Objective-C class implementation file, and this will only work if the callback is able to use a context pointer of some sort. So imagine the C callback signature is like this: void myCallback(void *context, int someOtherInfo); Then within

Why can I invoke a function via a pointer with too many arguments?

折月煮酒 提交于 2019-12-02 20:27:49
Say I have this function: int func2() { printf("func2\n"); return 0; } Now I declare a pointer: int (*fp)(double); This should point to a function that takes a double argument and returns an int . func2 does NOT have any argument, but still when I write: fp = func2; fp(2); (with 2 being just an arbitrary number), func2` is invoked correctly. Why is that? Is there no meaning to the number of parameters I declare for a function pointer? Yes, there is a meaning. In C (but not in C++), a function declared with an empty set of parentheses means it takes an unspecified number of parameters. When you

What is the difference between delegate in c# and function pointer in c++? [duplicate]

余生颓废 提交于 2019-12-02 20:27:26
This question already has answers here : are there function pointers in c#? (5 answers) Possible Duplicate: are there function pointers in c#? I'm interested in finding the difference between delegate in C# and function pointer in C++. Mike Christensen A delegate in C# is a type-safe function pointer with a built in iterator. It's guaranteed to point to a valid function with the specified signature (unlike C where pointers can be cast to point to who knows what). It also supports the concept of iterating through multiple bound functions. In C#, delegates are multi-cast meaning they can iterate

Why is Taking the Address of a Function That is Declared Only Working?

痴心易碎 提交于 2019-12-02 20:06:31
问题 I've asked a question here about whether taking the address of a function forces the compilation of said function specifically with regard to Substitution-Failure-Is-Not-An-Error. The most direct answer to this can be found here: Informally, an object is odr-used if its address is taken, or a reference is bound to it, and a function is odr-used if a function call to it is made or its address is taken. If an object or a function is odr-used, its definition must exist somewhere in the program;

Vector of pointer to member functions

我是研究僧i 提交于 2019-12-02 19:43:05
问题 I'm trying to write a program which creates a class that contains vector of pointers to member functions, with add() and remove() member functions. The code I wrote is - 1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 5 typedef void(*classFuncPtr)(); 6 7 class FunctionVectors 8 { 9 private: 10 vector<classFuncPtr> FunctionPointerVector; 11 public: 12 FunctionVectors(){} 13 void add(classFuncPtr funcPtr); 14 void remove(int index); 15 void run(); 16 void a(){cout<<"a: Why

Determining to which function a pointer is pointing in C?

谁说胖子不能爱 提交于 2019-12-02 18:42:04
I have a pointer to function, assume any signature. And I have 5 different functions with same signature. At run time one of them gets assigned to the pointer, and that function is called. Without inserting any print statement in those functions, how can I come to know the name of function which the pointer currently points to? nos You will have to check which of your 5 functions your pointer points to: if (func_ptr == my_function1) { puts("func_ptr points to my_function1"); } else if (func_ptr == my_function2) { puts("func_ptr points to my_function2"); } else if (func_ptr == my_function3) {