Saturday, January 26, 2013

redesigning in progress

Dear users our website is under maintenance. Please check the new pages to get your required information.

Administrator - ccplusplus.com 

unix tools


UNIX / Linux Tools:

All *NIX and its flavors gives a variety of inbuilt tools that are used widely in development process. The best features of this is that they are designed specially based on that OS's means they understand their system pretty well, and can offer a you a lot to work on. You must have heard of some these like.


1. Valgrind
2. GNU debugger - GDB
3. gcov - the coverage tool
4. tcpdump
5. indentindent man page
6. gprof - the profiling tool
.
.
.
and a lot.

linux tutorial


Linux refers to the family of Unix-like computer operating systems using the Linux kernel. Linux can be installed on a wide variety of computer hardware, ranging from mobile phones, tablet computers, routers and video game consoles, to desktop computers, mainframes and supercomputers. Linux is a leading server operating system, and runs the 10 fastest supercomputers in the world.

Topics:

unix tutorial


Unix (officially trademarked as UNIX, sometimes also written as Unix) is a multitasking, multi-user computer operating systemAT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna. The Unix operating system was first developed in assembly language, but by 1973 had been almost entirely recoded in C, greatly facilitating its further development and porting to other hardware. Today's Unix systems are split into various branches, developed over time by AT&T as well as various commercial vendors and non-profit organizations. The second edition of Unix was released on December 6th, 1972. originally developed in 1969 by a group of

The Open Group, an industry standards consortium, owns the “UNIX” trademark. Only systems fully compliant with and certified according to the Single UNIX Specification are qualified to use the trademark; others might be called "Unix system-like" or "Unix-like" (though the Open Group disapproves of this term). However, the term "Unix" is often used informally to denote any operating system that closely resembles the trademarked system.


Blog Topics

UNIX

1 . UNIX tutorial for beginners
2 . SSH Security and Configuration Basic Overview
4 . SSH Security and Configuration
5 . Private and Public Key pair for SSH
6 . SSH Configuration and its Usage
7 . Configuration SSH with UNIX applications or scripts
8 . Creating a trusted host environment using SSH
9 . Using !$#@*% - pipeling
10. Data redirection using <, << and >>, >
11. stdin
12. Subshells
13. The test commands [ ] and [[ ]]shells
14. Operators in UNIX

UNIX Tutorial

1. Mastering UNIX Part 1 - Mastering the Command Line 
2. Mastering UNIX Part 2 - Think Smarter and Work Smarter 
3. Mastering UNIX Part 3 - Prefer Working with Command Line 
4. Mastering UNIX Part 4 - Dealing with Ownership and Permissions 


Solaris 10/9

advanced c++ programming


The intent of this blog page is to explain the concepts of Inter Process Communication by putting the concepts and the example togather at time where ever possible. Here you will get all the IPC's like socket and the Socket Programming, File, Thread etc.

For a complete understanding you can refer to any of the good books that I am referring below also.

Following blogs are available for advance programming using C, C++. Click on the topic to browse with them.

5 .  Templates


##########################
#  Sample Shell Scripts  #
##########################
  1. Script to calculate the CPU usage
  2. killing a process by its name
  3. shell script to get the ip address
  4. check the kernel version of linux
  5. Get the Linux version 
  6. getopts example 
  7. kill a process by name (method 2) 
  8. get last file modification time  
  9.  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Reference Books:
  1. UNIX Network Programming, Volume 2: Interprocess Communications (2nd Edition)
  2. Interprocess Communications in Linux: The Nooks and Crannies
  3. Interprocess Communications in Linux: The Nooks and Crannies
  4. Pthreads Programming: A POSIX Standard for Better Multiprocessing (O'Reilly Nutshell)
  5. Programming with POSIX® Threads
  6. Multithreaded Programming With PThreads
  7. UNIX Systems Programming: Communication, Concurrency and Threads
  8. UNIX Network Programming, Volume 1: The Sockets Networking API (3rd Edition)
  9. UNIX Network Programming: Networking APIs: Sockets and XTI; Volume 1
  10. UNIX-Netzwerkprogrammierung mit Threads, Sockets und SSL (X.systems.press) (German Edition)
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

basic concepts c++ language programming


C++ (pronounced "see plus plus") is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell  Labs as an enhancement to the C language and originally named C with Classes. It was renamed C++ in 1983.
C++ is one of the most popular programming languages and its application domains include systems software (such as Microsoft Windows), application software, device drivers, embedded software, high-performance server and client applications, and entertainment software such as video games. Several groups provide both free and proprietary C++ compiler software, including the GNU Project, Microsoft, Intel and Embarcadero Technologies. C++ has greatly influenced many other popular programming languages, most notably C# and Java.
C++ is also used for hardware design, where the design is initially described in C++, then analyzed,  architecturally constrained, and scheduled to create a register-transfer level hardware description language via high-level synthesis.
The language began as enhancements to C, first adding classes, then virtual functions, operator overloading, multiple inheritance, templates, and exception handling among other features. After years of development, the C++ programming language standard was ratified in 1998 as ISO/IEC 14882:1998. That standard is still current, but is amended by the 2003 technical corrigendum, ISO/IEC 14882:2003. The next standard version (known informally as C++0x, in reference to the long-standing expectation that it would be released sometime before 2010) is in development; its final draft was approved on March 25, 2011 and the formal specification is expected to be published in the summer of 2011.


XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                       C++ Blog Topics
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

1. All about the array's in the C++
2. C++ Typecasting
3. C++ Typecasting Revisited
4. Preprocessor's in C++ 
5. Structure Padding in C++
6. Copy Constructor & Assignment Operator : The Basic Rule.
7. Auto Pointer's in C++ <auto_ptr>
8. Stack unwinding (C++ only)
9. Virtual Constructor (forcefully achieving the same effect)
10.All About inline function
11.Error handling - a complete guide
12.Pointer's a closer look
13.Why Program in C++
14.Operators - a quick review concept
15.Controls Statements basic
16.Branching Statements (if, else, switch)
17.Loops (for, while, do)
18.What is Function and its Basic
19.Object - What is it ?
20.Object Defined
21.Scope and Storage for a Class and its variables
22.const_cast
23.downcasting
24.namespace std
25.Header Files
26.Allocators
27.const vs #define
28.enum hack in c++
29.Initializer list in c++ (structure)
30.widening in C++

31.escape sequence
32.setprecision function
33.integer overflow
34.scientific notation
35.enum
36.precision of floating point numbers
37.signed and unsigned data type ranges

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                   C++ Saample Codes
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1. Searching a string from a File
2. Sample Makefile to build the c++ code
3. Using namespace in C++
4. Generates a random string of 32 hexadecimal digits
5. Liner Search
6. const_cast example
7. static member variable example
8. static member function example
9. Initializer list
10.using structure
11.typedef
12.modulus operator
13.escape sequence
14.setprecision example
15.scientific notation usage
16.integer overflow
17.ascending order sorting of an array

c language concepts


C (pronounced /s/, like the letter C) is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system.

Although C was designed for implementing system software, it is also widely used for developing portable application software.
C is one of the most popular programming languages of all time and there are very few computer architectures for which a C compiler does not exist. C has greatly influenced many other popular programming languages, most notably C++, which began as an extension to C.

This C blog page is not intend to provide a complete C  language, but instead I will give you the important concepts and will try to explain the toughest concept in C that developers and the students are having. I will keep on posting to this. Please add you comments at any moment if you feel something went wrong, of-course I am human being so mistakes can happen.

Blog Topics
----------------
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                            C Concepts
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1. C Language - The Development History
2. const in C 
3. All about C Pointers
4. File Handling with C
5. Bitwise Operator  
6. Varuous types of C Pointers 
7. Storage-Class Specifier
8. Operator Precedence in C
9. Escape Sequences
10.ASCII Chart
11.Strings
12.Dynamic Memory Management in C
15.malloc memory allocation
16.calloc memory allocation
17.reallocating memory using realloc
18.freeing memory using free
19.auto storage class in C
20.register storage class
21.external variable
22.variable definition vs declaration


XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
                        C Sample Codes
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5 . reverse a number using recursion
  1. Method First
  2. Method Second
6 . covert days to year, months and days
7 . program to print without semicolon
8 . identify given character vowels or consonants
9 . factorial calculator of large numbers
10. sorting an array or numbers given by user
11. finding maximum and minimum number from numbers entered
12. reverse string using pointer
13. compare two strings input by user
14. program to print a asterisk triangle
15. find given number positive , negative or smaller number
16. calculate the prime factor of a number
17. copy one string to another without using string functions
18. calculate square root of a number
19. calculating prime factor of a number
20. program to calculate hcf of the given numbers
21. program to calculate lcm of the given numbers
22. print triangle of numbers in reverse order
23. Matrix addition using array
24. Matrix multiplication using array
25. array sorting in ascending order
26. calculate mean, mode and median
27. swap two numbers without using third variable or number
28. swap two numbers
29. print simple of asterisk
30. insertion sort c code
31. sum of 1 to n numbers using recursion
32. calculating roots of quadratic equation
33. print pyramid of numbers
34. calculate power of a number
35. copy one string to another using pointers
36. strftime sample code
37. search an element in 2Dimentional array using liner search
38. check user entered character like symbol, digit, letter
39. check entered character is small or capital
40. calculate factorial using recursion
41. calculate area and perimeter of triangle
42. calculate area and perimeter of square
43. calculate area and perimeter of rectangle
44. calculate area and perimeter of circle
45. calculate area and perimeter of rhombus
46. calculate area and perimeter of parallelogram
47. calculate area and perimeter of trapezium
48. calculate area and perimeter of quadrilateral
49. calculate area and perimeter of semicircle
50. calculate area and perimeter of sector
51. calculate area and perimeter of sphere
52. calculate area and perimeter of cone
53. calculate area and perimeter of cylinder
54. calculate area and perimeter of cube
55. calculate area and perimeter of cuboid
56. calculate area and perimeter of hemisphere
57. check string is palindrome of not
58. weekday calculator in c
59. convert miles to kilometer
60. convert miles to meter
61. convert miles to feet
62. convert miles to inches
63. convert miles to centimeter
64. struct timeval example c code
65. check year is leap year or not
66. Compare two strings without using strcmp function
67. sort numbers in ascending or descending order
68. sort numbers in ascending order
69. sort numbers in descending order
70. sort numbers
71. calculate length of string without using strlen function
73. fibonacci series using recursion
74. absolute value of a number
75. count number of characters in a file
76. count blank spaces in a file
77. print number of triangle in reverse order
78. Celsius to Fahrenheit conversion
79. gettimeofday
80. register storage example in C
81. auto storage class example in C
82. external variable example
83. variable definition vs declaration
84. chdir system call example

File handling:
85. display content of a file
86. fopen example
87. display content of user entered file
88. count characters in file
89. count number of lines in a file
90. display portion of a file
91. compare two files
100.copy one file content to other file
101.copy one file content file to other file entered by user
102.using enum
103.modulus operator

Thursday, January 24, 2013

dot net developer jobs in delhi ncr


We have an excellent opening with below requirements,

Technical Requirement:
  • Must have 4 - 6 year experience on .Net Technology, in C#, Classic ASP, .Net 3.5, 4.0, 4.5. 
  • Strong in Writing SQL Server Script.
  • Database experience in Microsoft SQL Server 2005/2008, 2008 is must.
  • VB Script, JScript knowledge is good to have.
  • Crystal Report is good have.
  • XML, DHTL, CSS, AJAX.

Work Requirement:
  • Flexible in working completing the target in defined limit. 
  • You will working on Enterprise product development.
  • There will be no salary on annual basis.
  • You will be shared amongst the product overall profit, which will be much higher than your annual salary.
  • Good skill in understanding the existing product and making/implementing the enhancements and bug fixes.
  • You should be able to work online with your hardware and software support.

Interested candidate may send your resume to 
 "support@ccplusplus.com"

advantages of design pattern c++


Some of the advantages of design pattern are as follows,

  • Program Organization
  • Code Quality
  • Design Reuse
  • Code Reuse
  • Part of design vocabulary
  • Maintainability

design pattern advantages


Some of the advantages of design pattern are as follows,

  • Program Organization
  • Code Quality
  • Design Reuse
  • Code Reuse
  • Part of design vocabulary
  • Maintainability

benefit of design pattern


Some of the advantages of design pattern are as follows,

  • Program Organization
  • Code Quality
  • Design Reuse
  • Code Reuse
  • Part of design vocabulary
  • Maintainability

Advantages of Design Pattern

Some of the advantages of design pattern are as follows,

  • Program Organization
  • Code Quality
  • Design Reuse
  • Code Reuse
  • Part of design vocabulary
  • Maintainability

Wednesday, January 23, 2013

gdb tutorial linux




1. How do I use gdb?

When you compile your program, you must tell the compiler to produce a program that is compatible with the debugger. The debugger needs special information to run properly. To do this, you must compile your program with the debugger flag, -g. This step is critical. Without it, the debugger won't have the program symbol information. That means it won't know what your functions and variables are called, and it won't understand when you ask it about them.

1.1 How do I compile with debugging symbols?


Pass the -g flag to your compiler: 
prompt > gcc -g program.c -o programname
NOTE: If you have a larger program with several files, each must be compiled with the -g flag, and it must also be set when you link.
1.2 How do I run programs with the debugger?


First start the debugger with your program name as the first argument. 
prompt> gdb programname 
Next use the run command in gdb to start execution. Pass your arguments to this command. 
(gdb) run arg1 "arg2" ...
1.3 How do I restart a program running in the debugger?


Use the kill command in gdb to stop execution. The you can use the run command as shown above to start it again. 
(gdb) kill
Kill the program being debugged? (y or n) y
(gdb) run ...
1.4 How do I exit the debugger?


Use the quit command. 
(gdb) quit
NOTE: You may be asked if you want to kill the program. Answer yes. 
(gdb) quit
The program is running. Exit anyway? (y or n) y
prompt >
1.5 How do I get help on debugger commands?

Use the help command. Gdb has a description for every command it understand, and there are many, many more then this tutorial covers. The argument to help is the command you want information about. If you just type "help" with no arguments, you will get a list of help topics similar to the following:

(gdb) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help" followed by command name for full documentation.
Command name abbreviations are allowed if unambiguous.


2. How do I watch the execution of my program?
Gdb functions somewhat like an interpreter for your programs. You can stop your program at any time by sending it signals. Normally this is done using key combinations like Ctrl-C for the interrupt signal SIGINT. Outside of gdb this would terminate your program. Gdb traps this signal and stops executing your program. Also, using breakpoints you can have your program stop executing at any line of code or function call. Once your program is stopped, you can examine 'where' it is in your code. You can look at the variables currently in scope, as well as your memory space and the cpu registers. You can also change variables and memory to see what effect it has on your code.

2.1 How do I stop execution?


You can stop execution by sending your program UNIX symbols like SIGINT. This is done using the Ctrl-C key combination. In the following example, I pressed Ctrl-C after 'Starting Program...' appeared. 
(gdb) run
Starting Program: /home/ug/ryansc/a.out

Program received signal SIGINT, Interrupt.
0x80483b4 in main(argc=1, argv=0xbffffda4) at loop.c:5
5   while(1){
...
(gdb)
2.2 How do I continue execution?

Use the continue command to restart execution of your program whenever it is stopped.

2.3 How do I see where my program stopped?


Use the list command to have gdb print out the lines of code above and below the line the program is stopped at. In the example below, the breakpoint is on line 8. 
(gdb) list
3       int main(int argc, char **argv)
4       {
5         int x = 30;
6         int y = 10;
7      
8         x = y;
9      
10        return 0;
11      }
2.4 How do I step through my code line-by-line?


First stop your program by sending it signals or using breakpoints. Then use the next and step commands. 
5   while(1){
(gdb) next
7   }
(gdb)
*NOTE* the next and step commands are different. On a line of code that has a function call, next will go 'over' the function call to the next line of code, while step will go 'into' the function call. 
The next command:
(gdb)
11     fun1();
(gdb) next
12 }
The step command:
(gdb)
11     fun1();
(gdb) step;
fun1 () at loop.c:5
5    return 0;
(gdb)
2.5 How do I examine variables?


Use the print command with a variable name as the argument. For example, if you have int x and char *s: 
(gdb) print x
$1 = 900
(gdb) print s
$3 = 0x8048470 "Hello World!\n"
(gdb)
NOTE: The output from the print command is always formatted $## =  (value). The $## is simply a counter that keeps track of the variables you have examined.
2.6 How do I modify variables?

Use the set command with a C assignment statement as the argument. For example, to change int x to have the value 3: 
(gdb) set x = 3
(gdb) print x
$4 = 3
NOTE: in newer versions of gdb, it may be necessary to use the command 'set var', as in 'set var x = 3'

2.7 How do I call functions linked into my program?


From the debugger command line you can use the call command to call any function linked into the program. This includes your own code as well as standard library functions. For example, if you wish to have your program dump core: 
(gdb) call abort()
2.8 How do I return from a function?


Use the finish command to have a function finish executing and return to it's caller. This command also shows you what value the function returned. 
(gdb) finish
Run till exit from #0  fun1 () at test.c:5
main (argc=1, argv=0xbffffaf4) at test.c:17
17        return 0;
Value returned is $1 = 1

3. How do I use the call stack?
The call stack is where we find the stack frames that control program flow. When a function is called, it creates a stack frame that tells the computer how to return control to its caller after it has finished executing. Stack frames are also where local variables and function arguments are 'stored'. We can look at these stack frames to determine how our program is running. Finding the list of stack frames below the current frame is called a backtrace.
3.1 How do I get a backtrace?


Use the gdb command backtrace. In the backtrace below, we can see that we are currently inside func2(), which was called bu func1(), which was called from main() 
(gdb) backtrace
#0  func2 (x=30) at test.c:5
#1  0x80483e6 in func1 (a=30) at test.c:10
#2  0x8048414 in main (argc=1, argv=0xbffffaf4) at test.c:19
#3  0x40037f5c in __libc_start_main () from /lib/libc.so.6
(gdb)

3.2 How do I change stack frames?


Use the gdb command frame. Notice in the backtrace above that each frame has a number beside it. Pass the number of the frame you want as an argument to the command. 
(gdb) frame 2
#2  0x8048414 in main (argc=1, argv=0xbffffaf4) at test.c:19
19        x = func1(x);
(gdb)

3.3 How do I examine stack frames?


To look at the contents of the current frame, there are 3 useful gdb commands. info frame displays information about the current stack frame. info locals displays the list of local variables and their values for the current stack frame, and info args displays the list of arguments. 
(gdb) info frame
Stack level 2, frame at 0xbffffa8c:
eip = 0x8048414 in main (test.c:19); saved eip 0x40037f5c
called by frame at 0xbffffac8, caller of frame at 0xbffffa5c
source language c.
Arglist at 0xbffffa8c, args: argc=1, argv=0xbffffaf4
Locals at 0xbffffa8c, Previous frame's sp is 0x0
Saved registers:
 ebp at 0xbffffa8c, eip at 0xbffffa90
(gdb) info locals
x = 30
s = 0x8048484 "Hello World!\n"
(gdb) info args
argc = 1
argv = (char **) 0xbffffaf4

4. How do I use breakpoints?

Breakpoints are a way of telling gdb that you want it to stop your program at certain lines of code. You can also have it stop when your program makes specific function calls. Once the program is stopped, you can poke around in memory and see what the values of all your variables are, examine the stack, and step through your program's execution.

4.1 How do I set a breakpoint on a line?


The command to set a breakpoint is break. If you only have one source file, you can set a breakpoint like so: 

(gdb) break 19
Breakpoint 1 at 0x80483f8: file test.c, line 19

If you have more than one file, you must give the break command a filename as well: 

(gdb) break test.c:19
Breakpoint 2 at 0x80483f8: file test.c, line 19  
4.2 How do I set a breakpoint on a C function?


To set a breakpoint on a C function, pass it's name to break. 
(gdb) break func1
Breakpoint 3 at 0x80483ca: file test.c, line 10    
4.3 How do I set a breakpoint on a C++ function?


Setting a breakpoint on a C++ function is similar to setting a breakpoint on a C function. However C++ is polymorphic, so you must tell break which version of the function you want to break on (even if there is only one). To do this, you tell it the list of argument types. 

(gdb) break TestClass::testFunc(int) 
Breakpoint 1 at 0x80485b2: file cpptest.cpp, line 16.
4.4 How do I set a temporary breakpoint?

Use the tbreak command instead of break. A temporary breakpoint only stops the program once, and is then removed.
4.5 How do I get a list of breakpoints?


Use the info breakpoints command. 
(gdb) info breakpoints
Num Type           Disp Enb Address    What
2   breakpoint     keep y   0x080483c3 in func2 at test.c:5
3   breakpoint     keep y   0x080483da in func1 at test.c:10
4.6 How do I disable breakpoints?


Use the disable command. Pass the number of the breakpoint you wish to disable as an argument to this command. You can find the breakpoint number in the list of breakpoints, as shown above. In the example below we can see that breakpoint number 2 has been disabled (there is an 'n' under the Enb column). 
(gdb) disable 2
(gdb) info breakpoints
Num Type           Disp Enb Address    What
2   breakpoint     keep n   0x080483c3 in func2 at test.c:5
3   breakpoint     keep y   0x080483da in func1 at test.c:10
4.7 How do I skip breakpoints?


To skip a breakpoint a certain number of times, we use the ignore command. The ignore command takes two arguments: the breakpoint number to skip, and the number of times to skip it. 
(gdb) ignore 2 5
Will ignore next 5 crossings of breakpoint 2.


5. How do I use watchpoints?

Watchpoints are similar to breakpoints. However, watchpoints are not set for functions or lines of code. Watchpoints are set on variables. When those variables are read or written, the watchpoint is triggered and program execution stops. 
It is difficult to understand watchpoint commands by themselves, so the following simple example program will be used in the command usage examples. 

#include <stdio.h>

int main(int argc, char **argv)
{
 int x = 30;
 int y = 10;

 x = y;

 return 0;
}
5.1 How do I set a write watchpoint for a variable?


Use the watch command. The argument to the watch command is an expression that is evaluated. This implies that the variabel you want to set a watchpoint on must be in the current scope. So, to set a watchpoint on a non-global variable, you must have set a breakpoint that will stop your program when the variable is in scope. You set the watchpoint after the program breaks. 
*NOTE* You may notice in the example below that the line of code printed doesn't match with the line that changes the variable x. This is because the store instruction that sets off the watchpoint is the last in the sequence necessary to do the 'x=y' assignment. So the debugger has already gone on to the next line of code. In the examples, a breakpoint has been set on the 'main' function and has been triggered to stop the program.
 
(gdb) watch x
Hardware watchpoint 4: x
(gdb) c
Continuing.
Hardware watchpoint 4: x

Old value = -1073743192
New value = 11
main (argc=1, argv=0xbffffaf4) at test.c:10
10      return 0;
5.2 How do I set a read watchpoint for a variable?


Use the rwatch command. Usage is identical to the watch command.
 
(gdb) rwatch y 
Hardware read watchpoint 4: y
(gdb) continue
Continuing.
Hardware read watchpoint 4: y

Value = 1073792976
main (argc=1, argv=0xbffffaf4) at test.c:8
8         x = y;
5.3 How do I set a read/write watchpoint for a variable?

Use the awatch command. Usage is identical to the watch command.
5.4 How do I disable watchpoints?


Active watchpoints show up the breakpoint list. Use the info breakpoints command to get this list. Then use the disable command to turn off a watchpoint, just like disabling a breakpoint.

(gdb) info breakpoints
Num Type           Disp Enb Address    What
1   breakpoint     keep y   0x080483c6 in main at test.c:5
       breakpoint already hit 1 time
4   hw watchpoint  keep y   x
       breakpoint already hit 1 time


6. Advanced gdb Features
6.1 How do I examine memory?


Use the x command to examine memory. The syntax for the x command is x/FMT ADDRESS. The FMT field is a count followed by a format letter and a size letter. There are many options here, use the help command 'help x' to see them all. The ADDRESS argument can either be a symbol name, such as a variable, or a memory address. 
If we have char *s = "Hello World\n", some uses of the x command could be: 
Examine the variable as a string: 
(gdb) x/s s
0x8048434 <_IO_stdin_used+4>:    "Hello World\n"
Examine the variable as a character: 
(gdb) x/c s
0x8048434 <_IO_stdin_used+4>:   72 'H'
Examine the variable as 4 characters: 
(gdb) x/4c s
0x8048434 <_IO_stdin_used+4>:   72 'H'  101 'e' 108 'l' 108 'l'
Examine the first 32 bits of the variable: 
(gdb) x/t s
0x8048434 <_IO_stdin_used+4>:   01101100011011000110010101001000
Examine the first 24 bytes of the variable in hex: 
(gdb) x/3x s
0x8048434 <_IO_stdin_used+4>:   0x6c6c6548      0x6f57206f      0x0a646c72
6.2 How do I see what is in the processor registers?


Use the info registers command. The output of this command depends on the hardware architecture. The following is part of the output on an intel machine: 
(gdb) info registers
eax            0x40123460       1074934880
ecx            0x1      1
edx            0x80483c0        134513600
ebx            0x40124bf4       1074940916
esp            0xbffffa74       0xbffffa74
ebp            0xbffffa8c       0xbffffa8c
esi            0x400165e4       1073833444
...
6.3 How do I debug with a core file?


When your program segfaults and leaves a core dump file, you can use gdb to look at the program state when it crashed. Use the core command to load a core file. The argument to the core command is the filename of the core dump file, which is usually "core", making the full command core core
prompt > myprogram
Segmentation fault (core dumped)
prompt > gdb myprogram
...
(gdb) core core
...
6.4 How do I step through my code at the instruction level?

There are two commands, nexti and stepi, that work similar to next and step. See the usage of those commands for an idea of how to use these two.
6.5 How do I see the assembly code my program is running?


Use the disassemble command. The argument to this command is a memory address. Here is an example of the disassembly for the main function of a simple program on an intel machine: 

(gdb) disassemble main
Dump of assembler code for function main:
0x80483c0 <main>:       push   %ebp
0x80483c1 <main+1>:     mov    %esp,%ebp
0x80483c3 <main+3>:     sub    $0x18,%esp
0x80483c6 <main+6>:     movl   $0x0,0xfffffffc(%ebp)
0x80483cd <main+13>:    mov    0xfffffffc(%ebp),%eax
0x80483d0 <main+16>:    movb   $0x7,(%eax)
0x80483d3 <main+19>:    xor    %eax,%eax
0x80483d5 <main+21>:    jmp    0x80483d7 <main+23>
0x80483d7 <main+23>:    leave 
0x80483d8 <main+24>:    ret   
End of assembler dump.


7.1 Example Debugging Session: Infinite Loop Example


We are going to use gdb to discover where the infinite loop in the following program is. It may be obvious to you on inspection, but it is instructive to use gdb to find it. The program should print out all the alphanumeric (letter and number) characters in it's input.


1 : #include <stdio.h>
2 : #include <ctype.h>
3 : 
4 : int main(int argc, char **argv)
5 : {
6 :   char c;
7 :
8 :   c = fgetc(stdin);
9 :   while(c != EOF){
10:
11:        if(isalnum(c))
12:          printf("%c", c);
13:     else
14:          c = fgetc(stdin);
15:   }
16:
17:   return 1;
18: }




The first step is to compile the program with debugging flags: 
prompt> gcc -g inf.c
Now if we run this program and enter a few characters followed by a newline, we discover something is amiss. Note that you will have to press Ctrl-C to stop this program once the infinite loop starts! 
prompt> a.out
bob
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
...
Obviously, we have a problem. Lets load up gdb: 
prompt> gdb a.out
GNU gdb 5.0
Copyright 2000 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...
(gdb) 
To find the problem, we'll set off the infinite loop, and then press Ctrl-C to send the program a SIGINT. Gdb will trap this signal and stop program execution. 
(gdb) run
Starting program: /home/dgawd/cpsc/363/a.out 
moo
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
....
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
Program received signal SIGINT, Interrupt.
0x400d8dc4 in write () from /lib/libc.so.6
(gdb) 

Now the program is stopped and we can see where we are. We will use the backtrace command to examine the stack. The output of this command depends on your C libraries and exactly where the program was when you sent it the SIGINT. Mine looks like this: 

(gdb) backtrace
#0  0x400d8dc4 in write () from /lib/libc.so.6
#1  0x40124bf4 in __check_rhosts_file () from /lib/libc.so.6
#2  0x40086ee8 in _IO_do_write () from /lib/libc.so.6
#3  0x40086e46 in _IO_do_write () from /lib/libc.so.6
#4  0x40087113 in _IO_file_overflow () from /lib/libc.so.6
#5  0x40087de5 in __overflow () from /lib/libc.so.6
#6  0x40069696 in vfprintf () from /lib/libc.so.6
#7  0x40070d76 in printf () from /lib/libc.so.6
#8  0x80484c2 in main (argc=1, argv=0xbffffaf4) at inf.c:12
#9  0x40037f5c in __libc_start_main () from /lib/libc.so.6

From this output, we can see that the program stopped in the write() system call inside the C library. But what we really want to see is where we are in main, so we are going to switch to frame 8:

(gdb) frame 8
#8  0x80484c2 in main (argc=1, argv=0xbffffaf4) at inf.c:12
12                printf("%c", c);

From the output, we know that the value of c is probably 'm', but lets check anyway: 

(gdb) print c
$1 = 109 'm'

Now we have to find the loop. We use several iterations of the 'next' command to watch what is happening. Note that we have to work our way up the call stack back to main(). The next command will exit any functions we can't debug (like C library functions). We could also use the finish command here. 

(gdb) next
Single stepping until exit from function write, 
which has no line number information.
0x40087778 in _IO_file_write () from /lib/libc.so.6
(gdb) next
Single stepping until exit from function _IO_file_write, 
which has no line number information.
0x40086ee8 in _IO_do_write () from /lib/libc.so.6
.....
.....
(gdb) 

Single stepping until exit from function printf, 
which has no line number information.

main (argc=1, argv=0xbffffaf4) at inf.c:15
15        }
Ok, now we are inside main(). We run the next command several more times to watch the program execute. 
(gdb) n
11              if(isalnum(c))
(gdb) 
12                printf("%c", c);
(gdb) 
15        }
(gdb) 
11              if(isalnum(c))
(gdb) 
12                printf("%c", c);
(gdb) n
15        }
(gdb) 
11              if(isalnum(c))
(gdb) 
12                printf("%c", c);

Notice a pattern? The same two lines of code are executing over and over. This means we are looping, inside the while loop on line 9. Now, the value of 'c' is not changed in these lines. Maybe we should look back at our program: 

11:        if(isalnum(c))
12:          printf("%c", c);
13:     else
14:          c = fgetc(stdin);

The lines being executed are 11 and 12. The test is always passing because the character is never changing. So the program is only reading characters until it finds an alphanumeric character, after which it never reaches the fgetc. But we always want to read the next character. Removing the 'else' on line 13 will fix the bug. 

7.2 Example Debugging Session: Segmentation Fault Example 

We are going to use gdb to figure out why the following program causes a segmentation fault. The program is meant to read in a line of text from the user and print it. However, we will see that in it's current state it doesn't work as expected...


1 : #include <stdio.h>
2 : #include <stdlib.h>

3 : int main(int argc, char **argv)
4 : {
5 :   char *buf;
6 :
7 :   buf = malloc(1<<31);
8 :
9 :   fgets(buf, 1024, stdin);
10:   printf("%s\n", buf);
11:
12:   return 1;
13: }




The first step is to compile the program with debugging flags: 

prompt> gcc -g segfault.c
Now we run the program: 
prompt > a.out
Hello World!
Segmentation fault
prompt > 
This is not what we want. Time to fire up gdb: 
prompt > gdb a.out
GNU gdb 5.0
Copyright 2000 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...
(gdb) 
We'll just run it and see what happens: 
(gdb) run
Starting program: /home/dgawd/cpsc/363/a.out 
test string

Program received signal SIGSEGV, Segmentation fault.
0x4007fc13 in _IO_getline_info () from /lib/libc.so.6

So we received the SIGSEGV signal from the operating system. This means that we tried to access an invalid memory address. Let's take a backtrace: 

(gdb) backtrace
#0  0x4007fc13 in _IO_getline_info () from /lib/libc.so.6
#1  0x4007fb6c in _IO_getline () from /lib/libc.so.6
#2  0x4007ef51 in fgets () from /lib/libc.so.6
#3  0x80484b2 in main (argc=1, argv=0xbffffaf4) at segfault.c:10
#4  0x40037f5c in __libc_start_main () from /lib/libc.so.6
We are only interested in our own code here, so we want to switch to stack frame 3 and see where the program crashed: 
(gdb) frame 3
#3  0x80484b2 in main (argc=1, argv=0xbffffaf4) at segfault.c:10
10        fgets(buf, 1024, stdin)

We crashed inside the call to fgets. In general, we can assume that library functions such as fgets work properly (if this isn't the case, we are in a lot of trouble). So the problem must be one of our arguments. You may not know that 'stdin' is a global variable that is created by the stdio libraries. So we can assume this one is ok. That leaves us with 'buf': 

(gdb) print buf
$1 = 0x0

The value of buf is 0x0, which is the NULL pointer. This is not what we want - buf should point to the memory we allocated on line 8. So we're going to have to find out what happened there. First we want to kill the currently-running invocation of our program: 

(gdb) kill
Kill the program being debugged? (y or n) y
Now set a breakpoint on line 8: 
(gdb) break segfault.c:8
Breakpoint 1 at 0x8048486: file segfault.c, line 8.
Now run the program again: 
(gdb) run
Starting program: /home/dgawd/cpsc/363/a.out 

Breakpoint 1, main (argc=1, argv=0xbffffaf4) at segfault.c:8
8         buf = malloc(1<<31);
We're going to check the value of buf before the malloc call. Since buf wasn't initialized, the value should be garbage, and it is: 
(gdb) print buf
$2 = 0xbffffaa8 "Èúÿ¿#\177\003@t`\001@\001"
Now step over the malloc call and examine buf again: 
(gdb) next
10        fgets(buf, 1024, stdin);
(gdb) print buf
$3 = 0x0

After the call to malloc, buf is NULL. If you were to go check the man page for malloc, you would discover that malloc returns NULL when it cannot allocate the amount of memory requested. So our malloc must have failed. Let's go back and look at it again:
 
7 :   buf = malloc(1<<31);

Well, the value of the expression 1 << 31 (the integer 1 right-shifted 31 times) is 429497295, or 4GB (gigabytes). Very few machines have this kind of memory - mine only has 256MB. So of cousre malloc would fail. Furthermore, we are only reading in 1024 bytes in the fgets call. All that extra space would be wasted, even if we could allocate it. Change the 1<<31 to 1024 (or 1<<9), and the program will work as expected: 
prompt >
Hello World!
Hello World!

prompt >
So now you know how to debug segmentation faults with gdb. This is extremely useful (I use it more often then I care to admit). The example also illustrated another very important point: ALWAYS CHECK THE RETURN VALUE OF MALLOC! Have a nice day.


How to Debug Using GDB

We are going to be using two programs to illustrate how GDB can be used to debug code.
Debugging a program with a logical error
The first sample program has some logical errors. The program is supposed to output the summation of (X^0)/0! + (X^1)/1! + (X^2)/2! + (X^3)/3! + (X^4)/4! + ... + (X^n)/n!, given x and n as inputs. However the program outputs a value of infinity, regardless of the inputs. We will take you step by step through the debugging process and trace the errors:
  1. See the sample program broken.cpp
/*
Broken.cpp
*/
2.  #include <iostream>
3.  #include <cmath>
4.   
5.  using namespace std;
6.   
7.  int ComputeFactorial(int number) {
8.    int fact = 0;
9.   
10.  for (int j = 1; j <= number; j++) {
11.    fact = fact * j;
12.  }
13. 
14.  return fact;
15.}
16. 
17.double ComputeSeriesValue(double x, int n) {
18.  double seriesValue = 0.0;
19.  double xpow = 1;
20. 
21.  for (int k = 0; k <= n; k++) {
22.    seriesValue += xpow / ComputeFactorial(k);
23.    xpow = xpow * x;
24.  }
25. 
26.  return seriesValue;
27.}
28. 
29.int main() {
30.  cout << "This program is used to compute the value of the following series : " << endl;
31. 
32.  cout << "(x^0)/0! + (x^1)/1! + (x^2)/2! + (x^3)/3! + (x^4)/4! + ........ + (x^n)/n! " << endl;
33. 
34.  cout << "Please enter the value of x : " ;
35. 
36.  double x;
37.  cin >> x;
38. 
39.  int n;
40.  cout << endl << "Please enter an integer value for n : " ;
41.  cin >> n;
42.  cout << endl;
43. 
44.  double seriesValue = ComputeSeriesValue(x, n);
45.  cout << "The value of the series for the values entered is "
46.  << seriesValue << endl;
47. 
48.  return 0;
49.}


2  2.   Compile the program and execute the program.

% g++ -g broken.cpp -o broken
% ./broken
Whatever the input, the output will be inf. The -g option is important because it enables meaningful GDB debugging.
3.       Start the debugger

% gdb broken
This only starts the debugger; it does not start running the program in the debugger.
4.       Look at the source code and set a breakpoint at line 43

(gdb) b 43
which is
double seriesValue = ComputeSeriesValue(x, n);
5.       Now, we start to run the program in the debugger.

(gdb) run
Note: If you need to supply the command-line arguments for the execution of the program, simply include them after the run command, just as normally done on the command line.
6.       The program starts running and asks us for the input.

Let's enter the values as x=2 and n=3. The expected output value is 5. The following is a snapshot of the program running in the debugger:
This program is used to compute the value of the following series :
(x^0)/0! + (x^1)/1! + (x^2)/2! + (x^3)/3! + (x^4)/4! + ........ + (x^n)/n!
Please enter the value of x : 2

Please enter an integer value for n : 3

Breakpoint 1, main () at broken.cpp:43
43  double seriesValue = ComputeSeriesValue(x, n);
Note that the program execution stopped at our first (and only) breakpoint.
7.       Step into the ComputeSeriesValue() function

To step into a function call, we use the following command:
(gdb) step
ComputeSeriesValue (x=2, n=3) at broken.cpp:17
17  double seriesValue=0.0;
At this point, the program control is at the first statement of the function ComputeSeriesValue (x=2, n=3)
8.       Next let's step through the program until we get into ComputeFactorial.
  1. (gdb) next
  1. 18  double xpow=1;
  1. (gdb) n
  1. 20  for (int k = 0; k <= n; k++) {
  1. (gdb)
  1. 21    seriesValue += xpow / ComputeFactorial(k) ;
  1. (gdb) s
  1. ComputeFactorial (number=0) at broken.cpp:7
  1. 7  int fact=0;

Here we use the next command, which is similar to step except it will step over (instead of into) functions. The distinction doesn't matter here since there are no functions. You may use the shortest, unambigious spelling of a GDB command to save some typing. Here we use n and s instead of next and step, respectively. If the command is simply a repeat of the previous command, you can just hit return, which will execute the last command. Finally, we step (with s) into ComputeFactorial(). (If we'd used next, it would have stepped over ComputeFactorial.)
18.     Where are we?

If you want to know where you are in the program's execution (and how, to some extent, you got there), you can view the contents of the stack using the backtrace command as follows:
(gdb) bt
#0  ComputeFactorial (number=0) at broken.cpp:7
#1  0x08048907 in ComputeSeriesValue (x=3, n=2) at broken.cpp:21
#2  0x08048a31 in main () at broken.cpp:43
19.     Watching changes We can step through the program and examine the values using the print command.
  1. (gdb) n
  1. 9  for (int j = 0; j <= number; j++) {
  1. (gdb) n
  1. 10    fact = fact * j;
  1. (gdb) n
  1. 9  for (int j = 0; j <= number; j++) {
  1. (gdb) print fact
  1. $2 = 0
  1. (gdb) n
  1. 13  return fact;
  1. (gdb) quit

The print command (abbreviated p) reveals that the value of fact never changes. Note that the function is returning a value of 0 for the function call ComputeFactorial(number=0). This is an ERROR!
By taking a closer look at the values printed above, we realize that we are computing fact=fact * j where fact has been initialized to 0; fact should have been initialized to 1. We quit GDB with the quit command. Next we need to change the following line:
int fact = 1;
Recompile the code and run it, you will get the expected output.

Debugging a program that produces a core dump
This program causes a core dump due to a segmentation fault. We will try to trace the reason for this core dump.
Use the following program for this
testit.c :

#include <stdio.h>
 
void main()
{
        char *temp = "Paras";
 
        int i;
        i=0;
 
        temp[3]='F';
 
        for (i =0 ; i < 5 ; i++ )
               printf("%c\n", temp[i]);
 
        
}


     1.  Compile the program using the following command.
            g++ testit.c -g -o testit

     2.  Run it normally, you should get the following result:
                 Segmentation fault (core dumped)

3.      The core dump generates a file called corewhich can be used for debugging. Since, this program is really short, we will not need to set any breakpoints. Use the following command to start running the debugger to debug the core file produced by testit.
     gdb testit core
The output of the above command should look like this:
bash$ gdb testit core
GNU gdb 19991004
Copyright 1998 Free Software
Core was generated by `testit'.
Program terminated with signal 11, Segmentation fault.
Reading symbols from /usr/lib/libstdc++-libc6.1-1.so.2...done.
Reading symbols from /lib/libm.so.6...done.
Reading symbols from /lib/libc.so.6...done.
Reading symbols from /lib/ld-linux.so.2...done.
#0  0x804851a in main () at testit.c:10
10              temp[3]='F';       
4.      As we can see from the output above, the core dump was produced
      as a result of execution of the statement on line 10: temp[3] =F;
    Take a closer look at the declaration of temp on line 5 :
                        Line 5          char *temp = "Paras";
We find that  temp  is a char* which has been assigned a string literal, and so we cannot modify the contents of the literal as on line 10. This is what is causing a core dump