Principles of C and Memory Management

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

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Introduction to Scientific Computing and Problem Solving

ECET 264 C Programming Language with Applications

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

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

Introduction to C. Systems Programming Concepts

Basic and Practice in Programming Lab7

ECET 264 C Programming Language with Applications

Fundamental of Programming (C)

Q. 1 What will be the output of the following program? Justify your answer. [4] #include <stdio.h> main(){ int i=4, a[5]={1,2,3,4,5};

Content. In this chapter, you will learn:

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Programming. Data Structure

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

Decimal Representation

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

ESc101: Pointers and Arrays

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

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

C - Func(on, Pointer, Array. Zhaoguo Wang

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

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

Programming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014

CS 11 C track: lecture 5

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

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Final Intro to C Review

CS 61c: Great Ideas in Computer Architecture

Subject: Fundamental of Computer Programming 2068

Computer Organization & Systems Exam I Example Questions

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

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

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Example: Pointer Basics

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Computer Programming Unit 3

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

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

POINTER & REFERENCE VARIABLES

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

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Chapter 3. Fundamental Data Types

QUIZ on Ch.8. What is kit?

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

ESC101N Fundamentals of Computing

Fundamentals of Programming

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

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Functions. Arash Rafiey. September 26, 2017

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

C library = Header files + Reserved words + main method

Informatica e Sistemi in Tempo Reale

Dynamic Allocation of Memory Space

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Fundamentals of Programming Session 20

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

Computer Hardware Engineering

Procedural programming with C

Principles of C and Memory Management

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

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

Exercise Session 2 Simon Gerber

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Pointers (2) Applications

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Q1: Multiple choice / 20 Q2: Arrays / 40 Q3: Functions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Pointers (continued), arrays and strings

Character Strings. String-copy Example

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Data Type Fall 2014 Jinkyu Jeong

High Performance Programming Programming in C part 1

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #07. Topic: Pointer in C Date:

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Fundamental of Programming (C)

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

ISA 563 : Fundamentals of Systems Programming

Sums. Here's a (poor) program to add three numbers

Character Array. C Programming. String. A sequence of characters The last character should be \0 that indicates the end of string 5 \0

THE FUNDAMENTAL DATA TYPES

... Lecture 12. Pointers

Government Polytechnic Muzaffarpur.

Practice Sheet #07 with Solutions

Tutorial 10 Pointers in C. Shuyue Hu

Unit 3. Operators. School of Science and Technology INTRODUCTION

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

CSE 333 Lecture 2 Memory

Lecture 3: C Programm

Transcription:

COMP281 Lecture 8 Principles of C and Memory Management Dr Lei Shi

Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic, Functions

Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic, Functions Recap

Pointer Basics - Variable - Address & - Pointer * - Double Pointer ** Name Address Content int x = 9; x 0x7ffeebee48c8 9 int *y = &x; y 0x7ffeebee48c0 0x7ffeebee48c8 int **z = &y; z 0x7ffeebee48b8 0x7ffeebee48c0

Arrays - Declaring - Initialising - Accessing - 2D arrays int n[10], i, j; for(i=0; i<10; i++) { n[i] = i + 100; for(j=0; j<10; j++) { printf("n[%d]=%d\n", j, n[j]); for (i=0; i<4; i++) { for (j=0; j<3; j++) { arr[i][j] ; /* 2D array */

Functions - Declaring - Initialising - Accessing - Call-by-value vs call-by-reference void incr(int x) { x++; int x = 10; incr(x); void incr(int *x) { (*x)++; int x = 10; incr(&x);

Last Lecture Pointer Basics Previous Lectures Today Arrays, Arithmetic, Functions Pointer to Array Pointer Arithmetic Pointer with Functions

Pointer to Array

Highest address An array consist of contiguous memory locations. The highest address corresponds to the last element. The lowest address corresponds to the first element. Nums[n] Nums[2] Nums[1] Nums[0] Last element First element Lowest address

When an array is declared, the Compiler allocates sufficient amount of memory to contain all the elements of the array. Base address i.e. address of the first element of the array is also allocated by the Compiler Highest address Nums[n] Nums[2] Nums[1] Nums[0] Last element First element Lowest address Base address

Address vs pointer Let int arr[5] = {1,2,3,4,5; Assuming the base address of arr is 1000, and each integer requires 2 bytes, the 5 elements will be stored as: 1 2 3 4 5 element arr[0] arr[1] arr[2] arr[3] arr[4] address 1000 1002 1004 1006 1008

Address vs pointer Let int arr[5] = {1,2,3,4,5; Assuming the base address of arr is 1000, and each integer requires 2 bytes, the 5 elements will be stored as: 1 2 3 4 5 element arr[0] arr[1] arr[2] arr[3] arr[4] address 1000 1002 1004 1006 1008 The variable arr gives the Base Address, a Constant Pointer, pointing to the 1 st element of the array arr[0], so arr contains the address of arr[0] i.e. 1000.

arr has 2 purpose It is the name of the array It acts as a pointer pointing towards the 1 st element in the array arr is equal to &arr[0] by default We can declare a pointer of type int to point to the array arr

Example 1 #include <stdio.h> int main() { int arr[5] = {1,2,3,4,5; int *p = arr; printf("%i\n", *p); p++; printf("%i\n", *p); return 0;

Example 1 #include <stdio.h> int main() { int arr[5] = {1,2,3,4,5; Output 1 2 int *p = arr; printf("%i\n", *p); p++; printf("%i\n", *p); return 0;

Use a pointer to point to an array, then we can use the pointer to access the elements of the array.

Example 2 #include <stdio.h> int main() { int i; int arr[5] = {1, 2, 3, 4, 5; int *p = arr; for (i = 0; i < 5; i++) { printf("%d\n", *p); p++; return 0;

Example 2 #include <stdio.h> int main() { int i; int arr[5] = {1, 2, 3, 4, 5; int *p = arr; for (i = 0; i < 5; i++) { printf("%d\n", *p); p++; return 0; Output 1 2 3 4 5 The pointer *p prints all the values stored in the array one by one.

Example 2 #include <stdio.h> int main() { int i; int arr[5] = {1, 2, 3, 4, 5; int *p = arr; for (i = 0; i < 5; i++) { printf("%d\n", *p); p++; return 0; What if we replace printf("%d\n", *p); with printf("%d", arr[i]); printf("%d", i[arr]); printf("%d", arr+i ); printf("%d", *(arr+i)); printf("%d", *arr);

Example 2 #include <stdio.h> int main() { int i; int arr[5] = {1, 2, 3, 4, 5; int *p = arr; for (i = 0; i < 5; i++) { printf("%d\n", *p); p++; printf("%d", arr[i]); Prints all array elements printf("%d", i[arr]); Also prints all elements of array printf("%d", arr+i); Prints address of array elements printf("%d", *(arr+i)); Prints value of array elements return 0; printf("%d", *arr); Prints value of a[0] only

Example 2 #include <stdio.h> int main() { int i; int arr[5] = {1, 2, 3, 4, 5; int *p = arr; for (i = 0; i < 5; i++) { printf("%d\n", *p); p++; return 0; What about arr++;?

Example 2 #include <stdio.h> int main() { int i; int arr[5] = {1, 2, 3, 4, 5; int *p = arr; for (i = 0; i < 5; i++) { printf("%d\n", *p); p++; return 0; What about arr++;? Compiler time error -> cannot change Base Address of an array (Constant Pointer)

Arrays are pointers in disguise. Arrays: syntactic sugar for pointers. int i = 0, arr[5] = {1, 2, 3, 4, 5; printf("arr[i] = %d\n", arr[i]); printf("arr[i] = %d\n", *(arr + i)); arr[i] and *(arr + i) are identical arr is identical to &arr[0]

Pointer to Arithmetic

Pointer arithmetic Add/subtract integers to/from pointers (assume 4 byte integers) int arr[] = { 1, 2, 3, 4, 5 ; int *p = arr; /* (*p) ==? */ p++; /* (*p) ==? */ p += 2; /* (*p) ==? */ p -= 3; /* (*p) ==? */

Pointer arithmetic Add/subtract integers to/from pointers (assume 4 byte integers) 0x7ffeecaea8c0

Pointer arithmetic Add/subtract integers to/from pointers (assume 4 byte integers) int arr[] = { 1, 2, 3, 4, 5 ; 0x7ffeecaea8c0 1 2 3 4 5 arr 0x7ffeecaea8c0

Pointer arithmetic Add/subtract integers to/from pointers (assume 4 byte integers) int *p = arr; /* (*p)==? */ 0x7ffeecaea8c0 1 2 3 4 5 arr 0x7ffeecaea8c0 p 0x7ffeecaea8c0

Pointer arithmetic Add/subtract integers to/from pointers (assume 4 byte integers) p++; /* (*p)==? */ 0x7ffeecaea8c0 1 2 3 4 5 arr 0x7ffeecaea8c0 p 0x7ffeecaea8c4

Pointer arithmetic Add/subtract integers to/from pointers (assume 4 byte integers) p += 2; /* (*p)==? */ 0x7ffeecaea8c0 1 2 3 4 5 arr 0x7ffeecaea8c0 p 0x7ffeecaea8cc

Pointer arithmetic Add/subtract integers to/from pointers (assume 4 byte integers) p -= 3; /* (*p)==? */ 0x7ffeecaea8c0 1 2 3 4 5 arr 0x7ffeecaea8c0 p 0x7ffeecaea8c0

Note: Pointer arithmetic does NOT add/subtract address directly but in multiplies of the size of type in bytes. int arr[] = { 1, 2, 3, 4, 5 ; int *p = arr; p++; /* means: p = p + sizeof(int); */ 4 (assume 4 byte integers)

Pointer arithmetic Add/subtract integers to/from pointers int *p = arr; p++; (assume 4 byte integers) 0x7ffeecaea8c0 1 2 3 4 5 arr 0x7ffeecaea8c0 p 0x7ffeecaea8c4 = 0x7ffeecaea8c0 + sizeof(int) 4

Note: sizeof() is NOT a function - takes a type name as an argument

Size of pointer On a 64 bit machine, size of all types of pointer, be it int*, float*, chart*, double* is always 8 bytes. When performing arithmetic function e.g. increment on a pointer, changes occur as per the size of their primitive data type.

Size of pointer long unsigned decimal integer printf("sizeof(int) is %lu\n", sizeof(int)); printf("sizeof(char) is %lu\n", sizeof(char)); printf("sizeof(float) is %lu\n", sizeof(float)); printf("sizeof(double) is %lu\n", sizeof(double)); Printf("=========="); printf("sizeof(int*) is %lu\n", sizeof(int*)); printf("sizeof(char*) is %lu\n", sizeof(char*)); printf("sizeof(float*) is %lu\n", sizeof(float*)); printf("sizeof(double*) is %lu\n", sizeof(double*)); Outpout sizeof(int) is 4 sizeof(char) is 1 sizeof(float) is 4 sizeof(double) is 8 ========== sizeof(int*) is 8 sizeof(char*) is 8 sizeof(float*) is 8 sizeof(double*) is 8

Size of pointer int* p1; printf("%p\n", p1); p1++; printf("%p\n", p1); 0x7ffee46608f0 0x7ffee46608f4 char* p2; printf("%p\n", p2); p2++; printf("%p\n", p2); double* p3; printf("%p\n", p3); p3++; printf("%p\n", p3); 0x7ffee240c8f0 0x7ffee240c8f1 0x7ffeebfe08f0 0x7ffeebfe08f8 sizeof(int) is 4 sizeof(char) is 1 sizeof(float) is 4 sizeof(double) is 8 ========== sizeof(int*) is 8 sizeof(char*) is 8 sizeof(float*) is 8 sizeof(double*) is 8

Pointer with Functions

Pointers as function arguments Pointer as a function parameter is used to hold addresses of arguments passed during function call, known as call-by-reference. When a function is called by reference any change made to the reference variable will effect the original variable.

Example 3 #include <stdio.h> void swap(int *a, int *b); int main() { int m = 66, n = 99; printf("m = %d\n", m); printf("n = %d\n\n", n); swap(&m, &n); printf("after swapping:\n\n"); printf("m = %d\n", m); printf("n = %d\n", n); return 0; void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp;

Example 3 #include <stdio.h> void swap(int *a, int *b); int main() { int m = 66, n = 99; printf("m = %d\n", m); printf("n = %d\n\n", n); swap(&m, &n); printf("after swapping:\n\n"); printf("m = %d\n", m); printf("n = %d\n", n); return 0; void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; Outpout m = 66 n = 99 After Swapping: m = 99 n = 66

Functions returning pointer variables A function can return a pointer to the calling function. Be careful! - Local variables of function doesn t live outside of the function - If returning a pointer pointing to a local variable, that pointer will be pointing to nothing when the function ends.

Example 4 #include <stdio.h> int* larger(int *a, int *b); int main() { int m = 66, n = 99; int *p; p = larger(&m, &n); int* larger(int *a, int *b) { if(*a > *b) return a; else return b; printf("%d is larger.\n",*p); return 0;

Example 4 #include <stdio.h> int* larger(int *a, int *b); int main() { int m = 66, n = 99; int *p; p = larger(&m, &n); printf("%d is larger.\n",*p); int* larger(int *a, int *b) { if(*a > *b) return a; else return b; Outpout 99 is larger. return 0;

Pointer to functions A pointer pointing to a function can be used as an argument in another function. - to declare a pointer to a function: type (*pointer-name)(parameter); - an example int (*sum)();

Pointer to functions A function pointer can point to a specific function when it is assigned the name of that function int sum(int, int); int (*s)(int, int); s = sum; - s is a pointer to a function sum. Now sum can be called using function pointer s with required argument values. s(10, 20);

Example 5 #include <stdio.h> int sum(int x, int y) { return x+y; int main() { int (*fp)(int, int); fp = sum; int s = fp(66, 99); printf("sum is %d.\n", s); return 0; Outpout Sum is 165.

Summary

Today Next Pointer to Array Pointer Arithmetic Pointer with Functions Structure Union Typedef String