Thursday, November 27, 2008

C/C++ puzzler #2 - Is it typo ?

Ok, here is next C/C++ specific puzzler. Puzzler here is to guess, what will be result of program ?

  1. It won't compile

  2. It will compile, but it will segfault when we try to run it

  3. It will run and print 4 times 30

  4. Anything else you can think of



Highlight text bellow code to find out correct answer.


/* compile with cc -g -o test_typedef_sizeof test_typedef_sizeof.c */
#include <stdio.h>
#include <string.h>

typedef char name_t[30];

void fn(name_t value)
{
printf("%d\n", sizeof(name_t));
printf("%d\n", sizeof(value));
}

int main (void)
{
name_t aa;

printf("%d\n", sizeof(name_t));
printf("%d\n", sizeof(aa));

fn(aa);
return 0;
}



I'll just show you program output:

30
30
30
4

Is it not, what you have expected ? Fine, let's walk the program together. On entry to main() program prints sizeof(user defined type name_t), which has length of 30 chars. Next it'll print sizeof(instance of name_t) which again is 30 chars long. Then it calls function fn(instance of name_t), In fn() it prints sizeof(user defined type name_t) which is again 30 bytes. But when it tries to print sizeof(value) it prints 4, because arrays are passed as reference to first element in C/C++. And the sizeof(pointer) is 4 respectively 8 for 64 bit system.

Java EE factsheets

Stefan Jäger recently posted bunch of Java EE fact-sheets on his blog. Stefan created fact-sheets for: SF and SL session beans, MDB's and JMS, Transactions and security, and finally JPA. Very nice, and available as PDF downloads.

Thursday, October 30, 2008

C/C++ puzzler #1 - Where do you want to go today ?

First puzzler is in C/C++ and it is based on puzzler found in book Java Puzzlers by Neal Gafter and Joshua Bloch.
Problem:
We have following C program:
/* compile with: cc -g -o test_xxx test_xxx.c */
#include <stdio.h>
#include <stdlib.h>

int main (void)
{
printf("firefox.exe");
http://www.google.com;
printf(":new\n");
exit(0);
}

Options:
What will the program do:
a) print firefox.exe
b) run firefox.exe open http://www.google.com in new tab
c) fail to compile
d) other


Solution: (highlight to see)

First look at source, and you probably think that this couldn't compile. In fact it does. Real puzzler is, why does it compile. Problem seems to be in line http://www.google.com; But let's take a closer look. Is it problem ? It compile because of long forgotten feature of C, which is labels. (Except for kernel source, you'll find plenty of labels there ;) What compiler see is label http: followed by comment //www.google.com;

Puzzlers series

I guess everyone have been in situation when he/she has written code, which didn't behave as it should. Most of these times, problem was of PEBKAC (problem exists between keyboard and chair) type. Computers (nearly) always do, what they're told to do. Under influence of Java Puzzlers book by Neal Gafter and Joshua Bloch, I've decided to start series of posts to this blog, which would cover situations when program doesn't do what we think it should. At first posts will be mostly in C/C++, but i think more languages will be covered in the future. Posts will be formated, so that you'll see only problem with possible answers. Solution will have font color set to background color, or some close color, so you'd have to highlight block in order to see it. You're more than welcome to add your own puzzlers. Just contact me.

Thursday, October 16, 2008

Java Puzzlers

Oh my - oh my - oh my. Yet another funny presentation, this time by Joshua Bloch (Author of Effective Java) and Neal Gafter (Java Puzzlers: Traps, Pitfalls, and Corner Cases, closures proposal for Java7). Presentation is from JavaPolis 2007 and it's called Java Puzzlers/Overview. It's interesting even for non Java programmers.

Tuesday, September 23, 2008

Rules of API design

Joshua Bloch, author of Effective Java writes article about key principles of designing API's. However not only API developers should read this, because as first points states: "All programmers are API designers".
These principles are also mentioned in his original presentation:
How to Design a Good API & Why it Matters.

Wednesday, August 27, 2008

C++ coding style

Ever needed guide for c++ coding style ? Google recently came up with this.

Wednesday, March 19, 2008

Code WTF #1

  
def logResult(mti,resp,data)
if resp != nil
super(mti,resp,data)
else
super(mti,nil,data)
end
end

Saturday, March 15, 2008

dog - better cat than cat

Every *nix user has used "cat" command. It takes one or more files or stdin, concatenates them and puts result into standard output. Common use case is to print out contents of some file to console.
$ cat ~/myfile.txt
Situation is nowadays different from the time cat was born. At that time, users were usualy connected to one mainframe and all was done on local system. Today we use web2.0 applications, which spread across several systems,. We use our computer as terminal for network services. That's why enhanced version of cat, that supports network URL as source and/or target, several string conversions, newline conversion, and more has emerged. The name of this utility is dog (after all, dogs are much more usefull. Don't you think ?).

Friday, March 7, 2008

Ruby documentation

I found The Pragmatic programmers guide to Ruby most usefull. Also I printed out Small Ruby reference leaflet. You can find very nice Ruby tutorial at rubylearning.com.

Wednesday, March 5, 2008

Things I hatedon't like about Ruby

Ruby is quite cool, I like lot of features, but for me as C/C++ developer there are some things, which are quite annoying. Please note, that I'm aware that lot of issues I mention here are actually Ruby features. This post will be dynamic, so more items will be eventualy added. For each issue, I'll try to search for solution or workaround. Also I encourage you to post any ideas you might have about solving these problems.

  • multiple classes per file
    I know that you can have more classes in C++ as well, but I like Java way. I also know that Ruby doesn't force you into writing multiple classes per file, but it also doesn't discourage people that write spaghetti code. If you end up maintaining project like this, it's hell.
    Solution: Use search to navigate project, or refactor to class per file.

  • variables doesn't have to be declared
    This is anoying, if you have to study some old code. In Java/C++ world you have to declare class/object variables. If you look at the code after some time you have all members of the class neatly organised at declaration section. In ruby you're free to use variable wherever you like, without first declaring it.
    Solution: correct way of using members is to use accessors: attr_reader, attr_accessor, and attr_writer.
    Well ... one friend had good note: "Always code, as if the guy which end up maintaining your code is violent psychopat, who knows where you live."

  • arguments lack type information
    This is related to previous item. Coding with strongly type-based languages is quite addictive. Switching to Ruby, you'll start missing those: "cannot cast x to y" errors from compiler.
    Solution: ?

Monday, March 3, 2008

Eclipse and Java VM memory tuning

How to improve performance of eclipse instalation ? Well, I'm sure there are lot of posts about this on the internet, but it all narrows down to few steps:
  1. get rid of modules you don't use
  2. tune heap size of JavaVM
  3. tune perm size of JavaVM
Let's look closely at these steps.

1. get rid of unused modules
I like to test new features and modules for eclipse. But after some time I found out that these modules consume lot of memory and increase startup time. You're likely to accept slowdown due to some module that you realy use and it improves your coding. But modules, which you use only ocasionaly should go, or at least you should disable them.

2. tune JavaVM heap size
All objects instantiated during runtime live in heap. Therefore, the more heap you have, more likely all objects will fit there. JavaVM needs to have all of it's VM memory online in physical memory. You can specify maximum size of JavaVM memory with -Xmx<size>. Attempt to allocate more than -Xmx memory will end with OOM exceptions. On the other hand there is -Xms option to JavaVM. With this you specify how much of -Xmx should be directly allocated by JVM. Specifying this to same size as -Xmx means there will be longer startup, but application will run smother.

3. tune JavaVM perm size
The perm memory is the area of the VM, which is used to store data structures and class information. In case you have project with huge amount of classes, JVM may reach the limit of MaxPermSize. Simmiliar with -Xmx and -Xms there are two options:
-XX:PermSize=<size>
-XX:MaxPermSize=<size>

Use -vmargs as parameter to eclipse.exe together with these parameters to tune performance of your eclipse. My values are: -vmargs -Xms256m -Xmx256m -XX:PermSize=64m -XX:MaxPermSize=64m

Wednesday, February 27, 2008

RDT - IDE for Ruby development

I've been assigned to a project which is developed in Ruby. Because I'm not familliar with Ruby, I decided to use some IDE to work on the project. Because i like Eclipse for java/c++ development, my first choice was to look for Eclipse based IDE. Quick google search returned RDT - or rubyeclipse. Of course I've also bookmarked Ruby Tutorial. I'll post more after I play with it a little bit.

Wednesday, February 20, 2008

Optimize your web apps

While browsing the web, I found nice plugin for firefox called YSlow. It shows you slow aspects of displaying your page. Is it javascript,css,long HTML options ... it's all covered. On the page you can find links to podcast and screencast covering YSlow functionality.