Intermediate Programming, Spring 2017*

Similar documents
Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

a) (5 points) What is the output of the following code sequence? int *ptr = 0x1050; printf ("%x\n", ptr--); printf ("%x\n", ptr);

Intermediate Programming, Spring 2017*

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Pointers as Arguments

Lab 3. Pointers Programming Lab (Using C) XU Silei

Intermediate Programming, Spring Misha Kazhdan

Intermediate Programming, Spring 2017*

o Code, executable, and process o Main memory vs. virtual memory

Memory, Data, & Addressing II CSE 351 Spring

Intermediate Programming, Spring 2017*

Lecture 2: C Programm

Programming Studio #9 ECE 190

Arrays, Pointers and Memory Management

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

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

Course organization. Course introduction ( Week 1)

Intermediate Programming, Spring 2017*

Introduction to C. Systems Programming Concepts

Slides adopted from T. Ferguson Spring 2016

Variation of Pointers

CSC 1600 Memory Layout for Unix Processes"

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

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

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

DECLARAING AND INITIALIZING POINTERS

Intermediate Programming, Spring 2017*

Pointers. 10/5/07 Pointers 1

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

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

ECE260: Fundamentals of Computer Engineering

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

[0569] p 0318 garbage

Linked List. April 2, 2007 Programming and Data Structure 1

Code Generation II. Code generation for OO languages. Object layout Dynamic dispatch. Parameter-passing mechanisms Allocating temporaries in the AR

ECE 15B COMPUTER ORGANIZATION

Arrays and Pointers (part 1)

ECE 30 Introduction to Computer Engineering

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

EM108 Software Development for Engineers

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Using pointers with functions

Low-Level C Programming. Memory map Pointers Arrays Structures

Pointers, Dynamic Data, and Reference Types

Arrays and Pointers. CSE 2031 Fall November 11, 2013

BBM 201 DATA STRUCTURES

Variables, Pointers, and Arrays

Anne Bracy CS 3410 Computer Science Cornell University

CSCI2467: Systems Programming Concepts

Common Misunderstandings from Exam 1 Material

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

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

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

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Self-referential Structures and Linked List. Programming and Data Structure 1

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

04-17 Discussion Notes

Pointers, Arrays and Parameters

CS 222: Pointers and Manual Memory Management

ECE264 Fall 2013 Exam 1, September 24, 2013

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

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Pointers and Arrays 1

Arrays and Pointers (part 1)

Generic Swap. void swap (void *ap, void *bp, int size) { char temp[size]; memcpy (temp, ap, size); memcpy (ap, bp, size); memcpy (bp, temp, size); }

Solution for Data Structure

Introduction to C Language (M3-R )

SYSC 2006 C Winter 2012

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

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

A Fast Review of C Essentials Part II

Understanding Pointers

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

CS 2461: Computer Architecture I

What have we learned about when we learned about function parameters? 1-1

At the end of this module, the student should be able to:

Advanced Systems Programming

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

CS113: Lecture 4. Topics: Functions. Function Activation Records

CS 261: Data Structures. Dynamic Arrays. Introduction

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

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

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

What is an algorithm?

Algorithms & Data Structures

Lecture 04 Introduction to pointers

Anne Bracy CS 3410 Computer Science Cornell University

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

CSE Lecture In Class Example Handout

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Dynamic Memory Allocation

BITG 1113: POINTER LECTURE 12

Transcription:

600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general and you should not follow my lead on this.

*ptr = *ptr++; In terms of precedence: *ptr = *(ptr++); ptr++ increments the pointer but returns the old value, so the RHS side becomes This gives *(ptr++) ra[0]; ptr ra+1 ra[1]=ra[0]; int ra[5] = 1, 2, 3, 4, 5 ; int *ptr = ra; for( int i=0 ; i<5 ; i++ ) *ptr = *ptr++; printf( "array is: [ ); for( int i=0 ; i<5 ; i++ ) printf( "%d ", ra[i] ); printf( "]\n ); >>./a.out array is: [ 1 1 1 1 1 ] >>

In addition to *ptr giving = *ptr++; the wrong result, In terms this is of bad precedence: code because in the last iteration *ptr we = *(ptr++); are setting ptr++ ra[5] increments = ra[4]; the pointer which but is returns an out-of-bounds the old value, access! so the RHS side becomes *(ptr++) ra[0]; ptr ra+1 This gives ra[1]=ra[0]; int ra[5] = 1, 2, 3, 4, 5 ; int *ptr = ra; for( int i=0 ; i<5 ; i++ ) *ptr = *ptr++; printf( "array is: [ ); for( int i=0 ; i<5 ; i++ ) printf( "%d ", ra[i] ); printf( "]\n ); >>./a.out array is: [ 1 1 1 1 1 ] >>

with copies of the pointers and Swapping the addresses stored in list1 and list2 happens with the stack frame of swap and is not visible when the function returns void swap( char * list1, char * list2 ) char * temp = list1; list1 = list2; list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap(, ); >>./a.out >>

with copies of the pointers and Swapping the addresses stored in list1 and list2 happens with the stack frame of swap and is not visible when the function returns void swap( char * list1, char * list2 ) char * temp = list1; list1 = list2; list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap(, );

with copies of the pointers and Swapping the addresses stored in list1 and list2 happens with the stack frame of swap and is not visible when the function returns void swap( char * list1, char * list2 ) char * temp = list1; list1 = list2; list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap(, ); list1 list2

with copies of the pointers and Swapping the addresses stored in list1 and list2 happens with the stack frame of swap and is not visible when the function returns void swap( char * list1, char * list2 ) char * temp = list1; list1 = list2; list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap(, ); list1 list2

with copies of the pointers and Swapping the addresses stored in list1 and list2 happens with the stack frame of swap and is not visible when the function returns void swap( char * list1, char * list2 ) char * temp = list1; list1 = list2; list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap(, );

with the addresses of the pointers and Swapping the contents of data pointed to by list1 and list2 changes what and point to The effects are visible even after the function returns void swap( char ** list1, char ** list2 ) char * temp = *list1; *list1 = *list2; *list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap( &, &); >>./a.out >>

with the addresses of the pointers and Swapping the contents of data pointed to by list1 and list2 changes what and point to The effects are visible even after the function returns void swap( char ** list1, char ** list2 ) char * temp = *list1; *list1 = *list2; *list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap( &, &);

with the addresses of the pointers and Swapping the contents of data pointed to by list1 and list2 changes what and point to The effects are visible even after the function returns void swap( char ** list1, char ** list2 ) char * temp = *list1; *list1 = *list2; *list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap( &, &); list1 list2

with the addresses of the pointers and Swapping the contents of data pointed to by list1 and list2 changes what and point to The effects are visible even after the function returns void swap( char ** list1, char ** list2 ) char * temp = *list1; *list1 = *list2; *list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap( &, &); list1 list2

with the addresses of the pointers and Swapping the contents of data pointed to by list1 and list2 changes what and point to The effects are visible even after the function returns void swap( char ** list1, char ** list2 ) char * temp = *list1; *list1 = *list2; *list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap( &, &);

Piazza Resources section Resources tab Exercise 5-1