2015年4月7日星期二

Learn about 1NF, 2NF, 3NF, BCNF in five minutes! Come in!(Database)

If you have learnt about these normalization, but still cannot make it clear. I simplify it for you.
1.1NF
| changed to 1NF


2.2NF
| changed to 2NF

3.3NF
| changed to 3NF


4.BCNF
| changed to BCNF
There we go!


2015年4月5日星期日

Ubuntu-- Enable Root Login

    If it is your first time to use Ubuntu, you cannot access Linux system as a root user. 
    That is because of the difference between Ubuntu installation without starting a root account and setting a root password and the regular Linux installation. 
    So, we can use "sudo".  Sudo is a tool for a common user to have the access permission as a super user. It is very easy. You can use "$ sudo passwd root" and then input a password you specify twice. After doing this, you can switch to a super user by using "$ su".
    There are two ways for you to make a root account.
    (1)use $ sudo passwd root;
        The system will let you input your password twice.
         Then,use the command $gksu /usr/sbin/gdmsteup. Several years ago, Linux operator uses $sudo /etc/X11/gdm/gdm.conf, but now gdm has changed a lot. If you cannot find gksu command, first you should install it.
        when done, use "$gedit /usr/etc/gdm/gdm.schemas" command, put the root in the below schema to above schema, then you can use root account to login.
    (2) on the system desk, open system>security>allow local system administrator login.
There we go!

2015年3月31日星期二

The difference between Hashtable and HashMap in Java

1. Hashtable is synchronized, while HashMap is not. Hashtable can be used for multiple threads. Since lock takes time, it degrade the performance with Hashtable. HashMap is only used for non-threaded application. But, actually, this buys you very little because we need synchronization in many applications. So we use equivalently synchronized HashMap which can be obtained by:

But also, you need to correctly implement this logic with additional synchronization form:

So, if the map was modified, it is still not thread safe unless you guard them. But you can solve the problem by thread safe check-then-act semantics like:
2. Hashtable does not allow null keys or values, but HashMap allows one null key and any number of null values.
3. HashMap can have predictable iteration order by swapping out LinkedHashMap. But it is not easy with Hashtable.
4. Hashtable is almost used for legacy API. 

Print the modified date of the file in Linux

There are many ways to check the modified date of the file.
(1) For simplify, use ls -l
(2) use stat
   Here,
   1)the 'find' command will print the modification date for every file recursively.
   2)sort -n(numerically) -r(for reverse) or you can use "sort -nr"
   3)head -n 1: get the first entry or use "sed 1q".
(3) use -printf to avoid calling stat repeatedly
    Now, there we go!

2015年3月30日星期一

Memory Allocation and Vtable

1. There are four members in C++: static data member, non-static data member, static member function and non-static member function.
2. There are mainly five kinds of memory allocation:
(1) stack: created by program, and deallocated by program. Usually, function parameter, local variable and return value in this part.
(2) heap: no predetermined memory size. Programmers apply some sizes of memory and manually released the memory when not need it, or when program ends, the memory was reclaimed automatically.
(3)global and static(.bss): for global and static variable.
(4) literal constant(.data): for const string.
(5)code(.text):executable code.









3. non-static data member is put inside each object as its exclusive data. Static data member, static member function and non-static member function are all extracted into static data area and shared by all the objects. So only one copy is allowed. Only data belongs to one object and non-static data is binded by "this" pointer with the object. Each member function is shared by all the objects to perform the same actions but operator this data members.
4. Virtual table(V-Table)
(1) Single inheritance
In memory, first, there is a virtual table pointer vtbl, then data from base class and after this, data from the derived class. The vtbl points to the virtual table in which there are the addresses of all the virtual functions terminated with NULL. If the function in the derived class overrides the function in the baseclass, the function in the derived class replaces the function in base class.

In this diagram, v2 in class B overrides v2 in class A, v1 in class C overrides v1 in the class A and v3 in class C overrides v3 in Class B.
So, the memory module likes this.

(2) Multiple inheritance
Like single inheritance, all the virtual functions are in the virtual table, but for multiple inheritance, there are many virtual tables. If the function in the derived class overrides the function in base class, the virtual function in the derived class replaces the one in base class in the virtual table, but if new function created in the derived class, this function will be added into the virtual table.

In this diagram, v3 in class D overrides v3 in class B, a new function created in Class D.
So, the memory module likes this.

(3) virtual inheritance
There is only one base class in virtual inheritance. Compared to multiple inheritance, vbtable is added to store the offset to the base class.

In this diagram, vB in class D2 overrides vB in class B, class GD overrides vD1 in class D1 and meanwhile vB in Class B. new function created in Class GD.
So, the memory module likes this.
Now, there we go!

Find the bug in this code(2)

The wrong code looks like this:







    First, let me explain the key word constexpr: it is for evaluating the value of the function or variable at compile time. When used in an object declaration it implies const, instead used in an function declaration it implies inline.
    Here, we define a struct Int with a variable and a function called iter. In main function, we instantiate the template. 
    The declaration template<int i> constexpr auto iter(...)->... is instantiated with i = 0, which forces the evaluation of decltype(iter(Int<-1>{})). The reason for this is Since definition instantiation occures after template overload resolution. So the template iter<0> is never instantiated. Both triggers infinite instantiation. How should we change it?









Here, you go! 

Find the bug in this code(1)


The wrong code looks like this:












1. In this code, malloc function has no specific value, but from the function prototype, we know the default return value is (void *). So we have to use typecast(char*). 
2. Another problem in this code is  p += strlen(name); Now p has point to the end of the first string, so after assign p with '@', the *p is '@', with 'gre' disappears. So the result will be '@research.att.com'. 
3. in the strlen usage, now we prefer strcpy_s(which is in the package window.h). MS went on a "these standard functions aren't safe!"rampage a couple years ago and deprecated several standard functions, replacing them with _s(safe) alternatives.
The right code is:







The result shows on the console.