Friday, April 5, 2013

size_t is not unsigned int !!

I always consider size_t is just a typedef of unsigned int and that drops me to the torment today for hours. 

The thing I am trying to do is simple. It's a socket write and read. 

write:
...
size_t length = _value.size();

boost::asio::write(*sock, boost::asio::buffer((const char*)&length, sizeof(length)));
...

read:
...
int length = -1;

boost::asio::read(*sock,
            boost::asio::buffer(&length, sizeof(length)));
...

And this bit of code stirs up the whole program. 

After hours of probing around, I happened to find out the issue is caused by with size_t. Because its size is different from int on the receiver end.

Here is the nice explanation from wiki:

The C language provides the separate types size_t and ptrdiff_t to represent memory-related quantities. Existing types were deemed insufficient, because their size is defined according to the target processor's arithmetic capabilities, not the memory capabilities, such as available address space. Both of these types are defined in the stddef.h header (cstddef header in C++).
size_t is used to represent the size of any object (including arrays) in the particular implementation. It is used as the return type of the sizeof operator. The maximum size of size_t is provided via SIZE_MAX, a macro constant which is defined in the stdint.h header (cstdint header in C++). It is guaranteed to be at least 65535.

It's clearly said size_t is platform dependant. Cool, it's portable. With a small program, I found size_t is 8 bytes on my machine, and int is 4.

More info for size_t here : http://www.embedded.com/electronics-blogs/programming-pointers/4026076/Why-size-t-matters  Altougth I haven't read it, could be interesting.

fun!fun!fun!

Friday, January 18, 2013

Hadoop RPC is not RMI

I never closely looked IPC protocols in hadoop. Just run into this today, and luckily, found the explanation from the inventor:


Why use Hadoop IPC over RMI or java.io.Serialization? Here's what Doug has to say:
Why didn't I use Serialization when we first started Hadoop? Because it looked big-and-hairy and I thought we needed something lean-and-mean, where we had precise control over exactly how objects are written and read, since that is central to Hadoop. With Serialization you can get some control, but you have to fight for it.

The logic for not using RMI was similar. Effective, high-performance inter-process communications are critical to Hadoop. I felt like we'd need to precisely control how things like connections, timeouts and buffers are handled, and RMI gives you little control over those.

 I am going to brew my own RPC protocol in C++ as well !

Tuesday, January 8, 2013

c++ segmentation fault and undefined behaviour causes



1.I run into a issue when I call a function from a object it throws a Segmentation fault. It turns out to be the problem that I did n't make a copy of dynamic allocated elements when copying a object. The default copy constructor (and/or operator=) may not handle that correctly.

Details are explained well in this post:

http://www.cplusplus.com/forum/general/28420/

2. when you reference a item, but that item was deallocated already.
e.g.

class A {
    tcp::socket _sock;

public:
    void connect() {
        tcp::io_service _io_service;
        _sock = tcp::socket(_io_service);
    }
...
}

When connect() is called _sock is assigned with the connection socket. But _io_service is destructed when connect() finishes.

3. Destructor not virtual.

Tuesday, December 11, 2012

Import hadoop with Eclipse

Eclipse is a good tool for study hadoop code. But it may takes a while to resolve all the links, make tool tip  and declaration jump work.

Here are steps:

1. create a java proj, using j2SE1.5 as your jre (to date it's the highest j2SE you can find in eclipse);
2. symlink src to src in hadoop
3. select proj->Build Path->configure build path, go to library tab
4. click "Add external JARs", and all jars from hadoop dir and lib dir
5. Switch to "Order and Export", check all hadoop-*.jars, click OK
6. select proj -> Refresh

That's it.

When you try to dereference a declaration of a obj, it may prompts you to "Attach file". Just attach to src folder should solve the problem.

 

Tuesday, December 4, 2012

C++ find common factors


It's such easy to do this:

#include <iostream>
using namespace std;

int gcd(int a, int b)
{
if(b == 0)
{
       return a;
}
else
{
return gcd(b, a % b);
}
}

int main()
{
     int a,b;
         
     cout << "Input first number: ";
     cin >> a;
     cout << "Input second number: ";
     cin >> b;
   
     cout << "Greatest common divisior (GCD) is " << gcd(a,b) << endl;
     return 0;
}

Friday, November 23, 2012

can not find by apt-cache -> run apt-get update

I re-installed ubuntu 12.10 today. Then the first thing I like to do is install chrome. I downloaded the deb and run dpkg to install. It prompts a missing package called libgconf2-4. Routinely, I run apt-cache to look for this guy. But I can not find it. This pissed me a bit.

Think it over, I probably need to update my apt-cache. So I run apt-get update. This is better. Now I can find libgconf2-4 from apt-cache.

A good think about apt is it remembers missing packages, so now you just need to run "apt-get -f install", that will install the be spoken libgconf2-4.

nice and easy~ I won't be afraid to run into this again. haha