Greetings All, I'm a little rough with how function pointers work in C. I assumed there similar to way C++ stores virtual functions internally in the vtable for virtual functions by the compiler and checks the table at runtime. Please let me know if I'm wrong in my understanding. Thanks, Nick -- Sent from my Android device with K-9 Mail. Please excuse my brevity.
Virtual functions are implemented on on the base of vtables, which in turn are implemented using function pointers. So it would be good to get a grasp on functionpointers (they exist in C++ as well), also I believe this is not a C mailing list, take a look at stackoverflow or a c/c++ site or book of your choice. If you know how a pointer works, you will find it easy to understand how a functionpointer works. On 02/04/15 05:16, Nicholas Krause wrote:
Greetings All, I'm a little rough with how function pointers work in C. I assumed there similar to way C++ stores virtual functions internally in the vtable for virtual functions by the compiler and checks the table at runtime. Please let me know if I'm wrong in my understanding. Thanks, Nick
On April 2, 2015 6:15:19 AM EDT, Malte Vesper <malte.vesper@postgrad.manchester.ac.uk> wrote:
Virtual functions are implemented on on the base of vtables, which in turn are implemented using function pointers. So it would be good to get a grasp on functionpointers (they exist in C++ as well), also I believe this is not a C mailing list, take a look at stackoverflow or a c/c++ site or book of your choice. If you know how a pointer works, you will find it easy to understand how a functionpointer works.
On 02/04/15 05:16, Nicholas Krause wrote:
Greetings All, I'm a little rough with how function pointers work in C. I assumed there similar to way C++ stores virtual functions internally in the vtable for virtual functions by the compiler and checks the table at runtime. Please let me know if I'm wrong in my understanding. Thanks, Nick
I looked in to it and the kernel seems to be one of the few places where this is done along with in line functions. Why do we need function pointers in the kernel, outside of device drivers is my real question and is there any way to do the code using them without function pointers at all, I am assuming no. Thanks, Nick -- Sent from my Android device with K-9 Mail. Please excuse my brevity.
Since I am not sure what you are refering to exactly it is hard to answer. Refer to an example maybe? Also take a look at i.e. std::sort / the compare type (you seem to be a c++ guy). Why do we need a pointer there (actually function object, slight generalization of a funciton pointer)? Why would it be helpful to define your on method to be called on wakeup (one of the places where I know fp's are used)? On 02/04/15 11:54, Nicholas Krause wrote:
On April 2, 2015 6:15:19 AM EDT, Malte Vesper <malte.vesper@postgrad.manchester.ac.uk> wrote:
Virtual functions are implemented on on the base of vtables, which in turn are implemented using function pointers. So it would be good to get a grasp on functionpointers (they exist in C++ as well), also I believe this is not a C mailing list, take a look at stackoverflow or a c/c++ site or book of your choice. If you know how a pointer works, you will find it easy to understand how a functionpointer works.
On 02/04/15 05:16, Nicholas Krause wrote:
Greetings All, I'm a little rough with how function pointers work in C. I assumed there similar to way C++ stores virtual functions internally in the vtable for virtual functions by the compiler and checks the table at runtime. Please let me know if I'm wrong in my understanding. Thanks, Nick
I looked in to it and the kernel seems to be one of the few places where this is done along with in line functions. Why do we need function pointers in the kernel, outside of device drivers is my real question and is there any way to do the code using them without function pointers at all, I am assuming no. Thanks, Nick
I looked in to it and the kernel seems to be one of the few places where this is done along with in line functions. Why do we need function pointers in the kernel, outside of device drivers is my real question and is there any way to do the code using them without function pointers at all, I am assuming no.
Suppose you are registering a pci driver, and you are telling the pci layer that function xyz() is your probe function. incidentally, there is also another driver which is also having a probe function called xyz(). if you do not give the pointer to your function and pci layer starts calling the functions by its name , then which xyz() should be executed now??? regards sudip
Thanks, Nick
On 02/04/15 12:27, Sudip Mukherjee wrote:
I looked in to it and the kernel seems to be one of the few places where this is done along with in line functions. Why do we need function pointers in the kernel, outside of device drivers is my real question and is there any way to do the code using them without function pointers at all, I am assuming no. Suppose you are registering a pci driver, and you are telling the pci layer that function xyz() is your probe function. incidentally, there is also another driver which is also having a probe function called xyz(). if you do not give the pointer to your function and pci layer starts calling the functions by its name , then which xyz() should be executed now???
regards sudip
Thanks, Nick That would not compile for the very reason of name conflict, if the functions would be in the same namespace... But the question also read "Why do we need function pointers in the kernel, outside of device drivers is "
On Thu, 02 Apr 2015 06:54:42 -0400, Nicholas Krause said:
I looked in to it and the kernel seems to be one of the few places where this is done along with in line functions. Why do we need function pointers in the kernel, outside of device drivers is my real question and is there any way to do the code using them without function pointers at all, I am assuming no.
Geez Nick, you didn't look very hard at all, did you? [/usr/src/linux-next] find include -type f | xargs egrep '^struct .*_op.*{$' | sort There's 391 of them (at least in yesterday's linux-next tree). You should *at least* know and understand 'struct file_operations', 'struct dentry_operations', 'struct inode_operations', and 'struct super_operations" if you plan to have any realistic hope of understanding the VFS - which should be important to you, as I seem to recall you were interested in btrfs2. See include/linux/fs.h for the details.
On April 2, 2015 9:13:38 AM EDT, Valdis.Kletnieks@vt.edu wrote:
On Thu, 02 Apr 2015 06:54:42 -0400, Nicholas Krause said:
I looked in to it and the kernel seems to be one of the few places where this is done along with in line functions. Why do we need function pointers in the kernel, outside of device drivers is my real question and is there any way to do the code using them without function pointers at all, I am assuming no.
Geez Nick, you didn't look very hard at all, did you?
[/usr/src/linux-next] find include -type f | xargs egrep '^struct .*_op.*{$' | sort
There's 391 of them (at least in yesterday's linux-next tree).
You should *at least* know and understand 'struct file_operations', 'struct dentry_operations', 'struct inode_operations', and 'struct super_operations" if you plan to have any realistic hope of understanding the VFS - which should be important to you, as I seem to recall you were interested in btrfs2.
See include/linux/fs.h for the details. I do know about those too, however I was just curious about areas that the kernel does this I known the vfs and drivers do this. Thanks, Nick
-- Sent from my Android device with K-9 Mail. Please excuse my brevity.
participants (4)
-
Malte Vesper -
Nicholas Krause -
Sudip Mukherjee -
Valdis.Kletnieks@vt.edu