When a caller is higher in the stack, what does this mean? For example, lets say I start a program, a form loads up (we'll call this a), then this form calls another form (b). The called form will be at the top of the stack, so if this form called form a, will this be a caller higher in the stack making a call to something below?
Thanks
I think you have the wrong impression of the call stack. The call stach is just a "list" of the functions that have been called. When ou have a call chain like you describe, a calls b which calls a, your stack is just:
a.second
b.first
a.first
You can't really call "down" to something. You make another call, and it goes on top of the stack, even if it has been called before, the previous call is completely different, the new call starts a whole new "stack frame".
You need to distinguish between the object making the call (if any), the target of the call, and the method being called. For instance, your call stack could easily look like this:
FormA.Method3()
FormB.Method2()
FormA.Method1()
This is an instance of FormA executing Method1, calling Method2 on an instance of FormB. That then calls Method3 on an instance of FormA - either the same FormA as the first one, or a different one. It doesn't really matter.
It's not really a case of calling "something below" because the stack frames don't represent objects - they represent methods (and the state within those methods). Does that help at all, or is it just confusing things more?
Related
I am looking for the name and info on a pattern (?) that I'm contemplating. As I don't know the name, it's difficult to search for it.
Here's what I'm trying to do, with a totally hypothetical example...
find-flight ( trip-details, 'cheapest') :: flight
I have this public function, find-flight(...), that accepts 2 parameters, trip-details and some instruction to apply. It returns a single flight, not a list of them.
When I call it with 'cheapest', the function will wait for all available flight results to come in from expedia, travelocity, hotwire, etc, to assure the cheapest flight is found.
When I call it with 'shortest-flight', the function would do the same kind of underlying work as 'cheapest' but would return the shortest flight. I'm sure you can come up with other instructions to apply.
BUT! I'm specifically interested in a variant of this: the instruction (implied or not) would be 'I-am-filthy-rich-and-I-want-to-buy-a-ticket-now'. In other words, the function would call all the sources such as expedia, orbitz, etc, but would return the very first internally received result, at any price point.
I'm asking because I want my public function to be as quick as possible. I can think of a number of strategies that would make it respond fast, but I'm not sure that I know which approach would be best considering that the parameter is unknown until called, right?
So I'm thinking about writing various versions of this function that would all be called by the public version. It'd return the first result. Then, the other strategies could optionally be aborted. If I did that, I could get some metrics on the function and further optimize.
If I were to write this in Java, I'd have a bunch of future objects that the function would loop through to see which one is done first. I'd return that one.
What's that called?
It's been proposed that the pattern is called Promise Race
I have seen both "subroutine" and "routine" used in programming language books. Are they the same concept? What does "sub-" mean?
I guess there are many examples which you might have seen in computer science books, besides the following one from Programming Language Pragmatics, by Scott:
In Section 3.2.2 we discussed the allocation of space on a subroutine
call stack (Figure 3.1). Each routine, as it is called, is given a new
stack frame, or activation record, at the top of the stack. This frame
may contain arguments and/or return values, bookkeeping information
(including the return address and saved registers), local variables,
and/or temporaries. When a subroutine returns, its frame is popped
from the stack.
Thanks.
It is my understanding that subroutine or routine are just names for self-contained blocks of code or instructions the program runs. For example, in Ruby we'd call subroutines methods where as in JavaScript they are called functions.
In the context of the Programming Language Pragmatics example you provided, the subroutine appears to be the call stack of actions to be executed and each item of the stack are routines that launch their own self-contained stack. After all of the processes are performed, the routine exits and the subroutine moves down to the next routine.
Wikipedia has a great high-level explanation of what is happening within the call stack and how subroutines got their name.
Both terms refer to the same thing : a subroutine is a routine called inside a routine. Think of it as a main program (a routine) that has function calls inside and every call to a function is a subroutine. However there are few differences between functions and routines, you can read more here
I came across the following post however had a hard time understanding it - self message(non recursive) vs self recursive message.
I also came across the example at http://www.zicomi.com/messageRecursion.jsp hoping a real world scenario would help but that confused me further. Why would you need a recursive message when the order has been passed to the kitchen and chef. I would thought all you would have needed is a self message i.e. the chef completing the order and then passing it to the waiter.
The chef example is arguably "wrong" in what it shows and describes.
Simply put, a message to self just means that the method that is to be invoked next happens to be in the same class of objects. E.g. a call to SavingsAccount.withdraw(anAmount) may call SavingsAccount.getBalance() to determine if there are enough funds to continue with the withdrawal.
A recursive call is a special case of a call to self in that it is to the same method (with a different state, so that it can eventually return out of the recursive calls). Some problems lend themselves to this solution. An example is factorial (see Factorial). To do a factorial without recursion would be impossible (at least for all cases but the simplest, due to the volume of inline code needed). If you look in the factorial code example, you'll see that the call is changed by one each time (factorial(n-1)) and stops when n reaches zero. Trying to do this inline for a value like 1,000,000 would not be feasible without recursion.
I have seen the fastcall notation appended before many functions. Why it is used?
That notation before the function is called the "calling convention." It specifies how (at a low level) the compiler will pass input parameters to the function and retrieve its results once it's been executed.
There are many different calling conventions, the most popular being stdcall and cdecl.
You might think there's only one way of doing it, but in reality, there are dozens of ways you could call a function and pass variables in and out. You could place the input parameters on a stack (push, push, push to call; pop, pop, pop to read input parameters). Or perhaps you would rather stick them in registers (this is fastcall - it tries to fit some of the input params in registers for speed).
But then what about the order? Do you push them from left to right or right to left? What about the result - there's always only one (assuming no reference parameters), so do you place the result on the stack, in a register, at a certain memory address?
Also, let's assume you're using the stack for communication - who's job is it to actually clear the stack after the function is called - the caller or the callee?
What about backing up and then restoring the contents of (certain) CPU registers - should the caller do it, or will the callee guarantee that it'll return everything the way it was?
The most popular calling convention (by far) is cdecl, which is the standard calling convention in both C and C++. The WIN32 API uses stdcall, which means any code that calls the WIN32 API needs to use stdcall for those function calls (making it another popular choice).
fastcall is a bit of an oddball - people realized for many functions with only one in/out parameter, pushing and popping from a memory-based stack is quite a bit of overhead and makes function calls a little bit heavy so the different compilers introduced (different) calling conventions that will place one or more parameters in registers before placing the rest in the stack for better performance. The problem is, not all compilers used the same rules for what goes where and who does what with fastcall, and as a result you have to be careful when using it because you'll never know who does what. Finally, see Is fastcall really faster? for info on fastcall performance benefits.
Complicated stuff.
Something important to keep in mind: don't add or change calling conventions if you don't know exactly what you're doing, because if both the caller and the callee do not agree on the calling convention, you'll likely end up with stack corruption and a segfault. This usually happens when you have the function being called in a DLL/shared library and a program is written that depends on the DLL/SO/dylib being a certain calling convention (say, cdecl), then the library is recompiled with a different calling convention (say, fastcall). Now the old program can no longer communicate with the new library.
Wikipedia states that
Conventions entitled fastcall have not been standardized, and have been implemented differently, depending on the compiler vendor. Typically fastcall calling conventions pass one or more arguments in registers which reduces the number of memory accesses required for the call.
Say you had two classes A and B. If the relationship between is has-a
i.e. A has-a B
how can you pass information from B into A? Say for example in B you work out a calculation and need the answer in A.
Is there any other way of doing this besides passing a pointer to class A into class B and calling a function which takes the answer as a parameter.
Hope this makes sense,
MD.
Sorry I should have been more specific
quote from my comment below.
"well I gave a simple example. I am programming this in java and my class B runs a new thread and and will calculate the answer. Therefore I cannot just call the function from class A as I don't know when the calculation will be completed."
The method in A that needs the result of the computation should call into the method in B that does the computation.
This answer is so obvious that there may be something you're not telling us (?)
Ok, so the question is really about threading. Yes, then passing a reference to owner object and calling back into it may be a good idea. A better idea might be to return a future object that encapsulates the result of the computation.
If I understand correctly, you have this kind of relationship:
class Car {
Engine engine;
int test() {
int fuelLevel = engine.getFuelLevel();
// do sth with fuel level, store it, use it etc.
}
}
This example shows how can you pass information between the two classes: for instance as a result of function. Car object (your class A) calls a method on Engine object (class B) and int this way he obtains desired information. This can be easily translated to any kind of work that class B does.
There are basically two ways to manage "asynchronous" calls.
The first is having a callback and the other polling.
Having a callback is what , you describe. When B has finished, it needs to call A somehow that it has finished. That can been done by "giving" the adress of A to B, so it knows what to call, or by using a intermediate object C, which calls B synchronously and send the result back to A. C then needs to know about A.
Polling is when A check regularly if B has finished. This solution is usually less satisfying intellectually and more CPU consuming. You are also not notified exactly when B finished. (When B finish, nothing happend, you'll have to wait for the next poll to be aware of it). However, that way , B doesn't need no know anything about A.
I would use the first pattern with an intermediate object (and special class C). So that your model is still clean (B doesn't need to know about A or C). I suggest also you have a look at the Observer pattern.