avr-gcc: icall implementation in C++ - avr-gcc

I would like to know how to implement AVR ICALL instruction in the C++ scope. Let say I have a pointer variable that holds the address of my function and I want another function to call the function indirectly through the pointer kept in the variable.
Thanks in advance for the help and assistance.

Related

In c++ should I use pointer(or reference) of class as function's variable?

I heard c++ doesn't let me use array as function's variable since copying all elements in array could be inefficient. So I have to use pointer instead. That being said, is it better to use pointer of certain object as function's variable whenever possible? Or is there any downside of this method?
edit : I know how array is passed in C++. My questions are :
Is it efficient to use pointer(or reference) when sending class(or structure) object?
Is there any reason not to use pointer(or reference) every time? I saw lot of code which doesn't use pointer when object is small. I mean it is still extra memory usage, so why don't we use pointer in such cases as well?
I've got the answer of 1st question, so could you please answer second question?

What is a functions list pointer at beginning of structure called c++

I'm reversing some assembly code and I'm consistently coming across certain structures that have an address at the very beginning of the structure.
This address seems to be a pointer to the beginning of an array of function addresses related to that specific structure.
I've also noticed that the first function in the array is usually related to deallocated/cleaning up of the structure.
Does anyone know what this type of structuring is called? I'd like to learn how this works
That's the "vtable" a.k.a. "virtual method table".

Cost of using functions in fortran (or any other language)

Let say I have a array which is very big verybigvariable
And I have defined a function that does some operations like this
function myfunc(var) result(res)
real:: var(:,:,:),res
...
...
...
end function myfunc
My question is that when I call this function like this
myvar=myfunc(verybigvariable)
what happens? does it duplicate my variable so it holds 2X space in the ram during the execution of the function? If so how can I prevent this? (In a simple program, I know, I can define the function without any parameter and make it use existing variables, but If I am programming a module, it seems I have to include parameter to the definition)
The Fortran language standard does not specify how arguments are passed. Typically in the interest of efficiency the compiler will not make a copy but pass the address of the argument. There will be cases in which a Fortran compiler has to make a copy. E.g., the actual argument is a non-contiguous array but the procedure expects a contiguous argument. The compiler will have to fix the mismatch by making a copy that is contiguous to pass to the procedure. If the procedure modifies that argument, the values have to be copied back to the original argument.
In fortran it seems that parameters are passed by reference. This means that only the address of the variable is passed, and the function can then access the variable through that address.
So no, the array is not copied, only the address of the array is passed. The overhead for this will be either 32 bits for a 32-bit system, or 64 bits for a 64-bit system.
I have no experience with fortran, and this is only what I could figure out though a Google search, so if any Fortran programmers have any remarks, please feel free to edit/comment.

Reg function pointer

I have one clarification
What is the difference between calling a function through function pointer and calling a function directly by name ?
Anybody help me in this.
There is no difference in the actual call. Parameters are passed the same way, the function runs the same way, and the return value comes back the same way.
The only difference is that you can make the function pointer point somewhere else.
There is no difference except that a compiler/linker calculates exactly what address to transfer control of the program to when you call a function by name and hardcodes that value into the code, whereas with function pointers, the computer must use the pointer to calculate where to transfer control to at runtime.
No difference (except that calling by name will always call the same function, and pointer can be changed to point to different functions).
While the direct use of function pointers does not have any cost, you should bear in mind that function pointers aren't compile time constants, so it maybe has a cost to read them. So if you have a function pointer inside a class and use that to emulate polymorphic behavior, you won't get any speedup at all.

Symboling functions without PDBs

Suppose I have a function called "Overflow" in a DLL called "Overflow.dll" but I don't have its PDBs.
I know I can get the address it starts in with "GetProcAddress", but can I get somehow the address where it ends or its size?
(C++ in windows)
The reason I ask this is that I have an address and I want to know if it is inside my specific function. So I assume (and please correct me if I'm wrong) that the address is in my function if:
StartAddress <= My Address <= EndAddress
thanks :)
So I assume (and please correct me if I'm wrong)
I'm "correcting". The issue is that it's extremely unlikely that your function has no function calls inside of itself. For example, the position could be inside of a printf call called by your function, but the instruction pointer would not be in your function itself.
You could implement a parser for x86 instructions that looks for the return instruction to find the end address, assuming you know there is only one return in the function. If you don't know that there's only one return, then you need the PDBs.