What is the meaning of following statement.
((void*(*)(void*))keepfunc)(val)
Note:- In general I have problem understanding such expressions. Could, someone
Please suggest me some good material(web or book) for this?
You should try "parsing" the expression from inside out:
void*(*)(void*)
function pointer to a function expecting a pointer of void and returnning a pointer of void
(void*(*)(void*))keepfunc
cast to a function pointer expecting a pointer of void and returnning a pointer of void
((void*(*)(void*))keepfunc)(val)
This should be wrong, since a function pointer must be dereferenced before it can be called. It should look something like this:
(*(void*(*)(void*))keepfunc)(val)
Was this a working example from a textbook on C programming?
This is a cast that casts keepfunc to a function pointer that accepts a void* parameter and returns a void* result value, and then evaluates the function by passing val as the parameter (although, i think there should be an asterisk before keepfunc, because you need to deference the function pointer before calling it). I think you should just look up function pointers in google, that should clear things up.
Related
void(* resetDevice) (void) = 0;
This was a function declaration I stumbled upon, moreover no definition found later.
What kind of a function is this? What does it do? What does it return?
This is pointer to void function accepting no parameters. Pointer is named resetDevice and is initialized to null. Some library code later will initialize it to point to real function. You will be able to call it with
resetDevice();
Now, what will that function do exactly, I have no idea - it depends on your environment/libraries, which you have not specified. My guess would be it is Arduino, you can read bit more about it here
http://forum.arduino.cc/index.php?topic=385427.0
I use this piece of code to reset my Arduino board.
As I found it, it is not a really elegant method to reset the board, but it works.
If you call "resetDevice()" while it is pointing to address 0 (NULL), it will cause a runtime error or "null pointer exception" as the pointer is not pointing to a valid memory location containing a function. And that error causing a board reboot.
Why proc which is a function pointer gets null value?
EDITED:
My guess would be that the library you loaded doesn't export a function called "StartHook".
If the library is written in C++, which it looks like it is, the function name will be mangled based on its argument types (e.g, to something like _Z9StartHookv). Wrap the definition in extern "C" { ... } to prevent this.
NULL is the documented return value for when the function fails. To get the reason, call GetLastError and look it up here.
I was looking at a program in IDA as i was trying to figure out how a certain function worked, when I came across something like this:
; C_TestClass::Foo(void)
__text:00000000 __ZN14C_TestClass7FooEv proc near
__text:00000000 jmp __ZN14C_TestClass20Barr ; C_TestClass::Barr(void)
__text:00000000 __ZN14C_TestClass7FooEv endp
__text:00000000
Can anyone explain to me what exactly jumping to a function would do in a case like this?
I am guessing that it acts as a wrapper for the other function?
You are correct that jumping is often a way to efficiently handle wrapper functions that aren't inlined.
Normally, you'd have to read all the function parameters and push them back onto the stack before you can call the sub-function.
But when the wrapper function has the exact same prototype:
Same calling convention
Same parameters (and in the same order)
Same return type
there's no need for all the usual function calling overhead. You can just jump directly to the target. (These categories may not be entirely necessary, as it may still be possible in some other cases.)
All the parameters (either on the stack or register) that were setup when calling the wrapper function will already be in place (and compatible) for the sub-function.
This is what's known as a tail-call. The compiler is able to invoke the function directly and let it return to the original caller without causing any problems. For example:
int f(int x)
{
...
return g(y);
}
The compiler can simply jmp g at the end because there is room for the arguments in the same place as f's arguments and the return value is not modified before returning to f's caller.
For example,
if I have
deque<int> a;
and I want to use a function pointer to a.front() , a.back() and a.push_front(), a.push_back(), can I accomplish that? If yes, how?
Pointers to member functions are a different type of function pointer than regular pointers, due to needing to pass this. The best solution is to implement a wrapper function as such:
template<typename T>
T deque_front(std::deque<T> *q) {
return q->front();
}
To call a (non-static) member function, you need an object to call it on (as well as the parameters of the function). This is implemented by having the member function take a (hidden)
extra parameter, this (effectively).
If you're using c++11 you can write a lambda to do this, or use std::bind to bind an instance of a deque and a member function together.
In case of lack of proper updated tutorials for some particular library functions (in my case, latest allegro5), how can one learn by oneself how to call and use those functions? Is there some clue in header files?
thanks in advance
The header files are going to provide you with the bare minimum information required to correctly compile a program with those functions. It has the types, constants, and function prototypes. Nothing (short of comments) is going to explain how to correctly use the functions, just how to call them.
General
For example, if you see:
int do_something(int n, const char* desc);
You can only infer that you need to pass an integer n and a (C) string desc. That function returns an integer as well.
For a more complex example:
typedef struct {
int foo;
double bar;
} blam_t;
void munge(blam_t info);
You know that munge takes one argument of type blam_t which is a custom structure, as defined above. You could use that to create a blam_t variable and pass it to munge():
blam_t myvar;
myvar.foo = 42;
myvar.bar = 0.67;
munge(myar);
Allegro5
If we look at the source of include/allegro5/display.h we see things like this:
AL_FUNC(void, al_set_new_display_flags, (int flags));
This is an uncommon way of defining functions. They are using a macro AL_FUNC to define their functions. We see (by clicking on it) that AL_FUNC is defined as:
#define AL_FUNC(type, name, args) type name args
So that first example basically becomes:
void al_set_new_display_flags(int flags);
And we can call it with just an integer argument.
Without any documentation, you can only hope to learn by trying the functions. Then this becomes more a reverse engineering task.