Friday, May 19, 2006

What are the main advantages of using a pointer?

A pointer is a memory address to a variable.
Using a pointer is advantagous for 3 main reaons:

1. They allow functions to modify their calling parameters when passing by reference.
2. They provide a fast way to access array elements.
3. They support linked lists, hash tables and other dynamic data structures.


What are storage class specifiers?

when we talk about Storage classes in C++ we are usually talking either about extern or static. The four Storage Class specifiers are as the following:

extern, static, register, auto

extern is used for global variables during seperate compilation. When used the compiler doesn't create storage for the variable when it sees it again.

static local variables retain their values within the function that they are declared between calls but are unknown to the rest of the program (unlike globals). they are very important for making stand-alone functions.

static global variables: When static is used on a global variable, it makes the variable known only to the file in which it is declared.

register is used for variables that are heavily used and tell the compiler to hold the value inside a register (although this does not actually happen with moden computer systems). register variables cannot be declared as globals and can only be local.


In summary the static keyword lets you hide portions of your program from other portions which is very handy for large and complex programs.


Thursday, May 18, 2006

What is a Hash Table?

A hash table is a data structure optomized for searches.
With a hash table search times are improved when compared to a linked list or even a binary tree structure.
Inserts and space required are compromised to improve search times.
A hash table is an array implementation in which we decrease the size of the array by allowing for collisions.
To reduce the number of collisions, the number of elements in the array should be twice the size of the elements we wish to track.

cellIndex = value % tableSize;

We can then handle collisions by chaining (duplicates are put into a linked list) or by linear probing or quadratic probing (since values often group together).
When doubling the array size we must also remap the previously inserted values according to the new table size.

linear probing:
cellIndex = Hash(X) % tableSize;

cellIndex = Hash(X)+1 % tableSize;
cellIndex = Hash(X)+2 % tableSize;
cellIndex = Hash(X)+3 % tableSize;
...
quadratic probing cellIndex = value % tableSize;...
cellIndex = Hash(X) % tableSize;
cellIndex = Hash(X)+(1^2) % tableSize;
cellIndex = Hash(X)+(2^2) % tableSize;
cellIndex = Hash(X)+(3^2) % tableSize;
...

In summary, most databases (large ones) use hash table data structures.



What the heck is going on. Why can't I put any titles to my posts suddenly.

What is the difference between Free and Delete

Delete is used in C++ where as Free is used in C. They are both used for memory management and for releasing a chunk of memory which you have allocated using either Malloc in C or New in C++. The main difference between the two is that by using Delete you don't need to do any typcasting as to what type of data you want to free up. Using Free, you would have to typcast.