Stagii pe Bune – what a joke!

Posted by on iunie 18, 2007
Fără categorie / 3 Comments

Stagii Pe Bune is a project started by former students from my university. The idea is brilliant: to offer students with a way to accumulate work experience IN ROMANIA during the summer in internships in some of the best known IT&C companies in the country. The site is quite well-designed (despite a few privacy issues) and easy to use.

The funny (or sad, depending on the number of options you have) part starts when you are contacted by get to deal with the companies. I personally applied to 5 internships, I’ve been called to 4 interviews and went to two of them (my apologies to the Adobe Romania team, I would have really liked to know you 🙂 ).

One of the companies didn’t even look at the position I applied for, they just made a big pool of all the CVs then started pulling from the stack 🙂 It was a matter of luck to get where you wanted, and most of us didn’t. I even ended up in a different division. However, I was the only one to clearly tell them the I was disappointed with the offer and I would prefer to work in the division I applied for. They told me that they’ll forward my CV and will get back to me if I was to pass a second test. The test passed and I had lost all hope. Two weeks later, they call to inform me that their employees decided the first test was enough and they had a position for me. All of my colleagues have a similar story. I wonder if they do the same with the CVs received for ordinary openings (which, btw, never appear 😛 ) or that’s a treatment reserved for students.

The second company had a much more developed company culture. You had to pass 3 interviews, each one weirder than the one before. At first we were impressed by the brand new company HQ downtown, but for the second interview I had to drive all the way to the middle of nowhere, in the „Romanian Silicon Valley” (aka Pipera). There was of course no parking space for the visitors and there was a huge difference between the inside and the outside of the building 🙂 But hey, if I were to have an interesting internship, the place would be less important, right?

So I went in and started the interview. By the middle of the discussion, I was making huge efforts not to laugh. I already knew that the discussion should be „by the (company) book”, but that was hilarious. A very young lady (younger than me, maybe?) was reading the questions and then tried to write down my answers in full, just like a little robot. The one question that I found the funniest was „Let’s do a little role-playing: Imagine that I [the HR person] am Daniel, your best friend. I kindly ask you to break your NDA and let me in your computer. How do you say no?” All that was quite all right, I was ready to put up with it so that I can get to the final discussion with the project manager. But when she told me I had to drive all the way back to Pipera the next morning in rush-hour for a 15 minutes talk with the „man in charge” (as she put it) and that it was my only chance, as he was leaving the country, I just said to myself „thanks, but I’ll pass”.

I know that all companies have their own way when recruiting new employees and the process can seem weird or difficult, but it seems to me that they don’t take the students they accept for internships too seriously. And that’s too bad, because today’s students are tomorrow’s employees and we will remember our experiences. So, a little piece of advice for my younger colleagues: if you feel strong enough to quit at the end of the summer, find yourself a real job. You’ll be better off both financially and professionally.

Call by sharing

Posted by on iunie 15, 2007
Fără categorie / No Comments

In a previous article, I was saying trying to convince you that Java passes its parameters by value. Although this is the most widely-spread name for the action of passing references to objects, some experts have a different opinion. They name this parameter-passing technique „call by object” or „call by sharing„.

The only programming language that openly uses that name is CLU, however one must note that languages like LISP, Scheme, Python or Java use the same technique. You can find a short study on the subject in the python mailing list archives.

The const keyword (part 2)

Posted by on mai 29, 2007
Fără categorie / No Comments

In part 1 we introduced the const keyword. Today we’ll talk about constant pointers.

Let’s say you want to define a constant pointer. Which of the following declarations will you use?


const int* v1;
int* const v2;
const int * const v3;

The answer is: the second or the third. The first declaration defines a pointer to a constant integer and the third one defines a constant pointer to a constant integer.

Another interesting case is when defining a constant char array (credits go to Ulrich Drepper, link via RazvanD):


int main(void)
{
const char s[] = "hello";
strcpy (s, "bye");
puts (s);
return 0;
}

Although this code will give a warning (passing `const char *’ as argument 1 of `strcpy(char *, const char *)’ discards qualifiers is the exact message on Dev-C++), it will run, because s is allocated in the heap, so it is treated much like a pointer. You can force the value to be constant by adding the static keyword, wich will force the compiler to allocate s in read-only memory:

static const char s[] = "hello";

The const keyword (part 1)

Posted by on mai 24, 2007
Fără categorie / 1 Comment

This article will not actually present any tricks, it will be an introduction in the const keyword. In part 2, we will present the const and volatile pointers, which behave a little weird.

First of all, let’s see what the const modifier means in the C standard. Basically, a const variable is one who’s value can’t be changed. Actually, things are not so simple – as we’ll see later, you can change a constant variable. The standard states that „If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined.” On some architectures, constant variables are put in a special section (sometimes called .rodata – from Read Only DATA) of the program by the compiler.

If you want to define a constant in C/C++, you can write:

const int v1 = 0; //the usual way
int const v2 = 1; //also legal

Both declarations mean the same thing: define a new integer with a fixed value. So, if you want to change the value of v1, how would you do it? By using pointers:

const int v1 = 0; //define a constant
int* v2 = &v1; //define a pointer to v1
*v2 = 5; //change the value

Of course, you shouldn’t do that, as the are no guarantees that the result will be what you expect it to be, but with most compilers, v1 will be 5 after running the code presented above.

A sad day for Romania

Posted by on mai 23, 2007
Fără categorie / No Comments

Today, Ceauşescu is coming back to Cotroceni. I’ll spare you the details, we all now what happened on Saturday. I just wonder if the 6.000.000 people that voted for him really can’t see the authoritarian tendencies showed by the „beloved president” or they just don’t care.

Most of his supporters talk about the fight against corruption, but forget that the guy has his own problems with the justice. But this is more that a fight against corruption. If we let him have it his way, we will soon live in a country that will look a lot like this one.

EDIT: Even digg thinks i’m right 😀 :

ceausescu.JPG

Permission denied to call method XMLHttpRequest.open

Posted by on mai 18, 2007
Fără categorie / 1 Comment

May find more here!

Have you ever seen something like that? I recently had a very interesting experience within AJAX scripting. Let’s say you will access http://www.domain.ext, right? Now here comes my problem. There is a great difference between

XMLHttpRequest.open('http://www.domain.ext/file.html', ...)

and

XMLHttpRequest.open('http://domain.ext/file.html', ...)

The result? Opera’s console said just: Error, Internet Explorer said nothing, and only the Mozilla browsers did give me a hint. And the hint… an interesting exception of which I didn’t know anything till that moment.

If, let’s say, you’re accessing http://www.domain.ext and the Ajax script is written for http://domain.ext you will receive a very non explicit exception/error found in the title. After long searches on the internet I could not find anything clear, until…

I remembered that once I received this error within another website I was working at. At that moment I did not realize that I did find a solution, but hey… Who’s perfect?

Here’s a real thing. You know already that Ajax does not load on domain.ext pages from domain2.ext. This is a permission rule that cannot be forced that easy, but can can be avoided though a caching script. But did you know, also that Ajax cannot load a page even from a sub-domain of the domain you’re working with? It’s the same bugging rule that’s restricting this and from what I’ve tested till now, it’s true. And what do you think www.domain.ext is considered for domain.ext? Yes… it’s just an ordinary sub-domain…

The solution? A simple javascript class: location. How? Like this:

XMLHttpRequest.open('http://'+location.host+'/file.html', ...)

This is the solution I found for the problem showed above.

Now… If, for example you’re viewing domain.ext and you want do Ajax load something from sdmn.domain.ext, this is a completely different thing, and again it can be done with the caching script I was talking above. We write a simple php file in domain.ext, which will load the html from sdmn.domain.ext. (I will show how in another article, a little bit later. But you can find it on the internet if u ggl a bit.)

http://en.wikipedia.org/wiki/AJAX
http://en.wikipedia.org/wiki/XMLHttpRequest
http://en.wikipedia.org/wiki/Ajax_framework

Do remember please that Internet Explorer uses other javascript classes for Ajax. XMLHttpRequest is compatible only for Firefox (IceWeasel), SeaMonkey (old Mozilla / IceApe), Opera on Windows / Linux, Konqueror and, I think, Epiphany on Linux and MAC OS X’s Safari. There may be others of which I do not know.

Simple debugging in kernel programming

Posted by on mai 17, 2007
Fără categorie / No Comments

When programming Linux kernel modules, you have limited debugging options. The main way is to use the printk function (the kernel equivalent of printf). If you want to give as much information as possible, you could use some of the macros that the language offers you, such as __FILE__ or __line__. Here is a small snippet you could use in your modules:

#define DEBUG 1
#if DEBUG
#define Dprintk(format, ...) \
printk (KERN_ALERT "[%s]:FUNC:%s:line:%d: " format, __FILE__, \
__func__, __LINE__, __VA_ARGS__)
#else
#define Dprintk(format, ...) do {}while(0)
#endif

You can use the same code in userspace programs by replacing printk with printf. And just in case you’re wondering what’s with the empty do-while, you might want to take a look at this older article.

A little fun with cdecl

Posted by on mai 15, 2007
Fără categorie / No Comments

One of my teachers decided one day that he should write a pointer to an array of functions receiving a pointer to a function receiving an int and returning a pointer to a function receiving an array of pointers to int and returning int and an int and returning a pointer to a function receiving a pointer to a function receiving a pointer to an array of int and an int and returning a pointer to int and returning a function receiving int and returning int.

Fun, huh? 😀 Let’s try one more time:

a pointer to an array of functions
|  receiving a pointer to a function
|               |  receiving an int
|               |  returning a pointer to a function
|               |               |  receiving an array of pointer to int
|               |               |  returning int
|            an int
|  returning a pointer to a function
|               |  receiving a pointer to a function
|               |               |  receiving a pointer to an array of int
|               |               |            an int
|               |               |  returning a pointer to int
|               |  returning a function
|               |               |  receiving int
|               |               |  returning int

It does sounded quite tricky to me. Still, he managed to write it down in just 10 minutes. The secret? Cdecl. Cdecl reads files for statements which should be translated to C or C++. Six different statement are supported: declare composes a C type-declaration, cast composes a C type-cast, and so on. The grammar is described in full in the man page.

And here is the result for the function described above:
int ((*(*f)[] (int (*( * )(int))(int *[]), int))(int * ( * ) (int ( * )[], int)))(int)

What surprises can C# properties hide?

Posted by on mai 10, 2007
Fără categorie / 1 Comment

One of my colleagues was recently working on an app that was supposed to transform a color bitmap into a black and white image. The image was quite large (1600×1024) and the algorithm quite simple (2 nested for’s were used to go through the image).

He had implemented this algorithm before, but with a small difference. Can you tell what the difference is?

public Bitmap Grayscale1(Bitmap srcImg)
{
...
// get source image size
int width = srcImg.Width;
int height = srcImg.Height;
// for each line
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++, src += 3)// for each pixel
{
src[0] = src[1] = src[2] = (byte)(0.2125 * src[2] + 0.7154 * src[1] + 0.0721 * src[0]);
}
...
}
...
return srcImg;
}


public Bitmap Grayscale2(Bitmap srcImg)
{
...
// for each line
for (int y = 0; y < srcImg.Height; y++)
{
for (int x = 0; x < srcImg.Width; x++, src += 3)// for each pixel
{
src[0] = src[1] = src[2] = (byte)(0.2125 * src[2] + 0.7154 * src[1] + 0.0721 * src[0]);
}
...
}
...
return srcImg;
}

The answer: the second program was running much more slowly than the first. (almost 5 times slower, actually) 🙂

At first glance, the programs do the same thing. However, Grayscale1 was using two local variable, width and height to keep the size of the image. At each iteration, instead of calling the Height and Width properties of the image, the program was using the local variables.

So why was this causing such a dramatic effect on the running time? My friend’s idea was that the local variables were kept by the program in the registries, thus allowing for a quicker access time than the properties, which are in fact functions.

However, another programmer decided to do a little bit of profiling on the program and discovered something much more interesting: the getHeight() and getWidth() functions (corresponding to the Height and Width properties of the image) were not just returning a variable, as one might expect, but were actually calling some GDI+ functions.

You can read the original story and the follow-up (both in Romanian). And just in case you haven’t found out just yet, you COULD hit the same problem in C++ (Microsoft C++, that is).

How to read the bits in a IEEE 754 number?

Posted by on mai 08, 2007
Fără categorie / No Comments

That’s one of those questions you always wanted to know the answer to, but never found somebody who knew, right? 😛

Well, in C it’s pretty simple. All you have to do is to declare an union, like this:

union{
float f;
int bits;
}

This tells the compiler that both f and bits should be hold in the same memory zone. This is pretty useful to save some memory, if you know for sure that you wown’t need f and bits in the same time.

As a side effect, if you write a value in f then read bits, you will have all the bits represented in the IEEE 754 standard. Now you can use bit operations (& , | , ^) with your number, you can extract the different parts of the number, etc. Do keep in mind that this is bad programming practice.