Sunday, March 12, 2023

Notes on C

 Function is a callable block in assembly.  It means the block is marked with a label at the beginning so it can be jumped to symbolically.

Function defined with implementation details placed before (function) main served double up as a function declaration (signature that other function can call accordingly).

The main function is called by one of the exec family call and value is return to exec call when the C program ends.

Command line can be passed to the main function as argument.  The argument are passed as an array of string.  It can be declared as char *argv[] or char **argv.

There is no boolean type in C.  Integer (int) is used to represent false with a value of 0.  1 or any positive value represent true.  While (1) represent an infinity loop with a always true condition.

Condition used in control statement (for, while, do, switch etc) must be enclosed by parenthesis.

For SWITCH statement, execution will start from a matching case statement and continue from there straight until it reach a BREAK or end of SWITCH.  In other words, it will also execute codes in ensuing cases that following the matching case.

Array variable is a type of pointer.  It points to the first item of an array.  So is a function name.  It can be passed as a pointer as an argument to another function.

Pointer arithmetic convert an offset to its product with the item size.  For example, arr+3 = arr+(3*4) for an array of integer.  The compiler will perform the offset arithmetic on behalf of human.

There are 4 storage classes.  extern and static are for both function and variable.  auto and register are applicable to variable only.  

Variable is defined when it is assigned a value.  Variable is declared when no value is assigned.  There can only be 1 definition statement for variable in a program but there could be many declaration.

int a = 10; /*definition*/

int a; /*declaration*/

Variable defined outside a block is either static or extern (default).

Variable defined within a block is either register or auto (default).

When a variable is defined as extern or static, it exists through out the program's life time.  

A static variable declared in a function (block scope) is initialized once even the function may be called repeatedly.  A static variable has default value 0 if it is not initialized.

A static variable declared outside a function (outside a block) has scope from the point of declaration to the end of the file containing the declaration code.  It is not visible outside the file (i.e. in other program files that may be compiled together).

A extern variable has global scope if it is defined (initialized) in one file and is declared in other files that require access to the variable (and that are to be compiled together).

register class is typically not required anymore as compiler would optimize the code by assigning variables to register for efficiency.  If compiler deems no register is available, variable defined as register will be treated as auto.

volatile is not a storage class.  When variable is defined as volatile, the programmer informed the compiler to avoid optimize it by assigning it to register.  For example, if a variable is to be updated by ISR (e.g. placement for received byte), the variable needs to be in memory rather than register.

Function can be defined as static or extern, but not auto or register.  A static function is visible within the file containing the function.  It is similar to private function in C++,  A extern function is visible outside the file it is defined when the other file declares the function.



Saturday, February 25, 2023

java class in a class

A class defined in another class is called a nested class when it has the static keyword and inner class if it does not has the static keyword. A inner class does not has a reference to its outer class but an inner class has. In other words, a inner class can access the outer class instance variable

Monday, April 30, 2018

Scarborough Fair

The main melody is from an old song. The canticle refers to the counterpoint added to harmonize the main melody.

In Medieval times, the herbs mentioned in the song represented virtues that were important to the lyrics. Parsley was comfort, sage was strength, rosemary was love, and thyme was courage.

The things the singer asks for in Scarborough Fair are all impossible. A shirt with no seams. An acre of land between the sea and the seastrand (beach). To harvest grain with a leather blade (leather isn't very sharp), and bundle the stalks with heather (a fragile twig-like flower; doesn't make for very good rope or string).

Saturday, March 3, 2018

JDBC Connection

The early version of JDBC provided a Driver class with a GetConnection method for application.  Underneath Driver uses Java Socket class to get a connection to the database.  Later, DriverManager class provide the connection using an url.  Using a generic interface saved the application the need to know which specific driver to use.  The connection returned is a physical connection (a socket) which is adequate for a 2-tier client-server architecture as each client will create and use its own connection and seldom sharable.

JDBC v2.0 uses DataSource to support Java Transaction API and a 3-tier architecture.  An intermediate layer (application server) sits between client and the database.  DataSource returns a logical connection from a connection pool which improves performance by avoiding set up time for every JDBC connection request. 

Sunday, December 24, 2017

Volatile Variables and Restricted Pointer

A variable defined as volatile if the value of the variable may change outside the program.  For example, the variable value may be changed by hardware or by another program (multi-processing).  The compiler will reload the value from the memory every time before the variable is used.  This will disable some code optimization.

A pointer defined as restricted means the associated memory is only pointed by this pointer.  This allows the compiler to optimize code.  As there no other pointer points to the same memory area (pointer aliasing), the compiler may not save the value back to the memory immediately after it is changed.

Sunday, November 5, 2017

Scale

A note an octave higher than another is vibrating exactly 2 times of frequency of the lower note. The Just (meant correct, true) system divide an octave using notes which is simple faction of the base note.  For example, the fifth is 2/3 of the frequency of the base note.  The problem is that the Just system notes may not harmonized so well among them except with the base note.  For example, the tonic and the fifth frequency coincide every a few cyclerates and thus they sound harmonized.  But the 2nd (8/9) and 6th (3/5) coincide every 27 cycles and 40 cycles respectively and thus they do not sound harmonized.

The equal temperament system uses formula instead of fraction to divide the scale.  Specifically, ratio of the frequency for every 2 consecutive semitone are the same.  Both Just and ET system use 440 Hz for A.  Comparing with Just system, the note in ET is slightly out of tune with the tonic t still close enough to sound harmonized.  However, while not all chord in Just sound harmonized, all chord in ET sounds moderately harmonized.  This is the main difference for the 2 systems

Applying Chord

Typically, chord is chosen based on the rhythmically emphasized notes, which usually the first note  in each bar.  As melody is made up by a stream of notes usually rise or fall in small tone step, not every notes in the bar would fit to the chord chosen.  Choose the chord which included most of the notes in the bar.  When transiting from one chord to next, use inverted chord could "smooth" out the transition.  For example, to transit from C to F

C: C, E, G
F: F, A, C

Each note transverse a distance of 4 tones

If we use a inverted chord for F would reduce the movement for the tones between the 2 chords

C: C, E, G
F: A, C, F

Similar we can invert C to achieve similar effect

C: E, G, C
F: F, A, C