Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

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

Lecture 2: C Programm

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

C'Programming' data'separate'from'methods/functions' Low'memory'overhead'compared'to'Java'

Lecture 3: C Programm

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

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

CS61C : Machine Structures

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

CS61C : Machine Structures

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

HW1 due Monday by 9:30am Assignment online, submission details to come

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

ECE 15B COMPUTER ORGANIZATION

CS61C : Machine Structures

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

More C Pointer Dangers

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CS 61c: Great Ideas in Computer Architecture

CS61C : Machine Structures

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Pointers and Arrays 1

Memory, Data, & Addressing II CSE 351 Spring

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

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

Arrays and Pointers (part 1)

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Multidimension array, array of strings


Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

Introduction to Scientific Computing and Problem Solving

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

EL2310 Scientific Programming

Pointers (1A) Young Won Lim 3/5/18

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

Arrays and Pointers (part 1)

REFERENCES, POINTERS AND STRUCTS

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

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

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

MYcsvtu Notes LECTURE 34. POINTERS

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

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

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

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Programming Studio #9 ECE 190

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

IT 252 Computer Organization and Architecture Introduction to the C Programming Language

Pointers (1A) Young Won Lim 2/6/18

Pointers (1A) Young Won Lim 1/9/18

Pointers (1A) Young Won Lim 1/5/18

University of California, San Diego CSE 30 Computer Organization and Systems Programming Winter 2014 Midterm Dr. Diba Mirza

CS61C : Machine Structures

Introduction to C Language (M3-R )

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

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Pointers (1A) Young Won Lim 2/10/18

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Pointers. Memory. void foo() { }//return

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

[0569] p 0318 garbage

Pointers, Arrays and C-Strings

CS61C : Machine Structures

Heap Arrays. Steven R. Bagley

Unit IV & V Previous Papers 1 mark Answers

DECLARAING AND INITIALIZING POINTERS

CIS133J. Working with Numbers in Java

Memory, Arrays & Pointers

Pointers, Pointers, Pointers

Midterm Sample Questions. a) What evaluates to FALSE in C? What evaluates to TRUE?

Homework #3 CS2255 Fall 2012

EM108 Software Development for Engineers

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Pointers and Arrays

Has there been an update to ANSI C? Lecture #3 C Pointers Yes! It s called the C99 or C9x std

Memory, Data, & Addressing II

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Intermediate Programming, Spring 2017*

Systems Programming and Computer Architecture ( )

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

UNIT V Sub u P b ro r g o r g a r m a s

n Address of a variable in memory n Allows us to indirectly access variables ! Array n A list of values arranged sequentially in memory

C Pointers. 6th April 2017 Giulio Picierro

Expressions and Casting

Lecture 12 Integers. Computer and Network Security 19th of December Computer Science and Engineering Department

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

We do not teach programming

Data Types. Data Types. Integer Types. Signed Integers

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

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

Midterm CSE 131B Spring 2005

CS61C : Machine Structures

More Pointers Week 11

by Pearson Education, Inc. All Rights Reserved.

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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

Decimal Representation

Transcription:

CSE 30: Computer Organization and Systems Programming Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers Diba Mirza University of California, San Diego 1

Q: Which of the assignment statements produces an error at compilation. Why? int *p, ar[5]; //Declaration i) p=ar+5; ii)ar=p+1; ar 100 104 108 112 116 p 20 A. p=ar+5; B. ar=p+1; C. Both statements result in error at compile time D. Neither results in a compilation error

Q: What happens when the following code is executed? int *p, ar[5]; //Declaration p=ar-5; *p=0; A. Always results in a segmentation fault because a pointer cannot be used to change the value of an array element B. Always results in a segmentation fault because the array element being accessed is out of bounds C. Likely to result in a segmentation fault because the memory location being accessed may not be a valid address D. It results in a compilation error

Arrays Pitfall: An array in C does not know its own length, & bounds not checked! Consequence: We can accidentally access off the end of an array. Consequence: We must pass the array and its size to a procedure which is going to traverse it. Segmentation faults and bus errors: These are VERY difficult to find, so be careful.

Pointer Arithmetic Question How many of the following are invalid? I. pointer + integer (ptr+1) II. integer + pointer (1+ptr) III. pointer + pointer (ptr + ptr) IV. pointer integer (ptr 1) V. integer pointer (1 ptr) VI. pointer pointer (ptr ptr) VII. compare pointer to pointer (ptr == ptr) VIII. compare pointer to integer (1 == ptr) IX. compare pointer to 0 (ptr == 0) X. compare pointer to NULL (ptr == NULL) #invalid A: 1 B: 2 C: 3 D: 4 E: 5

Promotion and truncation Often arithmetic expressions contain mixed integral types Promotion: In an expression all variables are promoted to the data type of the largest size char c=4; int i= c+ 10; /* c is promoted to an int */ /*What is the value in i? */ Truncation: If the value of a larger data type is assigned to a smaller one, the compiler drops the most significant bits short i= 0x0104; char c =i; /*The value in i is truncated*/ /* c =0x04 Least significant byte is assigned */

Type casting If we want the variables in an expression to be interpreted differently, use casting Example: int i=10; float f = i/3; In this case, f=3.0 If we don t want the result of the division to get trunctated to an int, use explicit casting int i=10; float f = (float)i/3; /* Explicit cast of int to float */

Type casting with pointers We can cast addresses that are assigned to pointers to a different type char c =0x04; int *p; p= (int *)&c; /* Treat &c as address of an int */ OR short i =0x0104; char *p; p= (char *)&i; /* Treat &i as address of a char*/

Byte ordering in memory Consider the 4 byte number 0x0a0b0c0d: Big Endian: Most significant byte in lowest memory address 0a 0b 0c 0d Increasing memory addresses Little Endian: Least significant byte in lowest memory address 0a 0b 0c 0d Increasing memory addresses

What is the output of the following code? char *p; short i =0x0104; p= (char *)&i; /* Treat &i as address of a char*/ printf( Value in p is: %x,*p); A.It is always the lower byte 0x04 B.It is always the higher byte 0x01 C.Depends

What is the output of the following code? char *p; char arr[2] ={0x01,0x04}; p= (char *)&arr; printf( Value in p is: %x,*p); A. 0x04 B. 0x01 C. Depends

Consider the following function void swap(int x, int y); void swap(int x, int y) { int tmp; tmp= x; x= y; y= tmp; } //Declaration 12

int main() { int a=10, b=20; swap(a, b); } void swap(int x, int y) { int tmp; tmp= x; x= y; y= tmp; } Q: Which of the following changes would you make to interchange the values in a and b when swap is called? A. In swap, return the values of x and y to the main function after swapping them B. Declare a and b as global variables, so that they become accessible to the swap routine C. Pass the address of a and b to swap instead of their value and modify the swap function D. Move the implementation in swap to the main function 13

Functions: Pass by reference void swap(int *x, int *y) { } Q: What should the modified swap function do? A. Swap the values in x and y B. Swap the values pointed to by x and y C. Both the above operations are equivalent 14

Q: What happens when IncrementPtr(q)is called in the following code: void IncrementPtr(int *p){ p = p + 1; } int A[3] = {50, 60, 70}; int *q = A; IncrementPtr(q); A q 50 60 70 A. q points to the next element in the array with value 60 B. q points to the first element in the array with value 50

Q: How should we implement IncrementPtr(),so that q points to 60 when the following code executes? void IncrementPtr(int **p){ p = p + 1; } int A[3] = {50, 60, 70}; int *q = A; IncrementPtr(&q); A q 50 60 70 A. p = p + 1; B. &p = &p + 1; C. *p= *p + 1; D. p= &p+1;

Pointer Arithmetic What if we have an array of large structs (objects)? C takes care of it: In reality, ptr+1 doesn t add 1 to the memory address, but rather adds the size of the array element. C knows the size of the thing a pointer points to every addition or subtraction moves that many bytes: 1 byte for a char, 4 bytes for an int, etc.