Call function by known address? C++ [duplicate] - c++

This question already has answers here:
Calling a function through its address in memory in c / c++
(6 answers)
Closed 5 years ago.
I have a function at a known memory address(for example: 0x11111111). The function returns an int, and takes a uint32_t pointer as its only argument.
How would I call this function using c++? I have seen a few examples of calling a function by its address, but I can't seem to find one that takes a pointer as its argument
EDIT:I seen that. Doesn't address how to call the function that takes a pointer as an argument

If you’re sure that there’s a function there, you could call it by casting the address to a function pointer of the appropriate type, then calling it. Here’s C code to do this:
typedef int (*FunctionType)(uint32_t*);
FunctionType function = (FunctionType)0x11111111;
function(arg);
This can easily be modified to support any number of function arguments and any return type you’d like. Just tweak the argument types list of the FunctionType typedef.
Or, in one line (gulp):
(((int (*)(uint32_t *)) 0x11111111)(arg);

Related

Casting a function pointer in a Class [duplicate]

This question already has answers here:
How can I pass a member function where a free function is expected?
(9 answers)
How can I pass a member function pointer into a function that takes a regular function pointer?
(4 answers)
Closed 9 months ago.
I'm having problems casting a function pointer.
It works (no cast needed) outside a class.
Here is the signature of the function I'm calling.
Result* FancyClass::callMe(void(*functionPointer)())
It works with.
void defaultState()
{
//
}
// ..
Result *result= instance.callMe(defaultState);
But it does not work with
void MyClass::defaultState()
{
//
}
// ..
Result *result= instance.callMe(MyClass::defaultState);
I am getting this:
argument of type "void (MyClass::)()" is incompatible with parameter of type "void ()()"
How to cast this correctly?
You can't cast it correctly, because pointers to member functions are different from pointers to regular functions. For starters, you must write &MyClass::defaultState, and the parameter has to be of type void(*MyClass::functionPointer)()
You can't even store &MyClass::defaultState directly in a std::function<void()>. What object would you call it on? But you could bind an instance, and store the bound result in a std::function<void()>.
The question was tagged "C", but C doesn't understand classes and can't call member functions.

Size control on passing array to function [duplicate]

This question already has answers here:
Why does argument matching in C++ ignore array sizes?
(1 answer)
In a function declaration, what does passing a fixed size array signify? [duplicate]
(3 answers)
Why can one specify the size of an array in a function parameter?
(3 answers)
Closed 3 years ago.
In C++, arrays are supposed to be passed to functions by reference. Hence, in what follows, function foo should implicitly use array inp by reference.
void foo(double inp[10]) {}
void foo1(double (&inp)[10]) {}
My question is, since both functions supposedly have the same interpretation of the input variable, why can we call foo in what follows, but we cannot call foo1?
int main()
{
double ary[20];
foo(ary); // compiles without any problem.
foo1(ary); // compiler error: invalid initialization of reference of type ‘double (&)[10]’ from expression of type ‘double [20]’
return 0;
}
since both functions supposedly have the same interpretation of the input variable
But they don't. A function argument of the type double inp[10] is automatically adjusted to a pointer double*. The 10 plays no part in providing type information here. Since all arrays decay to pointers, that will allow you to pass an array of any size.
A reference to an array does not get adjusted however. The type information is still there, and it must be a reference to an array of exactly ten doubles. The only way to pass a reference to an array of any size is to have a separate function for it, which you may write a function template to accomplish
template<std::size_t N>
void foo2(double (&inp)[N]) {}

C++ what is the difference of type * and type *& in function [duplicate]

This question already has answers here:
Passing references to pointers in C++
(10 answers)
Closed 4 years ago.
I have two function signatures in C++
void printArray(int* arrayPtr);
void printArray(int*& arrayPtr);
I understand the 1st function. It says the function takes in a arrayPtr argument which is of type that's a pointer pointing to an integer.
Both function signature works, but I have a hard time understanding the 2nd signature(*&) and what benefits it offers?
It's exactly the same as type versus type&; the first is a value and the second is a reference. The fact that type is a pointer doesn't change that.

How to get C pointer (like `var (*)(void*, var)`) out of `boost::function<var (void*, var)>`? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
demote boost::function to a plain function pointer
So I use Flash C++ Compiler (aka flascc, alchemy) and havin code like:
boost::function<var (void*, var)> f = boost::bind(&as3_socket::socket_socketData, this, _1, _2);
socket->addEventListener(flash::events::ProgressEvent::SOCKET_DATA, Function::_new(f, NULL));
get next compiler error:
$ g++ $JN -static -emit-llvm -c src/utils/http/as3_socket.cpp -I../boost/boost_libraries/install-dir/include
src/utils/http/as3_socket.cpp: In constructor ‘as3_socket::as3_socket()’:
src/utils/http/as3_socket.cpp:75: error: no matching function for call to ‘AS3::ui::Function::_new(boost::function<AS3::ui::var ()(void*, AS3::ui::var)>*, NULL)’
/cygdrive/c/Users/Avesta/Downloads/FlasCC_1.0.1134176_11-09-2012/sdk/usr/bin/../../usr/include/AS3++/builtin.h:179: note: candidates are: static AS3::ui::Function AS3::ui::Function::_new()
/cygdrive/c/Users/Avesta/Downloads/FlasCC_1.0.1134176_11-09-2012/sdk/usr/bin/../../usr/include/AS3++/builtin.h:180: note: static AS3::ui::Function AS3::ui::Function::_new(AS3::ui::var (*)(void*, AS3::ui::var), void*)
So the question is how to get from boost::function<var (void*, var)> more C style var (*)(void*, var)
The short answer is "You can't".
A boost function object may contain just a function pointer, but it might be the result of calling boost::bind () on some other function type and binding some of the parameters. It might also contain a function object.
Just because you can call it like a pointer to function doesn't mean it is a pointer to function.
[ Just noticed - link to other question above explains this in great detail ]
You are asking the wrong question.
boost::function is a way to expressed "do something iin the future", jut it is not a pointer to a C++ function. I this too strong to fit as a pointer to a function.
However your call back interface is a C++ function that takes a void pointer and something else. Usually you provide the callback with both the void pointer and the function pointer.
So what you do is create a function that casts the void pointer to an object, then passes that object the rest of the arguments it was called with.
Make sure the type you cast to void and back is identical.
You could even turn a pointer to boost::function into the void ptr.
Note that this leaves you with a problem of managing the lifetime of the good pointed to object. Sucks don't it?

What's the difference between fun(...) and (*fun)(...) using a function pointer in C/C++ [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How come pointer to a function be called without dereferencing?
How does dereferencing of a function pointer happen?
Supposing I have a function pointer like:
void fun() { /* ... */ };
typedef void (* func_t)();
func_t fp = fun;
Then I can invoke it by:
fp();
or
(*fp)();
What is the difference/
Precisely two parentheses and an asterisk.
Both call the function pointed to by fun, and both do so in the same manner.
However, visually, (*fun) makes it clear that fun is not a function in and of itself, and the dereference operator is a visual cue that it is a pointer of some kind.
The without-parentheses syntax, fun(), is the same as a regular function call and so visually equates to that, making it primarily clear you're calling some kind of function. It takes context or a lookup to notice that it is a function pointer.
This is just a style difference, as far as what happens.