Showing posts with label cpp. Show all posts
Showing posts with label cpp. Show all posts

Monday, November 28, 2016

C++ Compile, Link Process and Characters.

C++ compilation is a two-step process. First, the source code is compiled into an object file that contains the machine code equivalent of the source file. Secondly, the linker combines the object files for a program into a file containing the complete executable program. The linker will also integrate any functions from the Standard Library used in the second step.

Imagining the intermediate object files from each .cpp source file are similar to the Java .class files, which you then run with JVM. However, the Java compiler interprets the source code into bytecode that is OS and platform independent and without saying, is not machine code.

Similar to Java, you can compile each source file independently in separate compiler runs. This is convenient since in the coding process, there will be typographical and other errors to be coded iteratively. Even if it compiles, it may have logical errors to be revised.

Regarding Characters

Talking about computer characters, ASCII was defined in 1960s as 7-bit code so that there are 128 code values. ASCII values 0 to 31 represent non-printing control characters such as carriage return (0x0F) and line feed (0X0C). Code value 65 yo 90 are the uppercase letters A to Z and 141 to 172 correspond to the lowercase a to z. The codes for uppercase and lowercase letters are only different in the sixth bit.

Enter Universal Character Set (UCS) around 1990s to overcome the limitations of ASCII codes and extend it to include codes for foreign languages. UCS is defined to code up to 32 bits.

However, it is very inefficient to use four bites when one byte can do the job.

UCS defines a mapping between characters and integer code values, called "code points". The code point is not the same as an encoding. It is an integer that can be represented in different ways of bytes or words in an computer system.

Unicode is a standard that defines the characters with the code points derived from UCS. Remember, with the same identical code point, you can have different encodings. Unicode standards provide such flexibility by dividing the codes into 17 code planes, each of which contains 65,536 code values.

Code plane 0 contains codes from 0x0 to 0xffff and code plane 1 with 0x10000 to 0x1ffff. Naturally code plane 0 contains most national languages.

As mentioned, Unicode provides more than one encoding method. The most commonly used are UTF-8 and UTF-16.

UTF-8 represents a character as a variable length of 1 to 4 bytes with ASCII character set appears in UTF-8 as single byte codes.

UTF-16 represents a character as one or two 16-bit values. UTF-16 includes UTF-8.

Java use UTF-16 unicode to represent internal text.

In C++, the default size of 'char' is 8-bit ASCII code and you can declare it as 'signed char' to have value -128 to 127. You also have wchar_t, char16_t and char32_t to store unicode characters.


Wednesday, November 16, 2016

C++ Reference.

Create Tests
#include 
#include 
using namespace std;

void fa();
void fb();
void fc();
void func  ( const int & i );
void func  ( const string & fs );
void func2 ( const string * fs );
void func3 ( const string * fs );
const char * prompt();
int jump   ( const char * );
void (*funcs[])() = { fa, fb, fc, nullptr };


int main( int argc, char ** argv )
{
    int x = 24;
    string s = "Hello";
    puts ("this is main()");
    func(x);

    x = 73;
    printf ("x is %d\n", x);

    func(&s);
    printf ("string is %s\n", s.c_str());
    func2(&s);
    printf ("string2 is %s\n", s.c_str());
    printf ("returned string is %s\n", func3().c_str());

    // function pointer *fp
    void (*fp)() = func4;
    void (*fp)(&s) = &func4; // same as above
    fp(); // or (*fp)();

     while ( jump (prompt()) );
     puts ("\nDone\n");

    fflush(stdout);
    return 0;
}

void func( const int & i )
{
    // would result in error if you try to change i in function
    printf ("value is %d\n", i);
}

void func( const string & fs )
{
    printf ("String is %s\n", fs.c_str());
}

void func2 (const string * fs )
{
    printf ("String2 is %s\n", fs->c_str());
}

// declare to be const so you can't change the string
const string & func3 (const string * fs )
{
    // declare to be static storage so the stack for function won't 
    //    overflow and create security problem
    // auto is deprecated, because it's default and stored in stack
    // stack is created fresh for each function
    //
    // also if you have to return a reference, declare it to be static
    //     so it can be stored in static storage space
    //     auto storage on stack is small. Use reference if you have
    //     to return big object and return the reference in static storage
    static string s = "This is static";
    return s;
}

void func4()
{
    printf ("String2 is %s\n", fs->c_str());
    puts ("a string");
}

void func4(const string * fs)
{
    printf ("String2 is %s\n", fs->c_str());
    puts ("a string");
}

const char * prompt() {

    puts ("Choose an option:");
    puts ("1. do fa()");
    puts ("2. do fb()");
    puts ("Q. quit");
    puts ("Choose an option:");
    printf(">> ");

    fflush(stdout);                // flush after prompt
    const int buffsz = 16;         // constant for buffer size
    static char response [buffsz]; // static storage for response buffer
    fgets(response, buffsz, stdin);// get response from console
    return response;
}

int jump ( const char * rs ) {
    char code = rs[0];
    if (code == 'q' || code == 'Q') return 0;
    // count the length of the funcs array
    int func_length = 0;
    while ( funcs[func_length] != Null ) func_length++;

    int i = (int) code - '0'; // convert ASCII numeral to int
    i--; // list is zero-based
    if ( i < 0 || i >= func_length ) {
        puts ("invalid choice");
        return 1;
    } else {
        funcs[i]();
        return 1;
    }

}




Sunday, September 18, 2016

C Programming Examples.



  • Swap function: difference between pass by value and pass by reference.
  • Fibonacci series. Two implementation: for loop and recursive.
    • loop:
      int fibo_loop (int n) {
          int j = 1;
          int k = 1;
          int sum;
          for (int i = 2; i < n; i++) {
              sum = j + k;
              j = k;
              k = sum;
          }
          return sum;
      }
    • recursive:
      int fibo_recur (int n) {
          if (n<=2) {
              return 1;
          } else {
              return fibo_recur (n-1) + fibo_recur (n-2);
          }
      }
  • The usage of constant, constant functions
  • Detect sequence pattern in FSM
  • What is the purpose of "volatile" flag in C programming language?