void SomeMethod(int x, double y) {
// some code
...
new Thread(ThreadFunc).Start();
}
What do you think about the code above?
Some may say nothing seems to be wrong. Some may say there is not enough information to comment. A few may say that it is awful to spin off a thread like that (the last line of the method), and that there is a probability for the thread to be garbage collected at an unexpected point of execution. That is something interesting to discuss about.
More ...
What does this cryptic bracket sequence mean? What programming language is it? Is it valid syntax? If there is even a weak chance of this syntax being valid? If so, what does it mean?
Alright, alright, alright! It is C++. That would calm most people; with all their love (pun) for C++. Specifically, it is C++0x. Amongst many other features that we have been waiting for, C++0x gives us the power of lambdas.
More ...
A couple of my colleagues and I huddled up to learn a bit of Android. I think I told you about that a short while back. We developed a very simple application β The Seinfeld Calendar.
Seinfeld calendar or otherwise called the habit calendar is Seinfeldβs productivity secret. The secret of achieving your goal is practising something, whatever your goal is, everyday and make it a habit. And mark it in your calendar each day you practice, and make sure you do not break the chain. Our application helps you keep track of your everyday tasks.
More ...I am not a java programmer. By that, I do not mean I am against Java. As a programmer by profession and passion, I try to learn things along the way. That includes a little of bit of Java. I should say that my proper encounter, so to say, with Java is a simple application that I am trying out with Android. There might be some hard core differences and/or limitations in the Android version of Java. But I am almost certain that I am using only primary level features of Java. More ...
The question is, in C++, how do detect if an object is allocated on the stack or heap.
On Windows, the stack address is in the range of 0x80000000
. If the address of the variable is in this range, then you could say that the object is allocated on the stack; else it is allocated on the heap. This technique of detecting is not preferable since it may not work on other operating systems (such as linux), and deals with the platform specific information making it a non-portable solution.
You may not know the guy in black. You should definitely be knowing the guy in green. Don’t you?
I am not a patron of his philosophies against planned design. But he sure is a great guy with lots of good ideas. It was nice having an hour long chat with him.
Alright, it is a long wait. And I am going to keep it short.
Recap of the problem: Why did the ref
variable in SomeMethod not get the expected result (DayOfWeek
.Friday
) when called from a different thread?
Boxing. Yes, that is the culprit. Sometimes, it is subtle to note. DayOfWeek
is an enum
β a value type. When the method is called from a different thread, we put the argument (arg3
) in an object array, and thatβs where the value gets boxed. So we happen to assign the resultant value to the boxed value.
Straight to code…..
int SomeMethod(string arg1, string arg2, ref DayOfWeek arg3) { // Wildest implementation! }
The above method had to be executed on its dispatcher thread. So let unravel a bit of the wildest implementation above.
int SomeMethod(string arg1, string arg2, ref DayOfWeek arg3) { if (Disptacher.CheckAccess()) { var funcDelegate = (Func<string, string, DayOfWeek, int>)SomeMthod;return Dispatcher.Invoke(funcDelegate, arg1, arg2, ref arg3); } // Wilder implementation!!
}
Before you say anything, yes, the compiler spat the following errors:-
More ...
Currying is a mathematical concept based on lambda calculus. It is a technique of operating on a function (taking multiple arguments) by splitting and capable of chaining into a series of single argument functions. It is very similar to what a human would attempt to do on paper. For example, if you have to add numbers 1
through 10
, what would you do? Class II mathematics -0
in hand, 1
in the mind, add 0
and 1
, so 1
in the mind, then 2
in the hand, … up to 10
. So we compute the addition with one argument at a time.
More ...
template<typename A, typename B>
class TClass
{
public: TClass()
{
}
// Overload #1
public: std::string SomeMethod(A a, B b)
{
std::ostringstream ostr;
ostr << a << "-" << b;
return ostr.str();
}
// Overload #2
public: std::string SomeMethod(B b, A a)
{
std::ostringstream ostr;
ostr << b << "-" << a;
return ostr.str();
}
};
So that is a template class with SomeMethod overloads. Why would somebody write such a class? Imagine it is an adder class, and the method overloads could used to add with parameters specified in either order. Following is the way one could use the above (based on the adder example):-
More ...