Wrong coding in c++

Use exception instead return boolean

Sample code as below:

try {
    // Do something here, which may throw exception
} catch (MyException& e) {
    if (e.getError() == MyExceptionA) {
        // Do another normal operation

Throw exception in loop


why use json


JSON(Javascript Object Notation)

  • JSON的如下优点,使得它快速成为最广泛使用的序列化协议之一。
    • 这种Associative array格式非常符合工程师对对象的理解。
    • 它保持了XML的人眼可读(Human-readable)的优点。
    • 相对于XML而言,序列化后的数据更加简洁。 来自于的以下链接的研究表明:XML所产
    • 它具备Javascript的先天性支持,所以被广泛应用于Web browser的应用常景中,是Ajax的事实标准协议。
    • 与XML相比,其协议比较简单,解析速度比较快。
    • 松散的Associative array使得其具有良好的可扩展性和兼容性


exception vs error code

TODO: To be continue


Linux c/c++ coverage testing tool

gcov & lcov

  • Reference http://sdet.org/?p=212
  • step 1: add optional in Makefile, and build with make coverage=yes
    ifeq ($(coverage), yes)
    CXXFLAGS += -fprofile-arcs -ftest-coverage
    LINKERCXX += -fprofile-arcs -ftest-coverage
    OPT_FLAGS = -g3
  • step 2: run test files
  • step 3: generate text result by gcov source_file.cc, see result with vim source_file.cc.gcov
  • step 4: generate html result by lcov -c -d ./ -o app.info and genhtml app.info -o cc_result


Detect c/c++ memory overflow

Understand the process address space

  • 0 to 0x08048000, keep for OS, not used.
  • 0x08048000 to 0x40000000
    • read only of code and data
    • read and write of global variables
    • heap (malloc and free)
  • 0x40000000
    • mmap allocate memory start from here, grow up
  • 0xC0000000
    • the stack allocate start from here, grow down
  • 0xC0000000 to 0xFFFFFFFF, use for kernel space

Stack and heap

For multiple threads program, OS will allocate a stack for every threads.

  • Stack
    • ESP register point to the top of stack
    • EBP register point to the function action record
    • Parameters be pushed to stack from right to left
    • Return big data structure
    • Stack [from high to low]
      • Parameter N
      • Parameter …
      • Parameter 2
      • Parameter 1
      • EIP (Next instruction address)
      • EBP
      • local variable 1
      • local variable 2
      • local variable …
  • Heap
    • malloc and mmap
      • malloc for small block memory, address bellow 0x40000000, and extend by brk/sbrk
      • When big block memory using mmap, address upper 0x40000000.
    • malloc: put the memory size before the real memory space to user


  • stack overflow
    • failures
      • changed global variable
      • task not work, function not work
      • some local variable be changed
    • reason
      • thread stack too small
      • defined too large local variable
      • function called too deep
  • global or heap overflow
    • failures
      • global variable be changed
      • memory leak
      • thread-A delete object but thread-B modify object again

Detect tools


memory program and debug

categories of memory error

  • memory leak
  • memory overflow
  • dangling pointers

how to detect memory error

  • ps -aux, check VSZ, e.g:
    • ps -aux |grep -E 'VSZ|2892.*firefox' |grep -v grep
    • watch -n 5 'ps -aux |grep -E "VSZ|2892.*firefox" |grep -v grep'

static program analysis tools

  • pc-lint
  • spint, a tool for statically checking c programs for
    vulnerabilities and coding mistakes.

detect methods

  • mtrace
  • valgrind


Database knowledges


  • DDL is Data Definition Language (DDL) statements are used to define the database
    structure or schema. Some examples:

    CREATE - to create table in the database
    ALTER - alters the structure of the database
    DROP - delete objects from the database
    TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed
    COMMENT - add comments to the data dictionary
    RENAME - rename an object

  • DML is Data Manipulation Language (DML) statements are used to managing data
    with schema objects. Some examples:

    SELECT - retrieve data from the a database
    INSERT - insert data into a table
    UPDATE - updates existing data within a table
    DELETE - deletes all records from a table, the space for the records remain
    MERGE - UPSERT operation (insert or update)
    CALL - call a PL/SQL or Java subprogram
    EXPLAIN PLAN - explain access path to data
    LOCK TABLE - control concurrency

  • DCL is Data Control Language (DCL) statements.

  • TCL is Transaction Control Language (DCL) statements.


  • ACID
    • Atomic 原子性
    • Con 一致性
    • Isolation 隔离性
    • D 持久性
  • 并发场景
    • 脏读:
    • 不可重复读
    • 幻读


  • structure
    • B-/B+ tree