Understanding Pass-By-Value-Result - pass-by-reference

Consider the following program
int i;
int a[2];
void foo(int x, int y)
{
x++;
i++;
y++;
}
main()
{
a[0] = 1;
a[1] = 1;
i = 0;
foo(a[i], a[i]);
print(a[0]);
print(a[1]);
}
I'm trying very hard to understand pass-by-value-result. However couldn't get the hang of it. I know it's similar to pass-by-reference. But in what way? How does the results differ in the above case? When does it actually use the "reference" of the values?

Probably in your posted code, you wanted "foo(a[i], a[i]);" instead of "p(a[i], a[i]);".
Anyway, your a[0] and a[1] will stay set at 1, because foo() gets arguments by value, and there is no way for foo() to change its arguments in a manner visible to the outside.
Passing by value and passing by reference are similar only because both execute a "passing". Passing by value involves a duplication of the data: foo() receives a copy of a[0] and a copy of a[1]; and foo() calls them x and y. Then foo() increments both, but x and y are copies only visible to foo(), so nothing happens in reality because x and y are no more used after the increment.
Inside foo() there is an increment of the variable i, which is global. This time instead, the variable gets really modified, and the change is visible to everybody.
Passing by reference is different in the sense that foo() would receive an address (the reference) of a variable, and foo() would be able to read but also write "in" that reference, thus modifying the object living there.
Think about this example. I am the main() and you are the foo(). I have a sheet of paper with something written in it, say the title of a song. I can let you know the name of the song by taking another sheet, copying onto it the name, and giving the new sheet to you. This is passing by value.
Different would be if I said to you: "the sheet is on the table" (the table is the location of the data, the "reference"). You go there, look at the sheet, so you know the title of the song. But you could also modify my sheet that lies on the table. This is passing by reference: I tell you where the data is. Of course, to be precise, the way I say to you where the sheet is, is accomplished by taking another sheet and writing on it "the sheet with the title is on the table", then I give you the new sheet. You, foo(), receive a sheet (passed by value), and you can also destroy or modify it, I will never notice. But before modifying this new sheet which will be useless soon or later, use it to look on the table and don't modify MY sheet with the name of the song on it! :-)

Results are:
2
1
This is because, in pass-by-value-result, any modification done to the variables within the calling function, will not be updated until the function exits.
With the same program, using pass-by-reference,
3
1
would be printed, because it references to the same address and updates any modification done immediately.

Related

Creating a class object without a variable

So I create my class Cars. Then I make a variable(audi) for an object from my class cars. Everything is fine and dandy but I noticed that I can create an object(with an engine value of 50) without specifying a variable for it to be held in. So now how can I access that object?
#include <iostream>
using namespace std;
class Cars
{
public:
int getStatus();
Cars(int engine=5);
private:
int m_engine;
};
Cars::Cars(int engine)
{
m_engine=engine;
cout<<"A new car is made."<<endl;
}
Cars::getStatus()
{
cout<<m_engine<<endl;
}
int main()
{
Cars audi(10);
audi.getStatus();
Cars(50);
}
It depends on when you want to access the object.
Cars(50);
With this piece of code, you create a temporary object that lives only during execution of the current statement (i. e. until execution of your code passes the semicolon).
Cars(50);
// I want to access the object here
In this case: Too late. The temporary has already been destroyed and doesn't exist any more. No chance to get back to it. You can, however, use the object as long as it lives, e. g. call a member function on it or pass it to another function:
Cars(10).doSomething();
// but right now (after the semicolon), the car doesn't exist any more
doSomethingElse(Cars(12));
// again, the object got destroyed
Be aware that in above example, you created two distinct instances, each ceasing to exist when reaching the semicolon. Be aware, too, that the objects' destructors get called at this point of time.
You need to do it all together, like this:
Cars(50).getStatus();
Otherwise you won't have a way to refer to your car with 50 m_engine.
This temporary should be used with fidelity. Read more in What is an Anonymous Object?
You can't! It's already dead by the time you try. Cars(50) is a temporary, and its lifetime ends at the end of its full-expression -- that is, at the ;.

Lua override class function in included file

I'm sorry I'm new to LUA scripts and I have to work on code written by others.
Please don't focus on code, my problem is only about included files and priority evaluating which function has to be called, in case of overriding.
Let's say I have a file Terrain.lua containing a class Terrain, which has a function Terrain:generate() and Terrain:generate() calls Terrain:getLatitude().
Terrain was included in a script MyScript.lua, which overrided Terrain:getLatitude() as follows:
include("Terrain");
function Terrain:getLatitude()
new code;
end
function myFunction()
local myTerrain = Terrain.create();
myTerrain.generate();
end
This has the effect of overriding getLatitude(): when myTerrain.generate() is called, generate() is the code from the included "Terrain", but getLatitude() is the local function with the new code, even if called by a function from the included class.
Now let's say I want to put some of the code in an external file Custom.lua. Custom (and not MyScript) has to override getLatitude().
This is the situation:
Terrain.lua contains Terrain class and these functions
Terrain.create()
Terrain.generate()
Terrain.getLatitude()
MyScript.lua is the script being executed, and include Custom:
include("Custom");
function myFunction()
return customFunction()
end
Custom.lua contains:
include("Terrain");
function Terrain:getLatitude()
new code;
end
function customFunction()
local myTerrain = Terrain.create();
myTerrain.generate();
end
Now, if I call customFunction() from MyScript, getLatitude() from Terrain is used, instead of getLatitude() from Custom. I assume ovveride is possible only inside the currenti file being executed? How can I achieve overriding in an included file?
I hope this example is enough to understand my problem, without posting a lot of code. Thank you.
Firstly, some corrections: there is no local function's in your question; include is not part of any lua standard, what that function actually does may be quite important.
Finally, Lua does not have actual class system, what you use in the question is merely a syntactic sugar (misleading and confusing as I find it) over table assignments. Lua is an interpreted language, so what may seem to you as a class definition is not a static structure known from the very beginning of the program execution but a code that gets executed from the top of the file to the bottom.
Thus, if we assume that include is similar to the require, then the your question code will be equivalent to the following:
do--terrain.lua
Terrain = {
create=function()
local created_object
--some code to assign value to created_object
return created_object
end
}
Terrain.generate = function(self) end
Terrain.getLatitude = function(this_is_a_self_too)
--some code that uses `also_self` as a reference to the object when called as object:generate()
end
--do end block is essentially an equivalent of file, its local variables are not seen outside
--global variables it has assigned (like `terrain`) will stay accessible AFTER its end
--changes it done to global variables will also remain
end
do--Custom.lua
Terrain.getLatitude = function(this)--this is the assignment to a field in a table stored in the global variable Terrain
--this function will replace the one assigned to the `getLatitude` field
end
customFunction = function()
local myTerrain = Terrain.create();
myTerrain.generate();--this one probably needs `:` instead of `.`
--depends on actual code inside terrain.lua
end
end
do--MyScript.lua
myFunction= function()
return customFunction() --this line calls the global variable customFunction
end
end
Thus if your actual setup is similar to the one in question, then the "override" will take effect after the Custom.lua is executed and for all the subsequent calls to the Terrain.getLatitude regardless of whether or not they've called the file. (And any later file can override it again, and all calls after that will be using the new one)
It is probably more complicated to do a limited override in this setup. That again depends on the actual details of how your team has defined the Terrain class and the class system itself.

Cannot create class in AHK after destruction

I'm trying to wrap my head around classes in AHK. I'm C++ dev, so would like to make use of RAII (__New, __Delete), but it looks like I miss some concepts since things look very contra-intuitive for me.
After some tries I came up with this simple example:
class Scenario
{
__New()
{
MsgBox, NEW
}
__Delete()
{
MsgBox, DELETE
}
}
scenario := new Scenario
scenario := new Scenario
scenario := 1
scenario := {}
scenario := new Scenario
Return
As a result I get the following messages:
NEW
NEW
DELETE
DELETE
Questions:
Why doesn't the object get destroyed during the second assignment? I'd assume the number of refs going to 0, no?
How come I get 2 destructions in a row? Where was that object stored meanwhile? How could scenario variable hold both references?
Why was not the third construction called?
Why doesn't the object get destroyed during the second assignment?
Garbage collection had not been triggered yet
I'd assume the number of refs going to 0, no?
References going to 0 does not necessarily trigger GC
How come I get 2 destructions in a row?
Garbage collection cleaned both references at the same time
Where was that object stored meanwhile?
The heap
How could scenario variable hold both references?
scenario does not hold both references
Why was not the third construction called?
Only two Scenario objects are constructed. The variable scenario is a dynamic variable and is not always an instance of the class Scenario. The last assignment scenario := {} just creates an empty object.
Ok, found out what was missing. Two things:
AHK script is case-insensitive.
Since class is an object by itself in AHK it's possible to override the class by another object.
Here is a piece of the documentation:
Because the class is referenced via a variable, the class name cannot be used to both reference the class and create a separate variable (such as to hold an instance of the class) in the same context. For example, box := new Box would replace the class object in Box with an instance of itself. [v1.1.27+]: #Warn ClassOverwrite enables a warning to be shown at load time for each attempt to overwrite a class.
This explains what happened in the code above: variable name scenario is effectively the same as a class name Scenario, so I just quietly overrode my class with an empty object.
Also, since the new instance of the class is created before assignment, I got two 'NEW' in a row, only than 'DELETE'.

MobileSubstrate: MSHookFunction example

I am trying to write a MobileSubstrate plugin which hooks into a C-method. I tried to edit the famous "ExampleHook", by just writing a demo MSHook and hook it in the Initialize method.
This is probably too optimistic and it doesn't work. But I cannot find anywhere a simple example of a MSHookFunction(). There is barely information about this on the Internet. It might be possible I misunderstood the whole concept of MSHookFunction.
Please, can anybody help me out with a little example code? I would deeply appreciate any help.
Best regards,
Marc Backes
I realize you have found this, but I am posting this answer to help whoever else may be needing this.
A simple example can be found at the MobileSubstrate article on the iPhone Dev Wiki, and an actual example of this in a project is at this bit of User Agent Faker.
But what is an answer without an actual explanation? Therefore, here we go!
void MSHookFunction(void* function, void* replacement, void** p_original); is the function definition for MSHookFunction, the magic function which causes your function X() to be interposed by Y(), for instance.
That is, when a program commonly would call X(), the call will be redirected to Y() instead. This is pretty much a basic explanation of function interposing.
Now, what are the parameters, and their usefulness?
function is a function pointer to the function you want to interpose. That would be a function pointer to X(), in our quick explanation.
replacement is a function pointer to the function you want function to be interposed with. In our quick explanation, that would be a function pointer to Y().
p_original is a pointer to a function pointer, which from now on will point to what function used to be.
The reason this is there is simple: If you intend to modify behavior, but not suppress it, you'll still need to call what X() used to be. But a common call to X() wouldn't work as intended, as it would end calling Y() instead of the default function.
Therefore, you have a function pointer to call X() as if it wasn't interposed.
Now, explaining the devwiki example:
static void (*original_CFShow)(CFTypeRef obj); // a function pointer to store the original CFShow().
void replaced_CFShow(CFTypeRef obj) { // our replacement of CFShow().
printf("Calling original CFShow(%p)...", obj);
original_CFShow(obj); // calls the original CFShow.
printf(" done.\n");
}
...
// hook CFShow to our own implementation.
MSHookFunction(CFShow, replaced_CFShow, &original_CFShow);
// From now on any call to CFShow will pass through replaced_CFShow first.
...
CFShow(CFSTR("test"));
Here, we:
Pass a pointer to CFShow, the function we want to change default behavior from as the function parameter.
Pass a pointer to the function we just created, replaced_CFShow as the replacement parameter. That is, whenever CFShow would be called by default, replaced_CFShow will be called instead.
We pass a pointer to the original_CFShow function pointer as the p_original parameter. Since we still want the things CFShow still does by itself to be done inside our replacement function, we call it.

c and objective-c variables

Let me see if I can articulate what I'm trying to do... I'll distill it to the core issue.
I have an objective-c program and a c callback function. The context is I am using cocos2d and chipmunk physics with the iphone sdk. What I am trying to do is update "score" upon a collision between a bullet and a monster. Everything works fine except for updating the score.
the game logic is in obj-c but the actual collision logic is done in a c function that gets called when things collide. For example, it is there that I render the flames/explosion of the collision. It is also there that I want to update the score.
Here's the problem:
But score is an instance variable of the "GAME" object and I do not have visibility into that instance variable. I suppose I can pass into the c function another parameter by reference called &addscore or something, but was wondering if there is a more obvious way to do it since there might be a bunch of other variables beyond 'score' that must be updated upon the collission.
the net is I need variables in the object to be seen by the c function, and vice versa: those variables set in the function to be seen in the object.
thanks in advance.
Your knowledge and thoughts about gaining access to Obj-C instance variables within C code by passing them by reference seems correct.
If you need to pass a number of variables I assume they collectively make up some kind of "game state" description. Perhaps these could be grouped into a seperate object or structure and passed into the C function via a single parameter?
As an example, the following class:
typedef struct GameState {
int score;
int numberOfBulletsLeft;
} GameState;
#interface GAME : NSObject {
GameState state;
}
...
#end
Would work well with a C function such as the following
void myCFunctionThatDoesCollisiondetectionEtc(GameState * state) {
NSLog(#"You have %d bullets left", state->numberOfBulletsLeft);
}
If your existing GAME object has suitable properties or messages, you may even be able to do something like the following, where you simply pass your entire Objective-C object:
void myCFunctionThatDoesCollisionDetectionEtc(GAME * myGame) {
if (...)
[myGame increaseScore:50];
else
[myGame decreaseScore:50];
}
A third alternative would be to change the return value of your C function to indicate if a collision has been detected and then leave it up to the Objective-C code within the GAME class to update the score and any other actions which may need to occur.
To improve on that last answer a bit more, you can do the following:
cpSpaceAddCollisionPairFunc( space, 1, 2, (cpCollFunc)functionName, game);
void functionName( cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, GameClass *game) {
[game doSomethingToScoreSomething];
}
No need to cast the pointer inside of the function, you can cast the function pointer instead. That's what I've done internally with the Chipmunk source and it's a lot cleaner.
No, you do not need variables in your object to be visible by the function. That breaks encapsulation, and it's a bad idea. When you want an object to do something (like changing some internal value such as the score) you should provide a method to accomplish that effect.
If you allow code outside your class to alter your instance variables, then you're throwing away a key advantage of object-oriented programming.
If the game object is a singleton you can access it from any scope (which includes your C callback). You would make a singleton if you only ever want one instance of some object to exist. A singleton can always be reached from anywhere by sending a message to the class to retrieve the singleton instance:
[Game sharedGameInstance];
Alternatively, chipmunk allows for you to pass a void * data to the callback. This is to accommodate the programmer to send information he needs to the callback.
You can send a pointer to your game object instance in that void * to the callback, like so:
cpSpaceAddCollisionPairFunc( space, 1, 2, &functionName, (void *)game );
void functionName( cpShape *a, cpShape *b, cpContact *contacts, int numContacts, cpFloat normal_coef, void *data ) {
GameClass * game = (GameClass *)data;
// do whatever you need here. You can call messages on game as per usual.
}