Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W)...

42
Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41

Transcript of Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W)...

Page 1: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Lecture 2

Xiaoguang Wang

STAT 598W

January 16th, 2014

(STAT 598W) Lecture 2 1 / 41

Page 2: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Outline

1 GNU compiler and debugger

2 Pointers and Arrays

3 Structures

4 Compilation Process

5 Exercises

(STAT 598W) Lecture 2 2 / 41

Page 3: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Outline

1 GNU compiler and debugger

2 Pointers and Arrays

3 Structures

4 Compilation Process

5 Exercises

(STAT 598W) Lecture 2 3 / 41

Page 4: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

GNU compiler

GNU Compilers: gcc (for C compiling), g++ (for C++ compiling)

-c : compile the code file into a machine code file

-o : produce the final executable file/output

-g : enable the executable file to be debugged with GDB debugger

-Wall : have the compiler generate many warnings about syntacticallycorrect but questionable looking code

-lm : Compile a C program that uses math functions such as ”sqrt”.

-lefence : Compile a C program with the ”electric fence” library.

For more information, please read the tutorial from the link below:

http://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html

(STAT 598W) Lecture 2 4 / 41

Page 5: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

GDB

GDB: GNU Debugger.

Debugger for many programming languages: C, C++, FreePascal,Fortran, etc.

Like GCC, it is part of the GNU project. (http://www.gnu.org/)

Command line interfacing only. But emacs provides a nice front-end.

It debugs executable files only.

(STAT 598W) Lecture 2 5 / 41

Page 6: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Some GDB commands

gdb name_program: start gdb.

b [file:]line: set breakpoint at line number in file.

run [arglist]: start your program with an arglist.

p expr: display the value of an expression.

c: continue running a program.

n: next line (stepping over function calls)

s: next line (stepping into function calls)

(STAT 598W) Lecture 2 6 / 41

Page 7: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

More information

http://www.gnu.org/software/gdb/documentation/

http://sourceware.org/gdb/current/onlinedocs/gdb/

http://refcards.com/docs/peschr/gdb/gdb-refcard-a4.pdf

(STAT 598W) Lecture 2 7 / 41

Page 8: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Outline

1 GNU compiler and debugger

2 Pointers and Arrays

3 Structures

4 Compilation Process

5 Exercises

(STAT 598W) Lecture 2 8 / 41

Page 9: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Pointers

Pointer: variable that contains the address of a variable.

Pointers and arrays are closely related.

The operator & gives the address of an object. For example:

p = &c

assigns the address of c to the variable p. (p is pointing to c).

The operator * is the “indirection” operator. When applied to apointer it accesses the object the pointer points to.

Pointer declaration:

type *variable_name

(STAT 598W) Lecture 2 9 / 41

Page 10: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Example

int x=1,y=1;

int *ip;

ip=&x;

y=*ip;

*ip=0;

Can you explain what this code is doing?

(STAT 598W) Lecture 2 10 / 41

Page 11: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Pointers and Functions

By default C passes arguments to functions by value.

The only way to change a variable in the calling function is by usingpointers.

What is the problem with the following code?

void swap(int x, int y){

int temp;

temp=x;

x=y;

y=temp;

}

(STAT 598W) Lecture 2 11 / 41

Page 12: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Exercise

Fix the swap function using pointers.

(STAT 598W) Lecture 2 12 / 41

Page 13: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Arrays

Syntax: type name[size]. Example:

int a[10];

This notation defines an array with elements a[0],a[1],...,a[9].

Suppose that pa is a pointer to a[0]. Then we have to state:

int *pa;

pa= &a[0];

Clearly, if i=1,...9, then:

pa+i is the address of the component a[i] and therefore pa+i=&a[i].a[i] has the same value than *(pa+i).

Then there is a 1-1 relationship between pointers and arrays.

(STAT 598W) Lecture 2 13 / 41

Page 14: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Arrays

Then the relationship pa=&a[0] can be written as pa=a.

Then when array is passed to a function, what is passed is thelocation of the first element.

Example: (include this function in your current directory)

int strlen(char *s){

int n;

for(n=0; *s != ’\0’; s++)

n++;

return n;

}

(STAT 598W) Lecture 2 14 / 41

Page 15: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Pointer operations

As long as two pointers point to the same array:

When you substract them the result is the number of elementsbetween those pointers.

One pointer is greater than the other if it points beyond where theother one points.

You can compare them by equality and inequality.

Example:

int array1[10], array2[10];

int *ip1, *ip2 = &array2[0];

int *ep = &array1[10];

for(ip1 = &array1[0]; ip1 < ep; ip1++)

*ip2++ = *ip1;

(STAT 598W) Lecture 2 15 / 41

Page 16: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Increment and Decrement Operators

The ”for” loop of the code from last page works as follows:

*ip2 = *ip1;

ip2 = ip2 +1;

Note that even though the increment operator ”++” has the highestprecedence, there is still a difference between ”prefix” and ”postfix”. Theprefix usage ”++i” first increases i by 1 and then the expression takes asits value the new stored value of i. However, the postfix usage ”i++”causes the value of i incremented by 1 while the value of the expression isstill the value of i before its increase.

(STAT 598W) Lecture 2 16 / 41

Page 17: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Memory Allocation

It is problematic and inefficient to work with fixed-size arrays.

malloc: returns a pointer to n bytes of memory. Example:

#include <stdlib.h>

char *line;

int linelen = 100;

line = malloc(linelen);

Note that 1 char uses 1 byte of memory.

If we need to allocate space for a different class of variables, usesizeof() operator:

int *ip = malloc(100 * sizeof(int));

(STAT 598W) Lecture 2 17 / 41

Page 18: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Memory allocation

If malloc fails in the memory allocation, it returns a NULL pointer.Then it is important to include this warning in your code:

int *ip = malloc(100 * sizeof(int));

if(ip == NULL)

{

printf("out of memory\n");

return 0;

}

After the memory is used, we can deallocate it by using:

free(ip);

Obviously, it is not mandatory if you use your memory only onceduring execution.

(STAT 598W) Lecture 2 18 / 41

Page 19: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Memory allocation

Sometimes you need to re-define your allocated memory. Use:

ip = realloc(ip, 200 * sizeof(int));

(STAT 598W) Lecture 2 19 / 41

Page 20: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Always Allocate memory for pointers

Once you define a pointer, always ask yourself what the pointer points to,i.e., the location the pointer pointing to. What’s the problem of the codebelow?

int *fun()

{

int *point;

*point=12;

return point;

}

(STAT 598W) Lecture 2 20 / 41

Page 21: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Always Allocate memory for pointers

Don’t forget to allocate memory for your pointer!Correction:

int *fun()

{

int *point = malloc(sizeof *point); /* Mandatory. */

*point=12;

return point;

}

(STAT 598W) Lecture 2 21 / 41

Page 22: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Pointers to functions

Syntax:

type (*name)(arguments)

Examples:

double (*func)(double x);

double (*myFunc)(double x);

double (*AnyFuncName)(double x);

(STAT 598W) Lecture 2 22 / 41

Page 23: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Pointers to functions

Recall:

Any pointer to function has the syntax:

type (*name)(arguments);

You can simplify the pointer definitions by using typedef, forexample:

typedef int (*cmpFcn)(const string &, const string &);

and afterwards, you can initialize a function pointer:cmpFcn pf2=lengthcompare;

A pointer to function argument can be declared as:

int (*)(const string &, const string &);

in your header files.

(STAT 598W) Lecture 2 23 / 41

Page 24: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Pointers to functions

If you use a pointer to function inside any function, you don’t need touse the * operator to call the pointed function, just use the pointer’sname instead.

(STAT 598W) Lecture 2 24 / 41

Page 25: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Pointers to functions: Example

How to interprete the following lines?

int (*pfi)();

int *pfi();

typedef int (*funcptr)();

funcptr pfi;

\\\\\\\\\\\

extern int f1();

pfi = &f1;

pfi = f1;

\\\\\\\\\\\

int (*pfr)(arg1, arg2);

int *pfr(arg1, arg2);

(*pfr)(arg1, arg2);

(STAT 598W) Lecture 2 25 / 41

Page 26: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Outline

1 GNU compiler and debugger

2 Pointers and Arrays

3 Structures

4 Compilation Process

5 Exercises

(STAT 598W) Lecture 2 26 / 41

Page 27: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Basics

Collection of one or more variables. They are grouped under a singlename.

Example:

struct point {

int x;

int y;

}

You can declare your structure as usual:

struct point pt={2,1};

And you can call its members:

printf("%d,%d", pt.x, pt.y);

(STAT 598W) Lecture 2 27 / 41

Page 28: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Operations on structures

We can do several operations on structures:

Copy.

Assign.

Take its address.

Access its members.

(STAT 598W) Lecture 2 28 / 41

Page 29: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Pointers to structures

We can declare pointers to structures, in the usual way:

struct point *pp;

Note that *pp is the structure and (*pp).x, (*pp).y are themembers.

Pointers to structures admit the following notation:

(*pp).x = pp->x ;

(*pp).y = pp->y ;

Using structures we can introduce nice data schemes, like trees andlinked lists. If you are interested there are a lot of web resources, forexample:http://publications.gbdirect.co.uk/c_book/chapter6/structures.html

(STAT 598W) Lecture 2 29 / 41

Page 30: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Important commands

enum: we can construct a list of named constants which areassociated with integer values. Example:

enum Colours {Red, Green, Blue, Indigo, Violet};

Colours myCol;

Colours myCol2 = Green;

typedef: add a new name for an existing type of variable. Example:

typedef int Int32;

Int32 myVar = 8;

(STAT 598W) Lecture 2 30 / 41

Page 31: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Outline

1 GNU compiler and debugger

2 Pointers and Arrays

3 Structures

4 Compilation Process

5 Exercises

(STAT 598W) Lecture 2 31 / 41

Page 32: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Compilation Process

(STAT 598W) Lecture 2 32 / 41

Page 33: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

C Preprocessor

It is a macro processor (cpp) that is used by the C/C++ compiler inorder to transform your program before compilation.

The most usual macros are:

#include: File inclusion.#define: macro substitution.

(STAT 598W) Lecture 2 33 / 41

Page 34: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

File Inclusion

The macro #include<filename> or #include"filename" takessource from “filename” to be used by the compiler.

If the filename is quoted, the searching process starts where thesource program is found.

If the filename is enclosed by < >, searching begins at /usr/includefolder.

(STAT 598W) Lecture 2 34 / 41

Page 35: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Macro substitution

A definition has the form:

#define name replacement_text

Then subsequent ocurrences of the token “name” will be replaced by“replacement text”.

Example:

#define MAXIMUM 300

#define forever for(;;)

#define printmax if(a<b) printf("%i",b);

#define sqr(x) ((x)*(x));

(STAT 598W) Lecture 2 35 / 41

Page 36: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Example

Suppose we have the following C code:

#include<stdio.h>

#define MAXI 20

int main(){

int i=4;

printf("%i\n", i+MAXI);

return 0;

}

If you use gcc compilex.c -o compilex you can do the entirecompiling process.

(STAT 598W) Lecture 2 36 / 41

Page 37: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Assembling

Once the C preprocessor has stripped the source code of thecomments and expanded the preprocessor commands, the compilertranslates the C code to assembly language (machine level code).

Use the command gcc -S compilex.c to create compilex.s(assembly file).

This code contains instructions that manipulate memory andprocessor directly.

(STAT 598W) Lecture 2 37 / 41

Page 38: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Compiling

The compiler takes the assembly code and converts the machine-levelinstructions into binary code.

The binary file will be called compilex.o. You can see it with thecommand: gcc -c compilex.c

(STAT 598W) Lecture 2 38 / 41

Page 39: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Linking

In order to create the executable file we need to use the linker toprocess the main function, its arguments and link it with other binaryfiles that our program needs.

The linker will call the printf binary file on the standard library. (if wehave header files or libraries called with #include, the process is thesame).

The linking process can be done withgcc compilex.o -o compilex. The final output is the executablefile.

(STAT 598W) Lecture 2 39 / 41

Page 40: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Outline

1 GNU compiler and debugger

2 Pointers and Arrays

3 Structures

4 Compilation Process

5 Exercises

(STAT 598W) Lecture 2 40 / 41

Page 41: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Midpoint Rule

Recall that the definite integral of a real-valued function f (x) can beapproximated by: ∫ b

af (x)dx ≈

n∑i=1

f (xi )∆xi

where ∆xi = b−an and xi = xi−1 + ∆xi , x1 = a + ∆xi

2 .

Implement the Midpoint rule using as inputs: pointer to a functionf (x), a, b and number of intervals n.

(STAT 598W) Lecture 2 41 / 41

Page 42: Lecture 2 - stat.purdue.edu · Lecture 2 Xiaoguang Wang STAT 598W January 16th, 2014 (STAT 598W) Lecture 2 1 / 41. Outline 1 GNU compiler and debugger 2 Pointers and Arrays 3 Structures

Midpoint Rule

Test your program with the integrals (Duffy, 2004):∫ 1

0x3dx

∫ 1

0

log(x)

1 − x2dx

(STAT 598W) Lecture 2 42 / 41