COP 3530 Spring2012 Data Structures & Algorithms - PowerPoint PPT Presentation

Cop 3530 spring2012 data structures algorithms
1 / 18

  • Uploaded on
  • Presentation posted in: General

COP 3530 Spring2012 Data Structures & Algorithms. Discussion Session Week 2. Outline. TA contact compilation makefile debug. About me. TA contact. Tao Li PhD student at CISE Office Hour This Week: Thursday 9 th period at E309.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Presentation

COP 3530 Spring2012 Data Structures & Algorithms

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Cop 3530 spring2012 data structures algorithms

COP 3530 Spring2012Data Structures & Algorithms

Discussion Session Week 2



  • TA contact

  • compilation

  • makefile

  • debug

About me

About me

Ta contact

TA contact

Tao Li

PhD student at CISE

Office Hour This Week:

Thursday 9th period at E309

Separate code

Separate code

header file

#ifndef _my_stack#define _my_stackint add(int x, int y); // function prototype for add.h


.cpp file

int add(int x, int y){ return x + y;}

Header guards

Because header files can include other header files, it is possible to end up in the situation where a header file gets included multiple times.

Compilation g

Compilation: g++

  • Compiling, in which C++ code is translated into assembly;

  • Assembling, in which assembly is translated into machine language; and

  • Linking, in which calls to functions outside the main file are linked to their definitions.


    g++ -c MyStack.cpp

    g++  -c main.cpp

    g++  -o stack main.oMyStack.o



    g++  -o stack main.cpp MyStack.cpp

    -o program_name         // compiling and linking to generate program_name, default "a.out"

    -c                                // compiling but no linking-g                                // for debugging, but runs slow

Make and makefile

make and makefile

make is a system designed to create programs from large source code trees and to maximize the efficiency of doing so. To that effect, make uses a file in each directory called a Makefile. This file contains instructions for make on how to build your program and when.

target: dependencies<tab>instructions<enter>


Note: Build several independent targets in order, below is 

sample makefile


all: target1 target2 target3

target1: dependencies


target2: ...



 A stack is a last in, first out (LIFO)  data structure

Main cpp input file

Main.cpp & Input file

While(fscanf(fp1, “ %d, &x”) > 0)

if(x == 1) {

fscanf(fp1, “ %d”, &y);


} else {


printf(“%d\n”, y);




1 1

1 2

1 3

1 4

1 5





Cop 3530 spring2012 data structures algorithms



For example: 


Gnu debugger gdb

GNU debugger -- gdb

A symbolic debugger is a program that aids programmers in finding logical errors, by allowing them to execute their program in a controlled manner.

  • Enable symbol table   

  • Debug the program

    g++ -g -o stack stack.cpp

    gdb stack

Use gdb

Use gdb

Starting Execution of Program

(gdb) run (or r)

Quitting gdb

(gdb) quit (or q or Ctrl-D)

Resuming Execution at a BreakpointOnce you have suspended execution at a particular statement, you can resume execution in several ways:

continue (or c)  Resumes execution and continues until the next breakpoint or until execution is completed.

next (or n)  next will execute a function in the current statement in its entirety.

Setting breakpoints print

Setting Breakpoints & Print

Setting a breakpoint permits you to mark a particular line in your program (called a breakpoint) so that when execution reaches that line, program execution will be suspended, allowing you to enter a gdb command.

break function: Set a breakpoint at entry to function function.

break filename:linenum :Set a breakpoint at line linenum in source file filename.

print expression (or p expression)Displays the value of the expression (usually a variable) once → at the current point of execution. 

Example tst cpp

Example: tst.cpp

1. #include “stdio.h”

2. int summation(int n) {

3. int sum = 0, i;

4. for(i = 1; i<n; i++) {

  • sum += i;

  • }

    7. return sum;

    8. }

    9. int main() {

    10. printf(“Summation is %d\n”, summation(100));

    11. return 0;

    12. }



g++ tst.cpp -o tst



Summation is 4950


1+2+…+100 = (1+100) * 100 / 2 = 5050

Where is the bug?

Tip reduce the input size

Tip: Reduce the input size


printf(“Summation is %d\n”, summation(100));


printf(“Summation is %d\n”, summation(5));

Expected result: 1+2+3+4+5 = 15

g++ tst.cpp -o tst



Summation is 10

Cop 3530 spring2012 data structures algorithms


Compile: g++ -g tst.cpp -o tst

Run gdb: gdbtst

List code: l

Breakpoint: break 5

Run to bkpnt: r

Next step: n

Print value: p (variable)

Finish: finish

Quit: q

The power of printf

The power of PRINTF


printf(“i=%d, sum=%d\n”, i, sum);


i = 1, sum = 1

i = 2, sum = 3

i = 3, sum = 6

i = 4, sum = 10

Summation is 10

  • Login