function-pointers

Linux Kernel - why a function's address in System.map is one byte preceding its address as seen in real time?

旧街凉风 提交于 2019-11-27 12:27:01
问题 In linux kernel source code, added this lines in tasklet_action code: printk("tasklet_action = %p\n" , *tasklet_action); printk("tasklet_action = %p\n" , &tasklet_action); printk("tasklet_action = %p\n" , tasklet_action); In the output I get: tasklet_action = c03441a1 tasklet_action = c03441a1 tasklet_action = c03441a1 But when searching it in the system.map file the tasklet_action address is at c03441a0 so there is an offset of 1 byte. Why do I have this offset? Is it always an one byte

Dynamic method dispatching in C

落爺英雄遲暮 提交于 2019-11-27 11:58:36
I know it sounds silly and I know that C is not an Object Oriented Language. But is there any way that dynamic method dispatching can be achieved in C? I thought about function pointers but don't get the entire idea. How could I implement this? As others have noted, it is certainly possible to implement this in C. Not only is it possible, it is a fairly common mechanism. The most commonly used example is probably the file descriptor interface in UNIX. A read() call on a file descriptor will dispatch to a read function specific to the device or service that provided that file descriptor (was it

Function pointer vs Function reference

﹥>﹥吖頭↗ 提交于 2019-11-27 11:49:45
In the code below, function-pointer and what i considered as "function-reference" seems to have identical semantics: #include <iostream> using std::cout; void func(int a) { cout << "Hello" << a << '\n'; } void func2(int a) { cout << "Hi" << a << '\n'; } int main() { void (& f_ref)(int) = func; void (* f_ptr)(int) = func; // what i expected to be, and is, correct: f_ref(1); (*f_ptr)(2); // what i expected to be, and is not, wrong: (*f_ref)(4); // i even added more stars here like (****f_ref)(4) f_ptr(3); // everything just works! // all 4 statements above works just fine // the only difference

Why do we use std::function in C++ rather than the original C function pointer? [duplicate]

谁说胖子不能爱 提交于 2019-11-27 11:46:51
This question already has an answer here: Should I use std::function or a function pointer in C++? 5 answers What is the advantage of std::function<T1(T2)> over the original T1 (*)(T2) ? std::function can hold more than function pointers, namely functors . #include <functional> void foo(double){} struct foo_functor{ void operator()(float) const{} }; int main(){ std::function<void(int)> f1(foo), f2((foo_functor())); f1(5); f2(6); } Live example on Ideone. As the example shows, you also don't need the exact same signature, as long as they are compatible (i.e., the parameter type of std::function

What is the address of a function in a C++ program?

喜欢而已 提交于 2019-11-27 11:08:19
问题 As the function is set of instruction stored in one contiguous block of memory. And address of a function (entry point) is the address of the first instruction in the function. (from my knowledge) And thus we can say that the address of function and the address of the first instruction in the function will be the same (In this case the first instruction is the initialization of a variable.). But the program below contradicts the above line. code: #include<iostream> #include<stdio.h> #include

Returning function pointer type

∥☆過路亽.° 提交于 2019-11-27 10:55:27
Often I find the need to write functions which return function pointers. Whenever I do, the basic format I use is: typedef int (*function_type)(int,int); function_type getFunc() { function_type test; test /* = ...*/; return test; } However this can get cumbersome when dealing with a large number of functions so I would like to not have to declare a typedef for each one (or for each class of functions) I can remove the typedef and declare the local variable returned in the function as: int (*test)(int a, int b); making the function body look like this: { int (*test)(int a, int b); test /* = ...

C syntax for functions returning function pointers

我们两清 提交于 2019-11-27 10:10:39
Consider the following typedefs : typedef int (*f1)(float); typedef f1 (*f2)(double); typedef f2 (*f3)(int); f2 is a function that returns a function pointer. The same with f3 , but the type of the function, the pointer to which f3 returns, is f2 . How can I define f3 without the typedefs? I know typedefs are the cleaner and easier to understand way to define f3 . However, my intention here is to understand C syntax better. Start with your declaration for f1 : int (*f1)(float); You want f2 to be a pointer to a function returning f1 , so substitute f1 in the declaration above with the

What's the syntax for declaring an array of function pointers without using a separate typedef?

萝らか妹 提交于 2019-11-27 09:58:42
问题 Arrays of function pointers can be created like so: typedef void(*FunctionPointer)(); FunctionPointer functionPointers[] = {/* Stuff here */}; What is the syntax for creating a function pointer array without using the typedef ? 回答1: arr //arr arr [] //is an array (so index it) * arr [] //of pointers (so dereference them) (* arr [])() //to functions taking nothing (so call them with ()) void (* arr [])() //returning void so your answer is void (* arr [])() = {}; But naturally, this is a bad

C isn't that hard: void ( *( *f[] ) () ) ()

删除回忆录丶 提交于 2019-11-27 09:57:41
I just saw a picture today and think I'd appreciate explanations. So here is the picture: I found this confusing and wondered if such codes are ever practical. I googled the picture and found another picture in this reddit entry, and here is that picture: So this "reading spirally" is something valid? Is this how C compilers parse? It'd be great if there are simpler explanations for this weird code. Apart from all, can these kind of codes be useful? If so, where and when? There is a question about "spiral rule", but I'm not just asking about how it's applied or how expressions are read with

How to print the address of a function?

别说谁变了你拦得住时间么 提交于 2019-11-27 09:13:44
I let gcc compile the following example using -Wall -pedantic : #include <stdio.h> int main(void) { printf("main: %p\n", main); /* line 5 */ printf("main: %p\n", (void*) main); /* line 6 */ return 0; } I get: main.c:5: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘int (*)()’ main.c:6: warning: ISO C forbids conversion of function pointer to object pointer type Line 5 made my change the code like in line 6. What am I missing to remove the warning when printing a function's address? R.. This is essentially the only portable way to print a function pointer. size_t i; int (