Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Thursday, December 29, 2011

Calling Member function using NULL pointer

To my surprise, my program did not crash when I called display() by NULL pointer, but it crashed when I tried to access member variable.

#include


#include

using namespace std;

class Test

{

public:

    int p;

   void display() {

       cout << "Test::display()" << endl;

   };

};



int main()

{

    try {

       Test *t = NULL;

       t->display();

       t->p = 5;

    } catch (exception &e) {

      cout << "Dereferencing pointer: " << e.what();

   } catch (...) {

      cout << "Unknown Exception";

   }

   return 0;

}

 

Monday, February 18, 2008

Version Control with Subversion

Revison Control System(RCS), VSS or SVN are few of commonly used Version Control Systems.

Subversion is a centralized system for sharing information. At its core is a repository, which is a central store of data. It is mainly used to maintain source code and files related to a project. It works in a typical client-server model. Any number of clients can connect to server and do read or write operations. But differnce between server and respository is that it remembers every change ever written to it: every change to every file, and even changes to the directory tree itself, such as the addition, deletion, and rearrangement of files and directories. Client can read latest information or information about who did the changes last time, what are the changes he made etc.
Advantage of Subversion system is, if two Users A and B wanst to edit same file F, then they can check out F from repository and get local copies, do edit and commit the changes to repository. Subersion control follows copy-modify-merge model instead of conventional locking model. In this model, if A does changes on F and commits and B also does changes in F and wants to commit, then repository says, F is out of date, just update your file,so that changes done by A are merged. If A and B does changes at same location in F, then repository says F is in conflict state, by showing changes done by A and changes done by B. User has to decide, which changes to keep and which changes to be discard and do necessary changes, should resolve the conflicted state and commit changes.

Few useful commands (for Version Control with Subversion)

svn co --- for check out

svn commit --- for check in

svn up/svn update ---- To update local copy

svn add --- To add a file or directory to svn

svn remove --- To remove a file or directory from svn

svn cleanup --- Recursively clean up the working copy, removing locks, resuming unfinished operations etc.

svn revert --- Restore working copy. Undone changes, if any.

svn resolved --- Remove 'conflicted' status of working copy files or directories.

svn di/ svn diff --- Display the difference between two paths.

svn st/svn status --- To see status of files in local system wrt files in svn(at the time of check out)

The first six columns in the output are each one character wide:
First column: Says if item was added, deleted, or otherwise changed
' ' no modifications
'A' Added
'C' Conflicted
'D' Deleted
'I' Ignored
'M' Modified
'R' Replaced
'X' item is unversioned, but is used by an externals definition
'?' item is not under version control
'!' item is missing (removed by non-svn command) or incomplete
'~' versioned item obstructed by some item of a different kind
Second column: Modifications of a file's or directory's properties
' ' no modifications
'C' Conflicted
'M' Modified
Third column: Whether the working copy directory is locked
' ' not locked
'L' locked
Fourth column: Scheduled commit will contain addition-with-history
' ' no history scheduled with commit
'+' history scheduled with commit
Fifth column: Whether the item is switched relative to its parent
' ' normal
'S' switched
Sixth column: Repository lock token
(without -u)
' ' no lock token
'K' lock token present
(with -u)
' ' not locked in repository, no lock token
'K' locked in repository, lock toKen present
'O' locked in repository, lock token in some Other working copy
'T' locked in repository, lock token present but sTolen
'B' not locked in repository, lock token present but Broken

svn --help --- To get Help on svn commands


@kova

Tuesday, February 5, 2008

A Note on Makefile

GNU make is a utility to maintain groups of programs.

The make command allows you to manage large programs or groups of programs. As you begin to write larger programs, you will notice that re-compiling larger programs takes much longer than re-compiling short programs. Moreover, you notice that you usually only work on a small section of the program (such as a single function that you are debugging), and much of the rest of the program remains unchanged.

The make program aids you in developing your large programs by keeping track of which portions of the entire program have been changed, compiling only those parts of the program which have changed since the last compile.

Syntax:

make [ -f makefile ] [ option ] ... target ...

Usually, name of 'make' file is Makefile/makefile. But it can be any user-defined name.

@> make - will search for default makefile.

To make, makefile with user-defined name, command is
@> make -f myMake

Makefile works based on Rules.

Rules:

A rule tells Make both when and how to make a file.
As an example, suppose your project involves compiling source files main.c and add.c then linking them to produce the executable project.bin. Withholding a detailed explanation for a bit, here is a makefile using GNU C which will manage the task of making project.bin:

The Example Makefile

project.bin : main.obj add.obj
gcc -o project.bin main.obj add.obj
main.obj : main.c
gcc –c main.c
add.obj : add.c
gcc –c add.c

This makefile shows three rules, one each for making project.bin, main.obj, and add.obj. The rules as shown above are called explicit rules since they are supplied explicitly in the makefile. Make also has inference rules that generalize the make process.

Dependency Lines: When to Build a Target

The lines with the colon “:” in them are called dependency lines. They determine when the target is to be rebuilt.

To the left of the colon is the target of the dependency. To the right of the colon are the sources needed to make the target. A dependency line says “the target depends on the sources.” For example, the line:

project.bin : main.obj add.obj 

states that project.bin depends on main.obj and add.obj. At run time Make compares the time that project.bin was last changed to the times main.obj and add.obj were last changed. If either source is newer than project.bin, Make rebuilds project.bin. The last-changed time is the target's time as it appears in the file-system directory. This time is also known as the target's timestamp.

The Make Process is Recursive

It is a basic feature of Make that a target's sources are made before the timestamp comparison occurs. The line:

project.bin : main.obj add.obj 

implies “make main.obj and add.obj before comparing their timestamps with project.bin.” In turn:

main.obj : main.c 

says “make main.c before comparing its timestamp with main.obj.” You can see that if main.c is newer than main.obj, main.obj will be rebuilt. Now main.obj will be newer than project.bin, causing project.bin to be rebuilt.

For more info, $man make



@kova