Sunday, August 21, 2016

Every thing you do I can do it with a double pointer in C

It is 2016, low level coding is not needed anymore. We have programming languages like javascript and python. When people code everything with a class and related methods. But do you remember we have a programming language called C? When we had no class, templates, operator-override or implicit constructor at that time. You actually know what are doing with C, no magic!

I claim no expert to programming, since I only have a four-year-old programming life. But even you are inexperienced like me, you should still be able to code some thing in C. IT IS fatigue to code in C, you have no standard containers to rely on, you spend 10 hours to build a perfect `deque` data type which works on amortized O(1) complexity and has no memory leaks before you are able to work on your project. Awesome!!! But why? Why try to build a huge robust data  structure in a non-OOP language? Instead of trying simulate constructors and destructors, you could always use a double pointer to do the job. Have you ever seen Linus Torvalds's double pointer code? It removes on special case where you would make mistakes. 

But really? what can you do with a double pointer in C? Well, Suppose I have `vector` data structure and I didn't want to write the code like this:
     
     struct vector {
            size_t elem_size;
            void *arr;
     };

we can actually just use a simple array to do the job. If you just pass the `void *` to insert function, how can I modify the size of the vector, or insert anything before header? This should not be a problem with this:

     void insert(void **arr, void *elem, size_t *remain_size, size_t *size) {
        size_t old_size = *size;
         if (*remain_size == 0) {              
                 *arr = realloc(*arr, 2 * *size);
                 *remain_size = *size;
                 *size *= 2;
         }
         *remain_size -= 1;
     //copy the elem to the end
     }

Well, at least you don't want the methods that bad anymore.

Monday, August 8, 2016

6 years of programming

 I have been studying in programming for 6 years, since my first day of undergrad life. I haven't realized it has been so long, this studying life already took away most of my youth. I was a kid without knowing the toughness of life, to a young man need to worry about jobs and how to start having family.

The computer industry changed vastly in this period. There was no Cloud Computing, Machine Learning was only known in Academics. Universities taught students how to code in VC++. Now everything is different.

What was my path? Well, 5 years ago, a friend of mine taught me how to install Linux on a laptop, it all started there. Firstly it was Debian and Ubuntu, Debian was at version 5 and it did not even support WPA2 encryption.

    "Why my laptop cannot connect to WI-FI?",
    "My X.org crashed again after I installed the proprietary graphics driver",
    "I ran that script, I don't know what is going on in the manual, it should work, right?".
    "How to get my speaker to work, I installed Alsa already".

I switched between Debian and Ubuntu for several times before I followed the installation guide of Arch Linux :p.

I started to live under a terminal with 'grep', 'cd', 'ls', 'find', 'cat', etc. Then there is the story of how-to-vim. Adapting to hjkl, Esc and i, then you tried to copy other people's vim script, how to use ctags with vim, how to use YouCompleteMe. Writing dozen vim code without knowing why. VimScript is a hell of hack anyway. So my system was basically terminals with editors and a browser.

Wait, I haven't talked anything about programming yet,  should I started at "hello world"? No, lets started at CSAPP (if you haven't read it yet, you really need to, it is a good book), you know there is a stack and a heap in your memory, and modern systems use "flat addressing", between these two, people found there are still some space for shared libraries. Every instruction that CPU executes follow the same pipeline, and I can actually execute multiple instructions simultaneously. Do you want your program to hit the cache every time? Just joking.

But you won't be able to find a job with C, right? Why the hell would any company would want employees to code in C? 2010s, it is all about javascript, this has been true for almost 6 years, I still haven't start learn javascript. But at least, I learned Python. Really, first time I did something besides computing and digits.

2012, it was just the time of machine learning, python was the first few languages that developed libraries with it.  Numpy and Scipy, strangely, they were the first two libs I used just after the night I learned python(You can learn python in one night, it is true). That was just 2 years ago.

Before I even realized my undergraduate life ended, the graduated offer came to my hand. I don't know what's ahead of me. But anyway, I was young.

With a full head of unknowns, I flew to Canada. No more undergrad type of large classes, what you have in graduate life is just small classes and deadlines. Gee, they really passed so fast.

2016, I am in a Computer Vision lab now, just recalled the path I walked, just realized that my youth almost ends.