The “C” Preprocessor

Some of the important changes will be done to the source code before it is handed over to the “C” compiler by the “C” Preprocessor.   We use Pre processor directives to get work done using Pre processor.   #include is a preprocessor directive which is used to include the elements of the corresponding file mentioned; Say you have used #include<stdio.h> This asks the pre processor to copy all the elements(definitions here) in the stdio.h file into the present file in place of the Pre processor directive(if angular brackets(<>) for a file then It is meant to search for the file in the standard compiler include paths and if double quotes (“”) are used it searches in the path mentioned in between the quotes. Say you use #define VARIABLE VALUE and say you used that variable you defined with the #define directive at different parts of your code the Pre processor replaces all the places where there is presence of VARIABLE with VALUE.

Conditional Compilation is an important feature of Pre processor Directives;  For this you use

#if boolean
//execute this code
#elif boolean
//execute this code
#else
//execute this code
//endif

//There boolean may be some statement which finally gets evaluated to a boolean

You can also have conditional Compilation as below

#ifdef MYDEF
//execute the code if MYDEF is defined i.e #define MYDEF value is done somewhere
#endif

#ifndef MYLIB_H
//usually this is used when you include your own .h file and making sure that type one is not included before i.e you do as below
#include “mylib.h”

#endif

Adding Two Numbers Without Using Arithmetic Operator “+”

Ever Wondered how to do math on a computer without having arithmetic operators; Oh God!!! Luckily we have them;  Here’s a post which is an alternative for adding two numbers without using Arithmetic Operators;  The strategy that we follow is to go back to the basic level of bits and bytes and what happens to binary state of numbers when we add them and then establish a paradigm to obtain the relation between the final pattern of the bits and the initial bit patterns of the two numbers;  Here’s the algorithm

Say you have to add two numbers a,b; The strategy is when you & two bit patterns you get one’s only the places where you get a carry 1 and a xor b will just give the correct sum of the individual bits without considering any carry’s i.e i.e you add 1 1 then you get zero in xor and the carry is taken care from a & b bit pattern;

int a,b,carry;

while(b!=0){

carry=a&b; // This bit pattern is for the carry i.e you are checking for the bit pattern of the carry

a=a^b;        // Now to get the sum of the bit pattern without considering any carry’s

b=carry<<1;//making a left shift of the carry as the carry bit goes left on each computation

}

Now printing a will give the sum of the initial b with a

Arrays and Pointers

Arrays and pointers are really intimidating at the first go. Here are some of the intricacies with the Arrays and Pointers
Arrays and Pointers are equivalent but not equal It’s the array indexing and pointer arithmetic that is similar. Arrays are the constant pointers i.e always an array reference(or variable) points to the first element of the contiguous memory allocated like if arr is the array then a reference variable always points to first element of the array i.e &a[0] or &*(arr+0).  One cannot assign to some other location like arr=&b or arr=arr+1 i.e one cannot change the location to which the reference variable or the name of the array is pointing..,

Coming to the 2D arrays:
In 2D arrays say you have an array say:

char ttt[3][3] = {{'x', 'x', 'o'},
                  {'o', 'o', 'x'},
                  {'x', 'o', ' '}
                 };

Now the memory allocation would be like

2D array memory representation
and the whole 2D array would like the above figure with contiguous memory allocation

Lets say you have int arr[10];

int *a = arr;

Here when you write arr[3] it actually means at the location represented by arr move by 3 places and read the value, when you write a[3] it means get the location value stored in a and move by 3 places and read the value. The value returned by both of them would be the same. This similarity in accessing the elements by the pointers and arrays is exploited in using pointers as function parameters instead of arrays.

&arr will give a pointer to an array of 10 ints whereas &a will give a pointer to an integer.  Though the value returned by both are same i.e address of the first element of the array but the type of the pointers are different.

An L-value  of type array-of-T which appears in an expression decays into a pointer to the first element of the array with the following 3 exceptions:

  1. As an argument to sizeof() function, :sizeof(arr) will give size of array wheras sizeof(a) will give size of a pointer on your machine i.e 4 bytes or 8 bytes.
  2. & operator: &arr will return the address of the first element in the array i.e array’s address but &a will return address of the pointer
  3. Literal string initialiser for a character array: char a[ ] = “hello”; char *a=”hello”; In the first case a doesn’t decay into a pointer but retains it’s behaviour as an array of int’s for the initialisation to take place.

Here a is a pointer to an int or integer. Now let’s go to 2D arrays say we have int arr[rows][Ncolumns] Now arr is pointer to array of Ncolumns ints and hence arr+1 would take you to the next column in Ncolumns. (Got It???)

So How do you usually declare a pointer to an array:

int *ip;       //This declares pointer to an integer

int *ip[3];   //This declares an array of pointers(here pointers of integers) where the array size is 3

int (*ip)[3]    //This declares pointer to an integer array of length 3

Hence It’s clear now that the reference variable or the name of the two dimensional array(of ints here) used is an array to No. of columns sized integer array(here);

Hence If you would like to have an iterator(or would like to pass it as an argument in a function) over an one dimensional array use int *p and If it’s two dimensional array then use int (*ip)[Ncolumns];

src:http://www.fredosaurus.com/notes-cpp/arrayptr/23two-dim-array-memory-layout.html

Performance Optimisation in using two dimensional arrays

Here’s the thing we always write a double for loop for iteration over two two dimensional arrays Yes! we did it many a times as we have to do it.  Here’s a way to optimize the way you write that nested for loop

Usually we can write the nested for loops in the following two ways both does exactly same but when it comes to optimization in execution time First Method wins over the Second Method

First Method:

for (int i = 0 ; i < width; i++) 
{
    for (int j = 0; j < height ; j++) 
    {
        testArray[i][j] = i * j;
    }
}

Second Method:

for (int j = 0; j < height ; ++j)
{
    for (int i = 0 ; i < width; ++i) 
    {
        testArray[i][j] = i * j;
    }
}

The reason is:

In the First Method  you iterate over element by element and once you finish a row you go to the next row hence in this case mostly you use a contigous memory location and you jump over for the next element only when you have to change a row after iterating over elements in that row and in this cache is in you support and when you fetch data from a memory location cache does a house keeping of storing data in immediately next memory locations and this help for a faster iteration.

In the Second Method you jump over the entire row for each element and as the element in the next row is usually not immediately the next memory location you aren’t just using your cache and this reduces the performance of your code.   I was really impressed when I read this article Though it looks obvious for many people realizing even the simplest things is sometimes non-trivial:)

src: http://blog.drorhelper.com/2009/04/performance-optimization-tip-understand.html