Wednesday, September 23, 2015

Variable attributes: Visibility - Scope - Lifetime

Ø  Variable Attributes:
Ø  Visibility:
Ø  void func()
       {
        int x;
        {
         // x is visible here
        }
       }

Ø  void func()
       {
        int x;
        {
         int x;
         // outer x is not visible here
        }
       }

Ø  Scope:
è Global scope – file scope – function scope – block scope
è In the previous example:
Ø  outer x scope is its block (outer block)
Ø  but it is not visible in the inner block (due to inner block variable with the same name)

Ø  Life Time:
è For local variables:
Ø  static: lifetime = program time
Ø  automatic: life time = function time
Ø  dynamic: malloc() and free()
è For global variables (global and file local)

Ø  lifetime = program time

if-elseif Statement: Most common conditions shall be handled first

·         Discuss the overhead of branching and why the most common case should be tested first in the (if – else) if construct to speed up execution.
o   Modern processors use pipelining.
From Wikipedia
o   This means that multiple commands are served in parallel

o   Ex:  in clock cycle 4
§  Cmd1: Fetch from memory
§  Cmd 2: Decode to the ALU
§  Cmd 3: Execute
§  Cmd 4: Write the result to the register or the memory
o   This requires that the CPU know the next commands to execute
o   If we have conditional branches, then, the CPU will not be able to know the next commands, and then it will not be able to utilize the pipelining.

o   Hence, it is recommended that the most common condition be the first condition in if-else statement, so we do not have a lot of conditional branching.

Create/Build C Project in VS C++

How To Create C Project in VS C++?
To edit your C program:
  1. From the main menu select File -> New -> Project
  2. In the New Project window:
    Under Project types, General > Empty Project
    Name your project, and specify a location for your project directory
    Click 'OK', then 'next'
  3. In the Application Wizard:
    Select Console application
    Select Empty project
    Deselect Precompiled header
  4. Once the project has been created, in the window on the left hand side you should see three folders:
    Header Files
    Resource Files
    Source Files
  5. Right-click on Source Files and Select Add-> New Item
    Select Code, and give the file a name
    The default here will be a file with a *.cpp extension (for a C++ file). After creating the file, rename it as a *.c file.
To compile and run:

  1. Press the green play button.
  2. By default, you will be running in debug mode and it will run your code and bring up the command window.
    To prevent the command window from closing as soon as the program finishes execution, add the following line to the end of your main function:
    getchar(); 
    This library function waits for any input key, and will therefore keep your console window open until a key is pressed.
  3. Another alternative is to use: 
    Or debug > Start without debuging
Precompiled Headers
Precompiled headers are a mechanism to speed up compilation by creating a partially processed version of some header files, and then using that version during compilations rather than repeatedly parsing the original headers.

Resource Files in VSC++
There are 3 folders in the “Solution Explorer Window” in VSC++:
·         Header Files: (.h)
·         Source Files: (.c)
·         Resource Files: (exe-images-...etc)

Win32 Vs Win64
·         Win32 has 32-bit address line à can support up to 4 GBytes RAM
·         Win64 has 64-bit address line à can support larger size RAM
·         Win32 Application shall run on both Win32 and Win64 system
·         Win64 Application shall run only on Win64 system

Assuming that we use Win64 machine for development, and we want the program to run on Win32 machines:
Host Platform: Win64 – Intel Processor
Target Platform: Win32/64 – Intel Processor

Notes:

  • Some compiler fires a warning in case of using undefined function. And assumes it is externed.
  • If the linker did not find the function externed, then it will fire an error
  • The manifest is not important for c, you can run the exe directly
  • Generated exe file is not pure machine code? it is OS dependent!

Sunday, September 20, 2015

Memory

Memory Types:


  • RAM
    • Data memory
    • Volatile
    • Fast
  • EEPROM / Flash
    • Program memory
    • Nonvolatile
    • From Wikipedia
    • Slow
  • Virtual memory
    • The OS combines regions of the RAM + regions of the Hard disk, and provide virtual addressing for them as if they are contiguous, to be used as a RAM
    • Have larger RAM, but slower
  • Cache memory
    • Faster than rams
    • Data cache, contains the data that is accessed frequently
    • Program cache, contains instructions that is being executed


Memory Management
  • The OS is responsible for allocating/deallocating/moving the heap/stack of the different processes

Friday, September 4, 2015

#define macro with a return value!

Can be done in gcc:

#define IS_DIGIT(x) ({\
int l;\
if (x == 1){\
l = 1;\
}else{\
l = 0;\
}\
l;\
})

The above macro returns an integer l. Can be used as follows:

int x = IS_DIGIT(1);

switch vs if

if the cases of switch are consecutive numbers, then switch may be more optimized than if

Example:

If worst case--> 4 comparison

If (x == 1){
    body 1;
else if (x == 2){
    body 2;
else if(x == 3){
    body 3;

else if(x == 4){
    body 4;
}

Assembly:

beq x,1,L1
beq x,2,L2
beq x,3,L3
beq x,4,L4
...
...
...
L1:
Body 1

L2:
Body 2

L3:
Body 3

L4:
Body 4




switch worst case --> 1 comparison

switch (x){
    case 1: 
        body1;
        break;
    case 2: 
        body2;
        break;    case 3: 
        body3;
        break;    case 4: 
        body4;
        break;}

Assembly:

jumpIfLessThan x,4,switch+x-1
...
...
...
switch:
    L1
    L2
    L3
    L4

...
...
...
L1:
Body 1

L2:
Body 2

L3:
Body 3

L4:
Body 4


Modern compilers output the same assembly for both switch and if