L2: Program Development Flashcards

1
Q

(CRE)2 Criteria

(Program Fitness Checklist)

A

A Good Program should be:

  • Correct
    • Does what specification said
    • What client wants
    • What user needs
  • Clear
    • Understandable Implementation
  • Robust
    • Resilient wrt the real world, human abilities, future requirements
  • Ready
    • Delivered within schedule, budget constraints
  • Efficient
    • Within limits of time, space, cost
  • Ethical
    • Meets standards set by law, professional ethics, and safety needs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Convergent Programming Steps

A
  1. Understand the Problem
    • State the Problem
    • Specify the Interface
    • List representative and stressful cases
  2. Code into C
    • Diagram your approach
    • Decide on variables and invariants
    • Outline overall logic
    • Convert logic into code
  3. Verify the Solution
    • “Desk Check” -mental test cases
    • Perform test cases on computer
    • Use Fitness Test (CRE)2
    • Peer Review
  4. Return to earlier steps as needed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Convergent Programming:

Basic Idea

A
  • Clearly define the logic:
    • in english,
    • mathematical notation
    • diagrams
  • THEN, express that logic in C

Code INTO C, not IN C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Convergent Programming Steps:

Step 1) Understand the Problem

-Substeps

A
  • State the problem
  • Specify the Interface
  • List Representative Cases
  • List Stressful Cases
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Convergent Programming Steps:

Step 2) Code Into C:

-Substeps

A

Code into C

  • Diagram your approach
  • Decide on variables and invariants
  • Outline overall logic
  • Convert logic into code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Convergent Programming Steps:

Step 3) Verify the Solution

-Substeps

A

Verify the Solution

  • “Desk Check” -mental test cases
  • Computer Test: Perform test cases on computer
  • Use Fitness Test (CRE)2
  • Peer Review
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

2 “Families” of Common Errors

A
  • Exogenous (Outward error)
    • Hardware Issues
      • Computer Crash/error
    • Software Issues
    • Data Issues:
      • incorrect data
      • too much data
      • too fast/slow
  • Endogenous (Problems with development process/team)
    • Analysis
      • Wrong Problem
      • Wrong Goals
      • Wrong User
    • Specification
      • incorrect
      • incomplete
      • inconsistent
    • Logic
      • forgetfulness
      • stupidity
      • laziness
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

make:

-n switch

A

%make -n

Do not actually perform the commands in the recipes,

just echo them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Programming as a Craft:

Maintain Respect for ________

A
  • The User
    • Will they feel good about using your product?
  • The Tools
    • Recognize their limits, and your own
  • Yourself
    • Use best known practices
    • Take pride in your work
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Make Files:

  • file name
  • format
A
  • Always have the name “makefile”
  • Format:

target: prerequisites

recipe

dependencies

Example:

foobar: main.o util.o

cc -c main.c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Key Files

during

Compiling and Linking

A
  • Source Files
    • c language
    • text format
    • end in .c and .h
  • Object Files
    • machine instructions, binary format
    • external names
    • data initialization
    • end in .o
  • Libraries
    • Collections of object files
    • binary format
    • end in .a
  • Executable Files
    • instructions
    • data
    • entry point (“main”)
    • other details
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Steps of Creating an Executable Program

A
  1. Edit
    • Create source files(.h, .c) with editor
  2. Compile
    • Translate each source into an object file
  3. Link
    • Combine the object files into an executable file
  4. Execute
    • Use the new program
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Compiler:

Useful switches for the

cc program

A

* -c

* compile into an object(.o) file * -D
* Define a variable, equivalent to #define
* example: cc -DFOO=this * -E
* Send preprocessor output to stdout and stop * -S
* Save .s files instead of deleting them

More advanced switches:

  • -g
    • create expanded symbol table for debugging
  • -p
    • link with the profiling facility “prof”
  • -O
    • optimize the code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Compiler Steps

A
  • Preprocessing
    • Replace #define, #include, etc
  • Compilation
    • Produce assembly language from c text
    • generates .s files
  • Assembly
    • Produce object files from assembly text
    • generates .o files
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Using External Variables

A
  • Use the “extern” keyword to declare an external variable
  • Allows use of a variable from another object that is linked
  • Example:
    • extern int flag
  • Ensure exactly one definition for each external name
  • Always initialize external variables
  • Ensure definition & import agree on type(the linker doesn’t check this)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What happens during Linking?

(Overview)

A
  • A program is usually specified in pieces, each in separate source files
  • Also, system routines may be referenced, which are in the System Library files (“.a” extensions)
  • The linker combines your objects and those needed system routines
  • Creates an executable program, no extensions, executable attribute
17
Q

What happens during Compiling?

(overview)

A
  • The compiler(cc) translates the source files(.c and .h) into a form suitable for efficient machine interpretation
  • The files created are Object Files( “.o” extensions)
  • Substeps:
    • Preprocessing
    • Assembly
18
Q

Using Malloc to Allocate storage

(Overview)

A
  • “malloc” routine gets storage from the heap
  • must include stdlib
  • the heap is a storage pool that you manage
  • Anything allocated persists until explicitly freed using free()
  • Initial bits in allocated storage are unpredictable(when using malloc())
19
Q

Allocating a Structure Instance

with malloc

A
  • Use sizeof() to determine the number of bits to be allocated
  • Malloc returns a block of memory
  • Cast the memory as the required type

name = (myType*) malloc(sizeof(myType))

20
Q

Declaring a Structure

A

typedef struct _myStruct{

char *myName;

int myNum;

struct _myStruct *other;

} MyStruct;

  • typedef : creates a new type name
  • struct : lays out a structure
  • fields of the struct are defined
  • within the definition, can reference the struct with _myStruct
21
Q

Pointer/Structures vs Arrays

A
  • Arrays are great when
    • Data items can be conveniently numbered without many gaps
    • Max number is known
    • Reorganizations will not be frequent
  • Pointers/Structures
    • Needed when things are more complicated
    • No obvious # of elements
    • Insertions are common
22
Q

Fundamental Principle of Pointer Programming

A

Diagram your f******

Pointer Logic

23
Q

Pointer Programming

Steps

A
  1. Think
    • of a specific example of the situation and the action at hand
  2. Diagram
    • how things are organized before the action(“before”)
  3. Modify
    • to show effects of the action (“after”)
  4. Generalize
    • to capture the general case
  5. Code
    • the general case into c
24
Q

make operations:

Recipes

A
  • Can have any number of commands
  • Make process stops when a command fails
  • Always precede the commands with a tab
25
Q

make operations:

targets

A
  • Can be specified on make command line
    • %mk foo.o
  • A rule may have multiple targets
  • A target with no prerequisites is useful to automate a task sometimes
26
Q

Benefits of make and makefiles

A
  • Records dependence between executables, objects and sources
  • Check last modified times on all files
  • Performs compiles and links ONLY when changes occur
27
Q

Common Make Errors

A
  • Circular Dependency
    • making a file dependent on itself
  • Insufficient Prerequisites
    • Ensure you have the .h files IN the prerequisites
  • Missing Recipe
    • Triggers a built-in recipe for .c -> .o generation, which is probably not what you want
  • No tab before recipe
    • Make will think the command is a target
  • $ -substitutions
    • make and shell each have $var variables for substitution
28
Q

Program Memory:

Two Methods to get an area from the Heap

A
  • malloc(size)
    • memory allocation
    • allocates a single area of the heap of the specified size, then is casted to a pointer
  • calloc(n, size)
    • allocate multiple n chunks of memory, for use by arrays
29
Q

Memory Layout for a program

A
  • Memory is a linear array of bytes
  • Stack changes with call and return
  • Heap changes with malloc/calloc and free

Sections of Memory, in order:

  • Code
  • Data
  • Heap
  • Available(used for temporary storage)
  • Stack
  • Shell Data
30
Q

When to use the Heap

A
  • When data should persist after a function returns
  • Size of data is not known until execution time
31
Q

Program Memory:

Variables used to track Program State

A
  • Key Addresses
    • Current Instruction Counter (IC)
    • Stack Pointer, SP
    • Frame Pointer, FP
    • Heap Bottom, Stack Bottom
  • Register Values
  • Scheduling Priority
  • Current Working Directory
  • Process id’s :
    • This process, pid
    • Parent Process, ppid
  • User and Group IDs
32
Q

3 Common Pointer Errors

A
  • Pointer is null
  • Unitialized fields
    • pointer is not null, but points to wrong thing or garbage data
  • Forgetting the asterisk:
    • person p - wrong
    • person* p - correct
33
Q
A