Posts Tagged ‘php’

A simple, tiny, Unix Shell written in C language, opensource

Friday, May 9th, 2008

BD-shell is a project I started about a month ago, which aims to implement a tiny, simple, clean unix shell written in C language. It's an academic project. The Operating Systems Course at my University requires this project as part of the assesment.
I decided to publish the source code and to release it under the GPL, for two reasons:

  1. Free software is better! Others can learn something from what I learned
  2. Free software is better! I can learn something from what others learned

As always, I accept every kind of suggestions!

Learn more about the project and download the code at this page:
http://bd-things.net/projects/bd-shell/

  • Share/Save/Bookmark

Hash Maps with linear probing and separate chaining

Monday, April 28th, 2008

Time for two new C programs! At the DSA course I learned something about Hash Tables and collision resolutions.
I just implemented insert/search/print operations.

The first source code is an implementation of a Hash Map with open addressing (linear probing) as collision resolution method.
The following are the interesting functions of the program. As always, take a look at the source code for comments:

// hashMapLinear[] is the hash map
void linearProbingInsert(int value){
    int probe = hash(value);
    while (hashMapLinear[probe]!=0){                            
        probe = fmod((probe+1),SIZE_HASH_MAP);
    }
    hashMapLinear[probe] = value;
}

int linearProbingSearch(int value){
    int probe = hash(value);  
    int i;
    for(i=0;i<size_hash_map ;i++){    
        if(hashMapLinear[probe]==value)
            return TRUE;                            
        probe = fmod((probe+1),SIZE_HASH_MAP);              
    }
    return FALSE;                                          
}
 

Download: hash-map-linear-probing.c

The second program is an implementation of a Hash Map with chaining as collision resolution method.
Interesting functions:

// t_hashTableNode is a struct that is created as single linked list
void chainedHashInsert(int value){
    int probe = hash(value);                        
    if(hashMapChained[probe] == NULL){          
        hashMapChained[probe] = malloc(sizeof(t_hashTableNode));
        hashMapChained[probe]->value = value;
        hashMapChained[probe]->next = NULL;
    }else{
        t_hashTableNode *hashTableNode = hashMapChained[probe];
        while(hashTableNode->next!=NULL){
            hashTableNode = hashTableNode->next;
        }
        hashTableNode->next = malloc(sizeof(t_hashTableNode));
        hashTableNode->next->value = value;
        hashTableNode->next->next = NULL;
    }
}

int chainedHashSearch(int value){
    t_hashTableNode *hashTableNode = hashMapChained[hash(value)];
    while(hashTableNode!=NULL){
        if(hashTableNode->value==value){
            return TRUE;
        }
        hashTableNode = hashTableNode->next;
    }
    return FALSE;
}
 

Download: hash-map-chaining.c

  • Share/Save/Bookmark

Insertion Sort on Linked Lists

Wednesday, April 9th, 2008

I'm pleased to publish this insertion sort implementation on single linked lists developed with my collegue Rigel.
It's a nice experiment in asymptotic complexity of O(n^3). The high complexity is due to the necessity of retrieving the neighbours of the node handled, a difficult operation when using single linked lists.

Download: Insertion Sort on Single Linked Lists

  • Share/Save/Bookmark

A Generic Quicksort Implementation in C

Monday, April 7th, 2008

As assignment for Data Structures and Algorithms course, we had to work with a modified version of the quicksort algorithm. It came obvious that for modifying a qsort you need to implement it :-)

It is difficult to find a clear quicksort algorithm implemented, so I wrote it.

Here is the generic C implementation of the Quicksort Algorithm, which sorts an array in place, following the Divide And Conquer design.

Download: quicksort qsort in C language

Interesting methods (look at the source code for comments):

int partition( int A[], int left, int right) {
        int pivot;
        pivot = A[right];                                                                      
        while(1){
                while( A[right] > pivot){
                        right--;
                }
                while( A[left] < pivot){       
                        left++;                                
                }
                if( left < right ){                                                            
                        swap(A,left,right);
                }else{
                        return left;                                   
                }
        }
}

void quicksort( int A[], int left, int right){
        int m;
        if( left < right ) {                           
                m = partition( A, left, right);        
                quicksort( A, left, m-1);              
                quicksort( A, m+1, right);
        }       
}
 

As you see, there is nothing optimized in this implementation. It just looks elegant and easy to be understood.
If you would like to have a more optimized version of this algorithm, take a look at this one.

  • Share/Save/Bookmark

Object Oriented Memory Management

Wednesday, March 19th, 2008

Major Update on 10th April 2009, inclusion of C++ programming language!
Updated on 18th April 2008, a complete example on stack and heap
Updated on 15th April 2008, new contents and new layout!
Updated on 6th April 2008, new contents!

The paper you can download from here is about a model for memory management during the execution of programs written in Java and C++.

It started on March, 2008 as a summary of the lecture notes of both the "Programming Project" and
"Software Engineering Project" courses held by professors of the CASE (Center for Applied Software Engineering) of the Free University of Bolzano - Bozen.

The first versions of this publication were only about Java memory management.
Subsequent revisions added information found on other sources. Unfortunately, the author forgot to
reference the sources on the document.

On March, 2009 the author began to add the information about C++ programming language. More
information from other sources were added, including their attribution.

The biggest source of this document is still the set of presentations of CASE.
The code snippets and their corresponding stack/heap diagrams are copied in full from those of the
slides.

The next major revision will contain original images (not belonging to CASE slides), as well as other code
snippets that I could find more clear than those of CASE.

If you find that this document contains information taken from one of your publications, please
contact the author, that is willing to either delete them from this document or to add an attribution to
your work.

Download the PDF of the summary

Table of Contents:

  • The model
  • Code load and execution
  • Activation Record (AR)
  • Contents of the Activation Record
  • Abbreviations for
  • Declaration vs. Definition
  • The scope of a variable
  • Extent of a Variable
  • Blocks
  • Scope Activation Record (SAR)
  • Example on SAR
  • Role of SLs
  • Dynamic Memory Allocation And Handling
  • Dynamic Vs. Static memory allocation
  • Dynamic Memory Scope and Extent
  • Accessing dynamic memory
  • Classes
  • Objects
  • Object instantiation
  • Objects in Memory (Java)
  • Objects in Memory (C++)
  • Memory Management issues (Java)
  • Memory Management issues (C++)
  • Methods
  • Methods (Java)
  • Methods (C++)
  • Attributes
  • The null value (Java)
  • The NULL value (C++)
  • Parameter
  • Parameter Passing (Java)
  • Example of parameters passing (Java)
  • Example of parameters passing (Java), continued
  • Parameter Passing (C++)
  • Example of parameters passing by value (C++)
  • Example of parameters passing by reference (C++)
  • Pointers vs. Parameters (C++)
  • Previous example using pointers (C++)
  • Constructor
  • Inline initialization
  • A constructor's call (Java)
  • Class attributes
  • Example of class attributes (Jav)
  • Example of class attributes (C++)
  • Class Method
  • Example of Stack/Heap Diagrams in Java
  • Code
  • Stack Diagram
  • Heap Diagram
  • Memory portions assigned to a program (code area, heap / dynamic memory area), execution stack
  • How code is loaded in Java
  • The Activation Record (AR) and function calls
  • Abbrevations (AR, RV, RA, SP, N/E, @, ??, arb)
  • Examples on method calls and activation records usage
  • Declaraion vs. Definition of a variable, the scope of a variable, blocks
  • Scope Activation Record (SAR), Static Link (SL), the role of SL
  • The extent/lifetime of a variable
  • Dynamic memory allocation and handling
  • Dynamic vs. Static memory allocation
  • Dynamic memory scope and extent
  • Accessing dynamic memory
  • Classes and Objects in detail, object instantiation
  • Memory Management issues
  • Objects vs. Variables (definitions)
  • Methods of Objects
  • Class Attributes
  • The null value
  • Parameters (formal, actual), parameters passing (by reference, by value)
  • Constructors and Inline Initialization
  • Constructor's call
  • Class attributes (static variables)
  • Class methods (static methods)
  • Complete Example of Stack/Heap Diagrams

Everything is integrated with simple examples.

Download the PDF of the summary

  • Share/Save/Bookmark

Sorting array elements with C language

Saturday, February 23rd, 2008

UPDATE 17:19: it seems that the program I implemented today uses a kind of Bubble Sort algorithm, give it a try, it's quite interesting!

After 3 long days studying C, I think I've assimilated a good knowledge base for the incoming semester.
So there is a tiny C program that sorts the elements of a given integer array, using pointers:

Download the source code (well commented)

#include <stdio.h>
void sortArray(int *firstElement, int *lastElement);
void swapArrayElements(int *firstElement, int *secondElement);
void printArray(int array[], int arraySize);

void sortArray(int *firstElement, int *lastElement){
        int *currentElement = firstElement;
        while (firstElement != lastElement){
                while(currentElement != lastElement){
                        if(*currentElement < *firstElement){
                                swapArrayElements(currentElement,firstElement);
                        }
                        currentElement++;
                }
                firstElement++;
                currentElement = firstElement;
        }
}

void swapArrayElements(int *firstElement, int *secondElement){
        int tmp;
        tmp = *firstElement;
        *firstElement = *secondElement;
        *secondElement = tmp;
}

void printArray(int array[], int arraySize){
        int counter = 0;
        while(counter<arraySize){
                printf("%d\n",array[counter]);
                counter++;
        }
}

int main (int argc, char *argv[]){
        int array[] = {2929393,1,23239,-66,15,4,3,0,112,45,3,1000,19};
        int arraySize = sizeof(array)/sizeof(array[0]);
        int *firstElement = &array[0];
        int *lastElement = firstElement + arraySize;
        printf("-------------------------------------------------\n");
        printf("Elements of the array:\n");
        printf("-------------------------------------------------\n");
        printArray(array,arraySize);
        printf("-------------------------------------------------\n");
        sortArray(firstElement,lastElement);
        printf("Elements sorted:\n");
        printf("-------------------------------------------------\n");
        printArray(array,arraySize);
        printf("-------------------------------------------------\n");
}

 

A better program should ask the user to input the array elements, and a better algorithm should not scan every array element n times, where n is the number of the elements.
But I wrote it just for fun and for learning C pointers. I will learn to do better in the Data Structures and Algorithms course in the next semester ;)

  • Share/Save/Bookmark