Saturday, October 16, 2010

Apple, Microsoft, and everything

If you have to wonder what this post is about, then you have probably been living in a hole, in an island somewhere off the coast of New Mexico. In that case, then, welcome; welcome to civilization strange one. Today we are talking about Apple, the silicon one.
If you however have not been living secluded in a cave off the coast of New Mexico, then you probably know of Apple, the makers of iPod, iPhone, iPad, oh yes, and they
make computers too. It's scary, I think, that we live in a society that consumes more electronic apples than the ones that grow on trees. Lately however, it seems that Apple can do no wrong. Any product they launch is record selling, and leaves mobs of "fanboys" salivating and sleeping outside malls and stores hoping to get into the latest trend.

I have for years, to the frustration of many of my friends, been a constant critic of Apple. This year however, to the delight of most of my friends, I bought an iPhone 4. Let me set the record straight on my views on Apple, Microsoft, and "fanboys."

Fanboys:

When I say fanboys, I am speaking of the people who are die-hard apple fans without regards to any bit logic. These are the guys that will pay $1500 for a bag of flaming shit, as long as it has the Apple logo on it. I remember not long ago I was sitting in the subway on my way home, and had to listen to a fanboy secrete a greenish goo of complaints against Microsoft, and viruses, and blue screens. Fanboys love talking about blue-screens, and viruses, and windows crashing. Listen guys, these are not Microsofts faults. Trust me, Microsoft has bigger flaws that actually make sense, flaws which upon mentioning their existence, will not make you sound ignorant and stupid. Those flaws I will site later in a different section. For now I will get back to the notion of blue screens, viruses, and windows crashing.

VIRUSES: The install base that windows has and has had for 30 years is far, far, Peter-Pan's-Neverland-far, greater than Mac OS. Therefore the chances of 14 year old immature programmers been familiar with a windows environment is much greater than with Mac Os, therefore there is a greater chance of having more windows oriented viruses. Also, until relatively recently Apple had not released a SDK for coders to develop on Macs. There is another reason, Apple computers for years have been targeted more to the "artistic community" than the nerdy programming one.

Blue Screens and Windows Crashing:
Again, the install base of Microsoft Windows is so big that if you were to put the windows cds next to each other they would wrap around the Earth several hundred times. Ok, I made that up. But it is really much much greater than Mac OS. By install base, I mean amount of copies installed, but also across a very very wide number of different types of systems. Apple creates the OS but they also create the hardware (yes of course they outsource to Intel and blah, but everything is done to their spec). It's really a lot easier to cater to your own hardware than have to deal with every single company outthere that thinks they can create peripherals. As if this isn't enough, then come the programmers of the world. There are so many programmers in the world that code for windows that if you were to stand them side by side they would wrap around..ok lets not go there, but again a lot more than on Mac OS. These are not doctors in computer science mind you, these are kids that took a programming course and release code on the internet for others to download. There is so much more third party code outthere for windows compared to Mac that if you were to make an analogy of the windows available code as being a giant then the equivalent in Mac code would be a little shit pebble stuck to the side of the giant's ass cheek. So, with the wide range of hardware peripherals that windows has to support, and all the different kinds of people releasing questionable code to run on PCs it is really a miracle that Windows works at all. In fact Microsoft is pretty freaking awesome in this aspect. So please, next time you want to talk to me about Apple and Microsoft, don't bitch about windows crashing.
You can say, "for people who are not tech savvy and may go around the net downloading every little .exe they see, and clicking on every popup, Mac OS may be safer." But what you can't say is Apple is so much better than Microsoft because windows crashes. That's stupid.


Apple:

To me Apple is the greatest designer company out there. They, without argument, have the best looking products available. And have hit the nail on a few ground breaking products like iPod and the iPhone. That is undeniable. I got the iPhone because for me at the moment, there is no better smart phone. I love the apps that we can download. I love the user interface. I love the browser.
However, as a company, I have many problems with it. I don't like the Big Brotheresque way in which it runs its business. As a developer I wish I could write an app for my iPhone and release it. First, I need to buy a Mac. It doesn't suffice that I bought an iPhone. I also need to buy a Mac. Why can't I write the app on my PC? Many will say, hey they own the products and if you don't like it don't buy it. That is true, however, it doesn't make it right. I bought the phone and don't regret it because I like the "smart phone aspect" of it. But I don't like the fact that I need to buy a Mac to develop for it. I think it's a blatant money grab.
I also don't like the fact that they decide what I can and can't put on the Apple Store. Yes, fine, check if it isn't malicious code, but who cares if my app is "useless" to you. You should be able to deny an app based on whether you think it's "useful." Again, it's a totalitarian-like regime which I really don't like.

Microsoft:
It's a great "developer" company, but not design oriented in the least. By "developer" company I mean that they develop for developers first and foremost. It's really easy to develop for windows and the tools are available and mostly free. What I don't like about Microsoft is that the design seems to be an after-thought. Take windows phone 7 for example. It seems to be a great OS, and the tiles are an amazing idea with "live" data showing. But, it looks like shit. 2D squares slapped on your screen, and the titles get cropped around the edges of the screen instead of zooming out, come on guys, give it some effort.


In general, I think Microsoft Windows XP (I hate Windows 7, but this is for a later time) is a remarkable OS, and Microsoft is a really good company with really incredible products, which needs to spend a bit more time on the design aspect. Apple, is an incredible design company which needs to care more about people and really give people a choice. They are very controlling and need to step back and let the consumers make some choices with their products. When I pay so much money for a phone I don't think it should be considered "a license", it's my product, let me use it.

Saturday, October 9, 2010

Country Folks

I am writing this entry from a motel on Highway 26 in Blue Mountains. The thing about driving out of the city is that right about when the last building drops below the horizon, everything begins to slow down. The highway is no longer 100 km/h instead it drops down to 80 km/h. High-rises and malls with car noises and crowd chatter are replaced by corn fields and farmland with the music of birds and swinging flora. Instead of 100+ horsepower mustangs, these folks own the original 1 horse power mustangs, no oil change or gas needed, and instead of polluting the air it fertilizes the orchards.

After checking in to the motel we decided to go for a drive around the village which is approximately 15 minutes away by car. On the way to the village the traffic became very slow due to construction on the road. While I waited for the traffic to begin moving again I spotted a group of kids a few cars up the road armed with bags full of apples approaching the helpless cars. "Here we go again," I thought. "Why can't I go a full day without somebody trying to sell me something." As one of the youngsters approached my passenger window I prepared to signal to him that I wasn't interested, but just then the always benevolent and never 'arrrgghhhh' wife of mine opens the window. Fine! I fished in my pockets for a $20 to be prepared for whatever price this little creep would try to take us for. "Would you like some free apples?" asked the boy. Would I like some free apples? Are you kidding me who hands out free anything anymore, let alone free apples? In Toronto if you get offered something free it's most likely deepfried or illegal. I had to put away the 20 dollar bill in order to free my hand to be able to receive for free the very apple I did not want to buy. Who gives free apples away?

Later, on our way back from the village just off Highway 26 I saw a sign advertising honey at a good price. I pulled over and approached the little table by the side of the highway where the honey containers were. The table was in the front yard of a house whose owners I could not see anywhere. What was peculiar about this picture was that I didn't see anyone selling the honey. There, in front of me were several jars of honey and, most interestingly a topperware full of cash. Are you kidding me guys!? Really? Cash? Just lying around, no need for surveillance, or any sort of care? I must have stood in front of the table like a moron for about ten minutes trying to decode this unfamiliar situation. I kept thinking, "this is a trap man, they got snipers in the cornfields!. Touch those jars and you'll get shot. Just walk away..." Then, when I was about to get back in my car and drive the hell away, a smiling lady comes out of the house and waves at us. "Hi folks, just grab a jar and put the money in the box."
She must have read the confusion in my face for right away she said "You are from the city aren't you?"
I just could not believe that you can leave a box full of money and the product you are selling unattended, and not only not have it get stolen, but get the product to sell itself while the box swells up with more money from nice and honest customers. Mind boggling! Apparently however, this is just how business is done on this side of the country.

I love my high-rises, sport cars, and technology infested city-boy society, but it takes a few jars of self selling honey and a self guarding money box to poke a hole in our sophisticated cloud of egotistical realism, and realize that we need to preserve the country folks lest the world implode into a useless goo of materialistic bullshit

Thursday, September 3, 2009

Stack Growth - Stackoverflow

Stack Growth

Another question you might get is to try to figure out which way a stack grows. As we know a stack (memory region interpreted as a stack) gets allocated for each process. The stack is used to store local variables and context local to the process. Each function call in a process gets a frame inside the stack where the variables local to the functions get stored; when the function exits the frame gets freed. The stack is also used to store the address of the caller function so that the IP (instruction pointer) can jump back after exiting a function call. i.e:

From the example below (Program:) before main() jumps off into foo(), it first pushes the current address of the IP into the stack so that when foo finishes, the IP can know where to go back to in main();

In some systems the stack is said to grow up and in others it is said to grow down. What the heck does that mean? See bellow:

Memory:
0x00000004 -- -- -- --
0x00000008 -- -- -- --
0x0000000C -- -- -- --
0x00000010 -- -- -- --
...

Program:
int foo();
main();
main()-call->foo();

In a simplistic world (don't hold me to the number it's just an example) if your main()'s frame is at 0x0000000C and foo()'s frame is pushed onto the stack at 0x00000008 then the stack is said to grow down-even though in the diagram it might appear as growing up-because the addresses decrease in that direction. If foo() were to appear at 0x00000010 then the stack would grow up, or in the direction in which the addresses increase.

Code:

#include <stdio.h>

void stackTracer(int *x)
{
int y;
if(&y > x)
printf("The stack grows down\n");
else
printf("The stack grows up\n");
}

int main()
{
int x;
stackTracer(&x);
getchar();
return 0;
}


The code works because when stackTracer is called, the main() frame had already been created in the stack, therefore stackStracker() would be pushed onto the stack after main(). If the address of a variable declared in stackTracer() is greater that the address of a variable declared in main(), that tells you that the stack is growing up as it's growing in the direction that the addresses increase.

StackOverflow

The Stack has a size that is defined even before the process begins to run. The size of the Stack is defined by the linker and can be adjusted by changing the linker flags. But once the program has been linked with the Stack size then that will remain the size of the process' stack throughout its life time. Sometimes if you are not careful, you can grow the stack (recursion anyone?) over its allowed size; and you get a stackoverflow exception.

How can you tell the size of your stack?

This program came out of a couple of minutes of spare time when a co-worker friend of mine and myself decided to overflow the stack just for fun:

Code:


#include <stdio.h>

int counter = 0;
void crasher()
{
double array[10];
array[0]=1;
counter++;
printf("Counter is at %d", counter);
crasher();
}
int main()
{
crasher();
return 0;
}



Then to calculate the size of your stack do => counter (amount of times you allocated that array) * 8 bytes (the size of a double [check your system though]) * 10 (the amount of doubles in the array). Note: this will be roughly the size of your stack, to check the exact size you can always check the default for your linker or the linker flags.

Wednesday, September 2, 2009

Byte swizzling

So, we have all heard about little endian and big endian concepts. We all know that in a little endian system the least significant byte gets stored first, or at the lowest address. In a big endian system the most gets stored in first, or at the lowest address.

So what happens if you are trying to read data that has been stored in a big endian system?

Say you have 0xDEADBEEF stored like so:

Memory:
a => DE
a+1 => AD
a+2 => BE
a+3 => EF

if you have a 32 bit pointer address pointing to a+3 the value read will be 0XEFBEADDE. In order to make sense of the value you will need to swizzle it. In other words; you will need to change the order of bytes.

Here is code which will swizzle a 32 bit value:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#define CODETOSWIZZLE 0XEFBEADDE
int main()
{

unsigned int swizzledCode = \
((CODETOSWIZZLE & 0x000000FF)<<24)\
+((CODETOSWIZZLE & 0x0000FF00)<<8)\
+((CODETOSWIZZLE & 0x00FF0000)>>8)\
+((CODETOSWIZZLE & 0xFF000000)>>24);

printf("Swizzled %x", swizzledCode);

getchar();
return 0;
}

Monday, August 31, 2009

Palindrome Test

The palindrome test is very common in interviews. Here is a program that shows how to find out if a string is a palindrome.

Please note that this a simple program as in an interview not much more is expected from you. The test is not meant to commercialize your greatest palindrome rendition. We will not be buying palindrome testing software in the near future. The point of this test is to know your basic knowledge of string manipulation and algorithm generation. This program takes a string and tests whether the string is a palindrome or not. The palindrome can be one word long, or a whole sentence, and can have upper and lower case letters. Note that palindromes are not case sensitive. Also, the palindrome should ignore punctuation marks.

Please note that in my simple algorithm I am assuming that regular ASCII characters are used.

Algorithm:

1) Get string and lets call it testItem;
2) Strip all the punctuation marks, spaces, and symbols,
and store in testItemClean.
3) Reverse the testItem and store it into reverse.
4) Compare testItemClean and reverse.

To find more about palindromes please visit here .


#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main()
{
char testItem[] = "Was it Eliot's toilet, I saw?\0";
int i = 0;
int currentPointer = 0;
int cPointerClean = 0;
int length = (strlen(testItem))-1;
char *reverse;
char *testItemClean;

reverse = (char*) malloc (length*sizeof(char));
testItemClean = (char*) malloc (length*sizeof(char));

for(i=0; i<=length; i++)
{
/*Reversing testItem and storing stripped
off symbols into *reverse*/

if((testItem[length-i]>=65 && testItem[length-i]<=90) \
||((testItem[length-i]>=97 && testItem[length-i]<=122)))
{
if(testItem[length-i]>65 && testItem[length-i]<90)
testItem[length-i] = testItem[length-i] + 32;
reverse[currentPointer] = testItem[length-i];
currentPointer++;
}

/*Stripping testItem off symbols and storing into
testItemClean*/

if((testItem[i]>=65 && testItem[i]<=90) \
||((testItem[i]>=97 && testItem[i]<=122)))
{
if(testItem[i]>65 && testItem[i]<90)
testItem[i] = testItem[i] + 32;
testItemClean[cPointerClean] = testItem[i];
cPointerClean++;
}

}
if(!strcmp(reverse, testItemClean))
printf("It's a palindrome");
getchar();
}

Bit counting (how many 1's in 5 [binary])

This code is simple and can be enhanced to work in more general cases. If you do end up changing any of the code and wish to post it, please feel free to do, but, do post a link back to this site.

Since so many of you have had problems in interviews regarding bit counting, I have decided to post this program. It counts the amount of 1's that there are in any 32 bit number.



#include <stdio.h>

#define BIT_MASK 0x1

int main()
{
int number = 52;
int i = 0;
int counter=0;
/*Looping for each bit in an int.*/
for(i = 0; i < 32; i++)
{
/*
RF1 -> Shift to the right by 1
52 is 110100 in binary.
Bitwise AND 0 with 1 ---> 0 -> RF1
0 with 1 ---> 0 -> RF1
1 with 1 ---> 1 increase counter -> RF1
0 with 1 ---> 0 -> RF1
1 with 1 ---> 1 increase counter -> RF1
1 with 1 ---> 1 increase counter -> RF1
...
It will continue 32 times except that
in this case (52) the rest will be 0s.
*/
if(number & BIT_MASK)
counter++;
number = number >> 1;
}


printf("The number of 1's in %d is: %d", number, counter);
/* Visual Studio will close the output window very fast.
This trick will keep it open.
*/
getchar();

}

Saturday, August 8, 2009

In Penang - Malaysia

So I have been in Penang - Malaysia for the last 3 weeks. I still have one more week to go. I am here on a business trip doing some embedded software development with Intel. And that's the most I am going to say about that.

I have to tell you, I am very impressed with Penang. When I arrived three weeks ago I wasn't sure about what to expect, or better yet, I expected different things. I had just returned from a business trip to Shanghai and while I loved the city and its people, I had some trouble getting used to the food. Being from Canada I am used to a different type of Chinese food. I arrived in Shanghai a month ago looking for chicken balls and sweet and sour chicken and instead found myself ordering from menus written completely in Chinese and having to pick based on pictures; and these sometimes deceive you. I learned towards the end of my trip that the taste of food I am more accustomed to is the Hong Konk style. So, while I was impressed with the explosion of modernism in high-rise, neon light, big screen Shanghai, and very thankful to the hospitality of its people, the trip left me worried about what to expect in Malaysia.

As soon as I landed in Penang I began to notice that most signs were written in English. Then, when I arrived at The Equatorial Hotel the whole 20 hours travel time began to look like it all might have just been worth it. But the Equatorial will be a subject of another post. This is just a look over Penang. I am very glad to say that most here speaks English very well. It is very easy to get around and I need not worry about getting lost. If I do, I get a cab and the driver will understand what I am saying. And the food...The food, as Robert from Everybody love's Raymond might say, "the food is unbelievable." The melting pot of cultures in Penang makes for an excellent dish. The most common restaurants are what the locals call Hawker places. These are food stands with a number of tables and chairs shared by all the stands. You walk and order the food you like from the different stands and tell them where you are sitting, they will then find you and bring you the food. In Hawker places you can find anything from Chinese, to Indian, to Italian food. As far as the food goes, I am doing very well.

Please keep tuned as I will keep writing about my Penang experiences.