Fundamentals of Programming

Similar documents
Informatica e Sistemi in Tempo Reale

Object Oriented Software Design II

Fundamentals of Programming

Introduction to the C programming language

EL6483: Brief Overview of C Programming Language

Fundamentals of Programming

Informatica e Sistemi in tempo Reale

Lectures 5-6: Introduction to C

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Informatica e Sistemi in Tempo Reale

Lecture 3: C Programm

Lecture 2: C Programming Basic

CSE 333 Lecture 2 Memory

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

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

CSE 307: Principles of Programming Languages

Arrays and Pointers (part 1)

Fundamental of Programming (C)

C: How to Program. Week /Mar/05

Computers Programming Course 5. Iulian Năstac

Principles of C and Memory Management

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

Systems Programming and Computer Architecture ( )

Chapter 2 - Introduction to C Programming

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

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

Object Oriented Software Design

Fundamentals of Programming

MODULE 5: Pointers, Preprocessor Directives and Data Structures

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

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

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

CSCI 171 Chapter Outlines

Lectures 5-6: Introduction to C

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

Design Patterns in C++

Kurt Schmidt. October 30, 2018

Arrays and Pointers (part 1)

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

CS 11 C track: lecture 5

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Run-time Environments - 2

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

Design Patterns in C++

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CMPE-013/L. Introduction to C Programming

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Computer Systems Lecture 9

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

Functions BCA-105. Few Facts About Functions:

ESC101N Fundamentals of Computing

Dynamic Memory Allocation and Command-line Arguments

Programs in memory. The layout of memory is roughly:

Chapter 1 & 2 Introduction to C Language

Compiling and Running a C Program in Unix

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

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

EM108 Software Development for Engineers

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

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

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

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

Procedural programming with C

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

Fundamentals of Programming

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

Programming in C - Part 2

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

Understanding Pointers


CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

A brief introduction to C programming for Java programmers

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

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

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

Chapter IV Introduction to C for Java programmers

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

CS 61c: Great Ideas in Computer Architecture

CS240: Programming in C

Heap Arrays. Steven R. Bagley

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

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

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

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

C Syntax Out: 15 September, 1995

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

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

COMP 2355 Introduction to Systems Programming

Pointers and Arrays 1

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

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

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

Data types, variables, constants

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

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

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Function Calls and Stack Allocation

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Transcription:

Fundamentals of Programming Pointers Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 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) Pointers February 29, 2012 2 / 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) Pointers February 29, 2012 3 / 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) Pointers February 29, 2012 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) Pointers February 29, 2012 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) Pointers February 29, 2012 6 / 39

Example i 5 23456 23460 23464 23468 pi j 23472 23476 23480 23484 23488 G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 7 / 39

Example pi is assigned the address of j i 5 23456 23460 23464 23468 pi j 23472 23476 23480 23484 23488 G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 7 / 39

Example pi is assigned the address of j i 5 23456 23460 23464 23468 pi j 23456 23472 23476 23480 23484 23488 G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 7 / 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) Pointers February 29, 2012 7 / 39

Examples point1.c int main() { 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) Pointers February 29, 2012 8 / 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) Pointers February 29, 2012 9 / 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) Pointers February 29, 2012 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) Pointers February 29, 2012 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) Pointers February 29, 2012 12 / 39

An example main.c #include "myfile.h" #include "yourfile.h" int d; int a=5; int b=6; int main() { double c = PI; d = MYCONST; a = SUM(b,d); return (int)a; // pi grego // a constant // a macro myfile.h #define MYCONST 76 extern int a, b; #define SUM(x,y) x+y yourfile.h #define PI 3.14 extern int d; G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 13 / 39

An example main.c #include "myfile.h" #include "yourfile.h" int d; int a=5; int b=6; int main() { 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; int main() { double c = 3.14; d = 76; a = b+d; return (int)a; G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 13 / 39

Macros effects Pay attention to macros, they can have bad effects #define SUM(x,y) x+y int main() { int a = 5, b = 6, c; c = 5 * SUM(a,b); What is the value of variable c? G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 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 main() { 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) Pointers February 29, 2012 15 / 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) Pointers February 29, 2012 16 / 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 main() { int x = 1; int y = 2; swap(&x, &y); PVAR(x); PVAR(y); G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 17 / 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) Pointers February 29, 2012 18 / 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) Pointers February 29, 2012 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) Pointers February 29, 2012 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) Pointers February 29, 2012 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) Pointers February 29, 2012 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) Pointers February 29, 2012 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]; ; int main() { 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) Pointers February 29, 2012 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) Pointers February 29, 2012 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) Pointers February 29, 2012 26 / 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) Pointers February 29, 2012 27 / 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; int main() { 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) Pointers February 29, 2012 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; int main() { 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) Pointers February 29, 2012 29 / 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) Pointers February 29, 2012 30 / 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) Pointers February 29, 2012 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 HEAP STACK BSS Dynamic memory Local variables Global variables G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 32 / 39

Memory segments The main data segments of a program are shown below 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 block HEAP STACK BSS Dynamic memory Local variables Global variables G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 32 / 39

Memory segments The main data segments of a program are shown below The HEAP segment contains dynamic memory that is managed directly by the programmer (we will see it later) HEAP STACK BSS Dynamic memory Local variables Global variables G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 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 main() { 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) Pointers February 29, 2012 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 is a pointer to the current stack frame while the function is working, it uses to access local data when the function finishes, all data is popped from the stack G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 34 / 39

Stack int f(int i) { double d; char s[] = "Lipari";... return i; int main() { int s, z; * SP f(s); z s G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 35 / 39

Stack int f(int i) { double d; char s[] = "Lipari";... return i; int main() { int s, z; f(s); * i z s SP G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 35 / 39

Stack int f(int i) { double d; char s[] = "Lipari";... return i; int main() { int s, z; f(s); * i z s SP G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 35 / 39

Stack int f(int i) { double d; char s[] = "Lipari";... return i; int main() { int s, z; f(s); * d i z s SP G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 35 / 39

Stack int f(int i) { double d; char s[] = "Lipari";... return i; int main() { int s, z; f(s); * s[6] = 0............ s[1] = i s[0] = L d i z s SP G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 35 / 39

Stack int f(int i) { double d; char s[] = "Lipari";... return i; int main() { int s, z; f(s); * s[6] = 0............ s[1] = i s[0] = L d i z s SP G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 35 / 39

Stack int f(int i) { double d; char s[] = "Lipari";... return i; int main() { int s, z; f(s); * s[6] = 0............ s[1] = i s[0] = L d i z s SP G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 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 and subtracts 4 bytes: - 4 To access local variable d, the compiler uses and adds 4 (skipping ). G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 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) Pointers February 29, 2012 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 f n = 4 G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 38 / 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; Second stack frame f n = 3 f n = 4 G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 38 / 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; Third stack frame f n = 2 f n = 3 f n = 4 G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 38 / 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; Fourth stack frame f n = 1 f n = 2 f n = 3 f n = 4 G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 38 / 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; f has been computed, return f = 1 n = 1 f n = 2 f n = 3 f n = 4 G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 38 / 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; f has been computed, return f = 1 n = 1 f = 2 n = 2 f n = 3 f n = 4 G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 38 / 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; f has been computed, return f = 1 n = 1 f = 2 n = 2 f = 6 n = 3 f n = 4 G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 38 / 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; f has been computed, return f = 1 n = 1 f = 2 n = 2 f = 6 n = 3 f = 24 n = 4 G. Lipari (Scuola Superiore Sant Anna) Pointers February 29, 2012 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) Pointers February 29, 2012 39 / 39