Informatica e Sistemi in Tempo Reale

Similar documents
Fundamentals of Programming

Object Oriented Software Design II

Informatica e Sistemi in tempo Reale

Introduction to the C programming language

Informatica e Sistemi in Tempo Reale

EL6483: Brief Overview of C Programming Language

Fundamentals of Programming

Lectures 5-6: Introduction to C

Fundamentals of Programming

Arrays and Pointers (part 1)

Lecture 3: C Programm

Lecture 2: C Programming Basic

First of all, it is a variable, just like other variables you studied

CSE 333 Lecture 2 Memory

Short Notes of CS201

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

CS201 - Introduction to Programming Glossary By

Computers Programming Course 5. Iulian Năstac

Principles of C and Memory Management

Arrays and Pointers (part 1)

Systems Programming and Computer Architecture ( )

Fundamental of Programming (C)

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

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Kurt Schmidt. October 30, 2018

C: How to Program. Week /Mar/05

Lectures 5-6: Introduction to C

CSCI 171 Chapter Outlines

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

In Java we have the keyword null, which is the value of an uninitialized reference type

CSE 307: Principles of Programming Languages

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

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Chapter 2 - Introduction to C Programming

Design Patterns in C++

Object Oriented Software Design

CS 11 C track: lecture 5

Fundamentals of Programming

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

Computer Systems Lecture 9

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Design Patterns in C++

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Functions BCA-105. Few Facts About Functions:

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

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

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Run-time Environments - 2

EM108 Software Development for Engineers

Compiling and Running a C Program in Unix

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

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Procedural programming with C

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

Understanding Pointers

Programming in C - Part 2

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

CMPE-013/L. Introduction to C Programming

CS 61c: Great Ideas in Computer Architecture


Chapter IV Introduction to C for Java programmers

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

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

ESC101N Fundamentals of Computing

Chapter 1 & 2 Introduction to C Language

Programs in memory. The layout of memory is roughly:

Please refer to the turn-in procedure document on the website for instructions on the turn-in procedure.

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

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

A brief introduction to C programming for Java programmers

Pointers and Arrays 1

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

Heap Arrays. Steven R. Bagley

Module 27 Switch-case statements and Run-time storage management

COMP 2355 Introduction to Systems Programming

Dynamic Memory Allocation and Command-line Arguments

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Data types, variables, constants

Fundamentals of Programming

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

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

CS240: Programming in C

C Syntax Out: 15 September, 1995

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

CS240: Programming in C

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Function Calls and Stack Allocation

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Transcription:

Informatica e Sistemi in Tempo Reale Puntatori Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 5, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 1 / 39 Outline 1 Pointer syntax 2 Preprocessor 3 Arguments by reference 4 Pointers and arrays 5 Examples with strings 6 Stack memory G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 2 / 39

Pointers A pointer is a special type of variable that can hold memory addresses Syntax char c; char *pc; int i; int *pi; double d; double *pd; // a char variable // pointer to char variable // an integer variable // pointer to an int variable // double variable // pointer to a double variable In the declaration phase, the * symbol denotes that the variable contains the address of a variable of the corresponding type G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 4 / 39 Syntax - cont. A pointer variable may contain the address of another variable int i; int *pi; pi = &i; The & operator is used to obtain the address of a variable. It is called the reference operator Warning: in C++ a reference is a different thing! Right now, pay attention to the meaning of this operator in C. G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 5 / 39

Indirection The reverse is called indirection operator and it is denoted by * int j; j = *pi; *pi = 7; // get the value pointed by pi // store a value in the address stored in pi In the first assignment, j is assigned the value present at the address pointed by pi. In the second assignment, the constant 7 is stored in the location contained in pi *pi is an indirection, in the sense that is the same as the variable whose address is in pi G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 6 / 39 Example pi is assigned the address of j j is assigned the value of the variable pointed by pi i 5 23456 23460 23464 23468 23472 pi 23456 23476 j 5 23480 23484 23488 G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 7 / 39

Examples point1.c int d = 5; int x = 7; int *pi; pi = &x; printf("%p\n", &x); printf("%p\n", &d); printf("%p\n", pi); printf("%d\n", *pi); //pi = d; // compilation error The commented line is a syntax error We are assigning a variable to a pointer The programmer probably forgot a & or a * d = *pi; printf("%p\n", pi); printf("%d\n", x); printf("%d\n", d); G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 8 / 39 The pre-processor It is time to look in more details at the compilation process That is, translating from high level C code to low-level machine code The step are described below include file lib file C file pre processor compiler object file linker Exe file G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 10 / 39

Pre-processor In this step, the input file is analyzed to process preprocessor directives A preprocessor directive starts with symbol # Example are: #include and #define After this step, a (temporary) file is created that is then processed by the compiler G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 11 / 39 Directives With the include directive, a file is included in the current text file In other words, it is copied and pasted in the place where the include directive is stated With the define directive, a symbol is defined Whenever the preprocessor reads the symbol, it substitutes it with its definition It is also possible to create macros To see the output of the pre-processor, run gcc with -E option (it will output on the screen) gcc -E myfile.c G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 12 / 39

An example main.c #include "myfile.h" #include "yourfile.h" int d; int a=5; int b=6; double c = PI; d = MYCONST; a = SUM(b,d); return (int)a; myfile.h #define MYCONST 76 extern int a, b; #define SUM(x,y) x+y yourfile.h #define PI 3.14 extern int d; // pi grego // a constant // a macro main.c.post # 1 "main.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "main.c" # 1 "myfile.h" 1 extern int a, b; # 2 "main.c" 2 # 1 "yourfile.h" 1 extern int d; # 3 "main.c" 2 int d; int a=5; int b=6; double c = 3.14; d = 76; a = b+d; return (int)a; G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 13 / 39 Macros effects Pay attention to macros, they can have bad effects #define SUM(x,y) x+y int a = 5, b = 6, c; c = 5 * SUM(a,b); What is the value of variable c? G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 14 / 39

Some helpful tricks It is possible to define a macro for obtaining the literal name of a variable: #define LIT_VAR(x) #x A complete example: point2.c #include <stdio.h> #define LIT_VAR(a) #a #define PVAR(y) printf("%s = %d", LIT_VAR(y), y) #define PPUN(y) printf("%s = %p", LIT_VAR(y), y) int d = 5; int x = 7; int *pi; pi = &x; PVAR(d); PPUN(&d); PVAR(x); PPUN(&x); PPUN(pi); PVAR(*pi); d = *pi; PPUN(pi); PVAR(x); PVAR(d); G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 15 / 39 Arguments of function In C, arguments are passed by value With the exception of arrays However, we can use pointers to pass arguments by reference void swap(int *a, int *b) int tmp; tmp = *a; *a = *b; *b = tmp; int x = 1; int y = 2; swap(&x, &y); PVAR(x); PVAR(y); G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 17 / 39

Arrays An array denotes a set of consecutive locations in memory In C, the name of an array is seen as a constant pointer to the first location Therefore, it can be assigned to a pointer, and used as a pointer int array[5] = 1, 2, 4, 6, 8; int *p; int d; p = a; d = *p; // this expression has value 1 G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 19 / 39 Pointer arithmetic It is possible to modify a pointer (i.e. the address) by incrementing/decrementing it int a[5] = 1, 2, 3, 4, 5; int *p; p = a; // p now points to the first // element in the array p++; p+=2; // p now points to the second // element (a[1]) // p now points to the fourth // element (a[3]) Notice that in p++, p is incremented by 4 bytes, because p is a pointer to integers (and an integer is stored in 4 bytes) G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 20 / 39

Array and pointers Array are constant pointers, they cannot be modified int a[10]; int d; int *p; p = &d; a = p; // compilation error, a cannot be modified Remember that the name of an array is not a variable, but rather an address! It can be used in the right side of an assignment expression, but not in the left side. G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 21 / 39 Equivalent syntax A pointer can be used to access the elements of an array in different ways: int a[10]; int *p; p = a; *(p+1); // equivalent to a[1] int i; *(p+i); p[i]; *(a+i); // equivalent to a[i] // this is a valid syntax // this is also valid In other words, a and p are equivalent also from a syntactic point o view G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 22 / 39

Pointer arithmetic - II The number of bytes involved in a pointer operator depend on the pointer type An operation like p++ increments the pointer by 1 byte if p is of type char 2 bytes if p is of type float 4 bytes if p is of type int To obtain the size of a type, you can use the macro sizeof() int a, b; char c; double d; a = sizeof(int); // a is 4 after the assignment a = sizeof(c); // c is a char, so a is assigned 1 sizeof() must be resolved at compilation time (usually during preprocessing) G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 23 / 39 Pointer arithmetic - III struct.c Pointer arithmetic is also applied to user-defined types; #include <stdio.h> typedef struct mystruct int a; double b[5]; char n[10]; ; struct mystruct array[10]; printf("size of mystruct: %ld\n", sizeof(struct mystruct)); struct mystruct *p = array; printf("p = %p\n", p); p++; printf("p = %p\n", p); G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 24 / 39

void pointers In C/C++, the keyword void denotes something without a type For example the return value of a function can be specified as void, to mean that we are not returning any value When we want to define a pointer that can point to a variable of any type, we specify it as a void pointer void *p; int d; p = &d; p++; // error, cannot do arithmetic // with a void pointer G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 25 / 39 Pointers and structures When using pointers with structures, it is possible to use a special syntax to access the fields struct point2d double x, y; int z; ; point2d vertex; point2d *pv; pv = &vertex; (*pv).x; p->x; // pointer to the structure // the following two expressions // are equivalent Therefore, to access a field of the structure through a pointer, we can use the arrow notation p->x G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 26 / 39

Copying a string (using arrays) strcpy.c #include <stdio.h> int strcpy(char *p, char *q) int c = 0; while (q[c]!= 0) p[c] = q[c++]; p[c] = 0; return c; char name[] = "Lipari"; char copy[10]; strcpy(copy, name); printf("name = %s\n", name); printf("copy = %s\n", copy); G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 28 / 39 Copying a string, (using pointers) strcpy2.c #include <stdio.h> int strcpy(char *p, char *q) int c = 0; while (*q!= 0) *(p++) = *(q++); c++; *p = 0; return c; char name[] = "Lipari"; char copy[10]; strcpy(copy, name); printf("name = %s\n", name); printf("copy = %s\n", copy); G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 29 / 39

Memory allocation We have discussed the rules for the lifetime and visibility of variables Global variables are defined outside of any function. Their lifetime is the duration of the program: they are created when the program is loaded in memory, and deleted when the program exits Local variables are defined inside functions or inside code blocks (delimited by curly braces and ). Their lifetime is the execution of the block: they are created before the block starts executing, and destroyed when the block completes execution Global and local variables are in different memory segments, and are managed in different ways G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 31 / 39 Memory segments The main data segments of a program are shown below The BSS segment contains global variables. It is divided into two segments, one for initialised data (i.e. data that is initialised when declared), and non-initialised data. The size of this segment is statically decided when the program is loaded in memory, and can never change during execution The STACK segment contains local variables Its size is dynamic: it can grow or shrink, depending on how many local variables are in the current HEAP STACK BSS Dynamic memory Local variables Global variables G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 32 / 39

Example Here is an example: int a = 5; // initialised global data int b; // non initialised global data int f(int i) // i, d and s[] are local variables // will be created on the stack when the double d; // function f() is invoked char s[] = "Lipari";... int s, z; // local variables, are created on the stack // when the program starts f(); // here f() is invoked, so the stack for f() is created G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 33 / 39 Stack A Stack is a data structure with two operations push data on top pop data from top The stack is a LIFO (last-in-first-out) data structure The stack memory is managed in the same way as the data structure When a function is called, all parameters are pushed on to the stack, together with the local data The set of function parameters, plus return address, plus local variables is called Stack Frame of the function The CPU internally has two registers: SP is a pointer to the top of the stack BP is a pointer to the current stack frame while the function is working, it uses BP to access local data when the function finishes, all data is popped from the stack G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 34 / 39

Stack int f(int i) double d; char s[] = "Lipari";... return i; int s, z; * * * * * SP BP f(s); * * z s IP BP i z s IP BP G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 35 / 39 Stack frame We will analyse the stack frame later in the course Right now let s observe the following things: The stack frame for the previous function starts from parameter i and ends with the last character of s[] The stack frame depends only on the number and types of parameters, and number and types of local variables The stack frame can be computed by the compiler, that knows how to access local variables from their position on the stack For example, to access parameter i in the previous example, the compiler takes the value of BP and subtracts 4 bytes: BP - 4 To access local variable d, the compiler uses BP and adds 4 (skipping IP). G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 36 / 39

Recursive functions It is possible to write functions that call themselves This is useful for some algorithms Consider the following function to compute the factorial of a number int fact(int n) int f; if (n <= 0) f = 0; if (n == 1) f = 1; else f = n * fact(n-1); return f; The function uses itself to compute the value of the factorial What happens on the stack? G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 37 / 39 Stack for recursive functions int fact(int n) int f; if (n <= 0) f = 0; if (n == 1) f = 1; else f = n * fact(n-1); return f; First stack frame Second stack frame Third stack frame Fourth stack frame f has been computed, return f IP BP n = 4 f IP BP n = 3 f IP BP n = 4 G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 38 / 39

Stack frames Every time we call a function we generate a different stack frame Every stack frame corresponds to an instance of the function Every instance has its own variables, different from the other instances Stack frame is an essential tool of any programming language As we will see later, the stack frame is also essential to implement the operating system G. Lipari (Scuola Superiore Sant Anna) Introduction to C October 5, 2011 39 / 39