4 ms·
Wouldn't you get a compiler error if there was a mismatch?
by TwoBit 11y ago
Wouldn't you get a compiler error if there was a mismatch?
- seba_dos1 11y agoNot necessarily. For instance, you won't get it on accessing the function pointer with dlopen/dlsym.
- delinka 11y agoAnd that's because dlsym takes a void * - attempting to teach dlsym about every function signature (e.g. imagine it were C++ and could be genericized...) would be monstrous.
- seba_dos1 11y agoOf course, so that's where typedefed declaration may be handy - since compiler doesn't know what is going to be dlsymed, it won't tell you when the signature won't match.
- delinka 11y agoI'm currently involved in function interposing. I'm interposing many functions with many different signatures, but I still need some housekeeping for each function that I interpose. The housekeeping struct has pointers for both the original function and the replaced function. These members are necessarily typed as void . Later, when I want to call the original function, I assign to a properly typed variable to the value of the void original_function member. The compiler has no way to know if that void * is actually FILE (fopen )(char , char ) or if it's int(open )(char , int, ...). When everything can be properly typed, I only ever get warnings about assigning the wrong function types to the wrong function pointer variables.
- Gibbon1 11y agoThe potential trouble is if the function pointer goes through a void* wormhole. (All of these types will be lost... like...) In C you can cast a pointer, okay anything really, to a function pointer and then call it. int foo(int a, int b){return a+b; } void *vptr = foo; int (*yolo)(int a) = vptr; int c = yolo(10); // Cthulhu come This sort of thing can happen when you pass a function pointer into some sort of callback mechanism+. When you get it back you have to cast it to the right type and then call it. So there is a disconnect between the cast and the function definition, unless you tie both together with a typedef. + Often helpful for an api that takes a callback to also record, who the caller was and some arbitrary bit of data. The arbitary bit of data might be a function pointer.
- jcranmer 11y agoActually, casting between void * and void (*)() is not legal C. You can convert between pointers of different object types, and you can convert between pointers of different function types, but you're not allowed to convert between a pointer of object type and pointer of function type (c.f. N1570, §J.5.4 (which makes it explicit) and §6.3.2.3 (where it's implicit by omission)). This is however a very common extension allowed in most compilers, since most processors that people encounter keep code and data in the same memory address pool. But on DSPs, it's common for data and code memory to be distinct address spaces, and the two pointer types aren't necessarily the same size.
- marcosdumay 11y agoDepends on how complex is your code. If you try to cast a pointer into your function type, you'll get a runtime error, not a compile time one.