Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Similar documents
The Compilation Process

C and Programming Basics

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

CSE 303 Lecture 8. Intro to C programming

CS356: Discussion #7 Buffer Overflows. Marco Paolieri

C++ Tutorial AM 225. Dan Fortunato

EC 413 Computer Organization

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Lecture 3: C Programm

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

Topic 6: A Quick Intro To C

Lectures 5-6: Introduction to C

Introduction to C An overview of the programming language C, syntax, data types and input/output

Chapter 11 Introduction to Programming in C

EL2310 Scientific Programming

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Lectures 5-6: Introduction to C

Chapter 11 Introduction to Programming in C

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Chapter 11 Introduction to Programming in C

The C language. Introductory course #1

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

printf( Please enter another number: ); scanf( %d, &num2);

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

CSCI 2132 Software Development. Lecture 8: Introduction to C

Chapter 11 Introduction to Programming in C

C++ for Python Programmers

Motivation was to facilitate development of systems software, especially OS development.

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

QUIZ on Ch.8. What is kit?

Key Differences Between Python and Java

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Introduction Presentation A

COSC121: Computer Systems: Runtime Stack

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

University of Washington

PROGRAMMAZIONE I A.A. 2017/2018

Lecture 2: C Programm

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Algorithms and Programming I. Lecture#12 Spring 2015

PRINCIPLES OF OPERATING SYSTEMS

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

CS356: Discussion #8 Buffer-Overflow Attacks. Marco Paolieri

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

EL2310 Scientific Programming

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

CS429: Computer Organization and Architecture

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Programming Studio #9 ECE 190

A brief intro to pointers for the purposes of passing reference parameters

[0569] p 0318 garbage

2. Numbers In, Numbers Out

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Compiling and Running a C Program in Unix

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Motivation was to facilitate development of systems software, especially OS development.

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

EL2310 Scientific Programming

Important From Last Time

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

ENGR101: Lecture 7. C language tools and intro to C programming. March ENGR101: Lecture 7 March / 20

Lecture 3. More About C

Machine-Level Programming (2)

Important From Last Time

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-64 assembly code is produced:

Memory Allocation in C

Concurrency. Johan Montelius KTH

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Language comparison. C has pointers. Java has references. C++ has pointers and references

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Computer Systems Lecture 9

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Brief Assembly Refresher

Programming refresher and intro to C programming

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

x86 assembly CS449 Spring 2016

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

Chapter 11 Introduction to Programming in C

C: How to Program. Week /Mar/05

Machine-level Programs Procedure

2. Numbers In, Numbers Out

Problem Set 1: Unix Commands 1

Areas for growth: I love feedback

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Programming. Data Structure

Transcription:

CMPT 125

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance John Edgar 2

Edit or write your program Using a text editor like gedit Save program with a.c extension Compile your program Using gcc This generates a.out Run your program By typing./a.out John Edgar 4

Write your C program using a simple editor Like Notepad for Windows, or TextEdit for the Mac, or gedit for Linux gedit, and other editors highlight text for C syntax Save your program with a.c extension Programs, like variables and functions, should be given sensible names John Edgar 5

#include <stdio.h> int main(){ } #include is like import in Python When your program is run it calls the main function {}s denote a block of code, like indentation in Python John Edgar 6

#include <stdio.h> int main(){ printf("hello World!\n"); } printf is the standard output function and is in stdio.h All statements end with a ; \n is the newline character, this statement prints a new line after the message John Edgar 7

Save your program as a.c file Let's say we've called it hello.c Compile your program using gcc gcc used to stand for Gnu C Compiler Now stands for Gnu Compiler Collection Open a console window and run gcc at the prompt >$ gcc hello.c If the command is successful it creates an executable program called a.out John Edgar 8

Run your program at the command prompt By entering./a.out >$ gcc hello.c >$./a.out Hello World! >$ When compiling your programs it is useful to name the output program something sensible By using the o flag >$ gcc o hello hello.c Now the program is called hello instead of a.out John Edgar 9

In Linux using GCC

correct errors Source File (.c) write in a text editor Debugged Executable compile preprocessor handles directives Object File (binary) test Other Object Files linker links files Executable File

The Python IDLE editor can be used as an interpreter That processes one instruction at a time C programs have to be compiled A compiler translates the entire program Into machine language A machine language program can be directly processed by a computer Each instruction is represented in binary Machine languages are very hard for humans to read and write John Edgar 12

Assembly languages are higher level than machine languages But lower level than C Operation codes are used to identify instructions Memory addresses are given labels Like very basic variable names An assembler translates an assembly language to machine language Where operation codes and memory addresses are binary.section TEXT, text,regular,pure_instructions.globl _main.align 4, 0x90 _main: ## @main.cfi_startproc ## BB#0: pushq %rbp Ltmp2:.cfi_def_cfa_offset 16 Ltmp3:.cfi_offset %rbp, -16 movq %rsp, %rbp Ltmp4:.cfi_def_cfa_register %rbp subq $16, %rsp leaq L_.str(%rip), %rdi movl $0, -4(%rbp) movb $0, %al callq _printf movl $0, %ecx movl %eax, -8(%rbp) ## 4-byte Spill movl %ecx, %eax addq $16, %rsp popq %rbp ret.cfi_endproc.section als L_.str:.asciz TEXT, cstring,cstring_liter ## @.str "Hello World!\n".subsections_via_symbols John Edgar 13

C is a high level programming language It can be compiled into machine code And executed on a computer Programming languages are formal and lack the richness of human languages If a program is nearly, but not quite syntactically correct then it will not compile The compiler will not figure it out

Python print arg1,... arg1 = raw_input() int, float, str, bool,... variables declared during execution and, or, not if-elif-else for i in range(n) indented blocks lists may grow/shrink C printf(format, arg1,...) scanf(format, &arg1,...) int, float, char,... variables declared at compile time &&,,! if { } else if { } else { } for (i = 0; i < n; i++) { } { blocks in curly braces } arrays are fixed size John Edgar 16

Variables must be declared before being used int main(){ int a = 5; int b = 17; printf("sum of %d + %d is %d", a, b, a+b); } int a = 5; declares an integer variable named a and gives it an initial value of 5 Declaration does not have to include initialization int a; //declares an integer called a Un-initialized variables may have garbage values John Edgar 18

The type of a variable in C cannot be changed Once a variable is declared as an int it stays an int Or a char, float, double, etc. It is possible to change the type of a variable in Python When the program is run space is reserved for variables in main memory Usually 4 bytes for an int or a float Usually 8 bytes for a long long or a double Usually 1 byte for a char

Variables are stored in unique locations in memory This location is referred to as its address Which is represented by an integer A variable can therefore be described in three ways Its type (e.g int) Its value (e.g. 42) Its address (its main memory location) Sometimes a program need to explicitly use the address of a variable

When a program runs it requires main memory (RAM) space for Program instructions (the program) Data required for the program There needs to be a system for efficiently allocating memory We will only consider how memory is allocated to program data (variables) code storage data storage

RAM can be considered as a long sequence of bytes Starting with 0 Ending with the amount of main memory (-1) RAM is addressable and supports random access That is, we can go to byte 2,335,712 without having to visit all the preceding bytes

Consider a computer with 1GB * of RAM *1 GB = 1,073,741,824 bytes RAM can be considered as a sequence of bytes, addressed by their position 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1073741816 1073741817 1073741818 1073741819 1073741820 1073741821 1073741822 1073741823 This is a simplified and abstract illustration

Declaring a variable reserves space for the variable in main memory The amount of space is determined by the type The name and location of variables are recorded in the symbol table The symbol table is also stored in RAM The symbol table allows the program to find the address of variables We will pretty much ignore it from now on!

For simplicity's sake assume that each address is in bytes and that memory allocated to the program starts at byte 2048 int x, y; x = 223; x = 17; y = 3299; Creates entries in the symbol table for x and y variable address x 2048 y 2052 These lines change the values stored in x and y, but do not change the location or amount of main memory that has been allocated data 223 17 3299 address 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062...

Variables are stored in main memory We can find out the value of a variable And its address To retrieve the address write the variable name preceded by an ampersand (&) The value of a variable can be changed by assignment But its storage location and the amount of memory allocated to a variable cannot change

We can use the printf function to print the value of a variable, or its address int x = 12; printf("x = %d, ", x); printf("the address of x is %d", &x); Here is another example I wouldn't usually use x or y as the name of a variable since it doesn't convey any meaning, but in this example they are just arbitrary values float y = 2.13; printf( y = %f, ", y); Note the use of %f to print a floating point value, and also note that the address of y is still an integer so its format specification is still %d printf("the address of y is %d", &y);

A variable can be declared that stores the address of another variable Such variables are referred to as pointers Pointers are declared with the type of the variable they point to followed by an * Pointers are used for a number of reasons including Passing addresses to functions The first example of this is the input function, scanf Declaring pointers to arrays in dynamic memory

The scanf function requires the address of the variable that input is to be stored in int main(){ } int a = 0, b = 0; printf("enter an integer: "); scanf("%d", &a); printf("enter another integer: "); scanf("%d", &b); printf("sum of %d + %d is %d", a, b, a+b);

Functions must be defined outside main Note that main is itself a function Function anatomy return type parameter list int gcd(int a, int b){ while (b!= 0){ function name int temp = b; b = a % b; // remainder of a divided by b a = temp; } return a; return statement } John Edgar 32

All C functions are pass by value Data in the argument is copied to the parameter The scope of the parameter is the scope of its function This prevents side-effects, where the function can unexpectedly modify data passed to it Functions in Python are pass by reference Which can result in side-effects but only when the data is mutable Java is a mix of pass by value and pass by reference John Edgar 33

Let's say we want to write a function to swap the values in two variables Here is a first attempt void swap(int x, int y) { int temp = x; x = y; y = temp; } Does this work?

// Calling code (in main) int a = 23; int b = 37; swap(a, b); void swap(int x, int y) { int temp = x; x = y; y = temp; } 23 37 23 37 a b x y x and y are parameters of the swap function so have their own space in main memory

// Calling code (in main) int a = 23; int b = 37; swap(a, b); void swap(int x, int y) { int temp = x; x = y; y = temp; } 23 37 23 37 23 37 23 a b x y temp

// Calling code (in main) int a = 23; int b = 37; swap(a, b); void swap(int x, int y) { int temp = x; x = y; y = temp; } 23 note that neither a nor b's values have changed, so the swap function achieved nothing! a 37 b 37 x 23 y 23 temp once swap has executed its memory is released

Remember that scanf accepts the address of a variable as an argument And that the value of the variable is changed once scanf has finished its execution We can specify an address using & A function definition also needs to specify that it expects an address Note that the address of a float is not the same as a float

// Calling code int a = 23; int b = 37; swap(&a, &b); void swap(int* x, int* y) { int temp = *x; *x = *y; *y = temp; } 23 37 a b x y x and y contain the addresses of a and b, not their values

// Calling code int a = 23; int b = 37; swap(&a, &b); void swap(int* x, int* y) { int temp = *x; *x = *y; *y = temp; } 37 23 23 37 23 a b x y temp

// Calling code int a = 23; int b = 37; swap(&a, &b); void swap(int* x, int* y) { int temp = *x; *x = *y; *y = temp; } 37 23 23 a b x y temp Because swap's parameters are passed addresses a and b have changed once swap has executed its memory is released

The * is used to mean a number of different things, dependent on the context Multiplication Declaration of a pointer variable, when used after a type name int * p; declares a pointer called p that will store the address of an int Dereferencing of a pointer to access the variable that it points to *p = 7; assigns 7 to the int that p points to

Pointers store addresses Addresses are always the same size on the same system So why do we have to say what type of data is going to be pointed to? To reserve enough space for the data and To ensure that the appropriate operations are used with the data

Pointer variable are identified by an * that follows the type in the declaration int * p; This declares a variable called p that will point to (or refer to) an integer Note that the type of a pointer is not the same as the type it points to p is a pointer to an int, and not an int

The operation shown below is unsafe int x = 12; This is not a good thing to do and will result in a compiler warning or error int *p = x; Remember that the type of p is an address (to an int), and not an int Addresses are actually whole numbers but assigning arbitrary numbers to them is a bad idea Since a programmer is unlikely to know what is stored at a particular memory address

Pointers can be assigned the address of an existing variable Using the address operator, & In this way it is possible to make a pointer refer to a variable

Pointers can be used to access variables But only after they have been assigned the address of a variable To change the value of a variable a pointer points to the pointer has to be dereferenced Using the * operator which can be thought of meaning the variable pointed to

int x = 5; int *p = &x; //assign p the address of x // Use p to assign 23 to x *p = 9; //dereferences p printf("value in x = %d\n", x); printf("address of x = %x\n", &x); printf("value in p = %x\n", p); printf("address of p = %x\n", &p); printf("value in *p = %d\n\n", *p);

int x = 12; int y = 77; int *p1 = &x; //assign p1 the address of x int *p2 = &y; //assign p2 the address of y p1 p2 12 77 x y

int x = 12; int y = 77; int *p1 = &x; //assign p1 the address of x int *p2 = &y; //assign p2 the address of y p1 = p2; //assigns the address in p2 to p1 p1 p2 12 77 x y

int x = 12; int y = 77; int *p1 = &x; //assign p1 the address of x int *p2 = &y; //assign p2 the address of y *p1 = *p2; p1 12 77 x p2 77 y

In practice we don't often use pointers like the preceding examples Pointers can be used to allow functions to change the value of their arguments They are also key to managing memory for objects that change size during run-time Such objects are allocated space in another area of main memory in dynamic memory

There are several measures Is it: Correct (no bugs)? Reliable? Efficient? Affordable? Maintainable? Easy to use? John Edgar 54

When we assess the performance of an algorithm we focus on its efficiency There are two main measures of efficiency Time Space (in main memory) Recently, time is considered to be the more important of these two Memory is fairly cheap Memory space is not usually a constraint There are exceptions to this John Edgar 55

There are two main ways to measure performance Time the code on a variety of inputs We can plot graphs and predict behavior This measure is hardware dependent Count the number of operations performed by the algorithm We can plot graphs or derive functions or Use the big-o estimate This measure is hardware independent John Edgar 56