Personal SE. Computer Memory Addresses C Pointers

Similar documents
Goals of this Lecture

Memory, Data, & Addressing II CSE 351 Spring

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

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

Basic Pointers. CSCI 112: Programming in C

[0569] p 0318 garbage

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

Pointers as Arguments

Intermediate Programming, Spring 2017*

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

SYSC 2006 C Winter 2012

Hardware: Logical View

Computer Organization & Systems Exam I Example Questions

Integer Representation

Pointers. 10/5/07 Pointers 1

What is an algorithm?

APS105. Pointers. Memory RAM 11/5/2013. Pointers. Need a way to refer to locations of things. a pointer: Address. Example: In C

Pointers, Arrays and Parameters

BSM540 Basics of C Language

2) Using the same instruction set for the TinyProc2, convert the following hex values to assembly language: x0f

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Lecture 04 Introduction to pointers

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


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

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

EM108 Software Development for Engineers

Intermediate Programming, Spring 2017*

Documentation Nick Parlante, 1996.Free for non-commerical use.

ECE331 Homework 4. Due Monday, August 13, 2018 (via Moodle)

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Programming Studio #9 ECE 190

Survey. Motivation 29.5 / 40 class is required

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

Memory, Data, & Addressing II

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

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Applications of Pointers (1A) Young Won Lim 4/24/18

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

Pointers. Mr. Ovass Shafi (Assistant Professor) Department of Computer Applications

Tutorial 1: C-Language

CS102 Software Engineering Principles

Arrays, Pointers and Memory Management

CPSC 213, Summer 2017, Term 2 Midterm Exam Solution Date: July 27, 2017; Instructor: Anthony Estey

Applications of Pointers (1A) Young Won Lim 4/11/18

Pointers! Arizona State University 1

Dynamic Memory Allocation

Variation of Pointers

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

CMSC 313 Fall2009 Midterm Exam 1 Section 01 October 12, 2009

MIPS Functions and the Runtime Stack

Fall 2018 Discussion 2: September 3, 2018

Computer Programming

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

Week 6 Part 1. Kyle Dewey. Monday, July 30, 12

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

Homework #3 CS2255 Fall 2012

Chapter 6. Problems All programming problems should include design pseudo code either as a separate design document on embedded comments in the code.

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

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

Decimal Representation

A FRAMEWORK FOR DATA STRUCTURES IN A TYPED FORTH

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

CIS133J. Working with Numbers in Java

Personal SE. Arrays Pointers Strings

Learning to Program with Haiku

CSCI E-119 Section Notes. Section 5 Solutions

CISC Processor Design

CS Programming In C

Pointers and Dynamic Memory Allocation

UNIT- 3 Introduction to C++

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Arrays and Pointers (part 1)

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

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

CS 241 Data Organization Binary

a data type is Types

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

BSNL TTA Question Paper-Computers Specialization 2007

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

Introduction to C++ Introduction to C++ 1

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

Memory, Arrays & Pointers

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

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CSC 1107: Structured Programming

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

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

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

Memory Corruption 101 From Primitives to Exploit

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

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Introduction to Computer Science Midterm 3 Fall, Points

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Basic and Practice in Programming Lab7

Semantic actions for expressions

Transcription:

Personal SE Computer Memory Addresses C Pointers

Computer Memory Organization Memory is a bucket of bytes.

Computer Memory Organization Memory is a bucket of bytes. Each byte is 8 bits wide.

Computer Memory Organization Memory is a bucket of bytes. Each byte is 8 bits wide. Question: How many distinct values can a byte of data hold?

Computer Memory Organization Memory is a bucket of bytes. Each byte is 8 bits wide. Question: How many distinct values can a byte of data hold? Bytes can be combined into larger units: Half-words (shorts) 16 bits 65,536 combinations Words (ints) 32 bits 4 10 9 4 billion Double words (long) 64 bits 16 10 18 16 quadrillion

Computer Memory Organization Memory is a bucket of bytes. Each byte is 8 bits wide. Question: How many distinct values can a byte of data hold? Bytes can be combined into larger units: Half-words (shorts) 16 bits 65,536 combinations Words (ints) 32 bits 4 10 9 4 billion Double words (long) 64 bits 16 10 18 16 quadrillion The bucket is actually an array of bytes:

Computer Memory Organization Memory is a bucket of bytes. Each byte is 8 bits wide. Question: How many distinct values can a byte of data hold? Bytes can be combined into larger units: Half-words (shorts) 16 bits 65,536 combinations Words (ints) 32 bits 4 10 9 4 billion Double words (long) 64 bits 16 10 18 16 quadrillion The bucket is actually an array of bytes: Think of it as an array named memory.

Computer Memory Organization Memory is a bucket of bytes. Each byte is 8 bits wide. Question: How many distinct values can a byte of data hold? Bytes can be combined into larger units: Half-words (shorts) 16 bits 65,536 combinations Words (ints) 32 bits 4 10 9 4 billion Double words (long) 64 bits 16 10 18 16 quadrillion The bucket is actually an array of bytes: Think of it as an array named memory. Then memory[ a ] is the byte at index / location / address a.

Computer Memory Organization Memory is a bucket of bytes. Each byte is 8 bits wide. Question: How many distinct values can a byte of data hold? Bytes can be combined into larger units: Half-words (shorts) 16 bits 65,536 combinations Words (ints) 32 bits 4 10 9 4 billion Double words (long) 64 bits 16 10 18 16 quadrillion The bucket is actually an array of bytes: Think of it as an array named memory. Then memory[ a ] is the byte at index / location / address a. Normally the addresses run from 0 to some maximum.

Pictorially N byte Memory N - 1 0 N - 1 Either way (horizontal or vertical) is fine. The key is that memory is logically an array 0

What's In a Number? What does the hexadecimal number 0x4A6F65 mean?

What's In a Number? What does the hexadecimal number 0x4A6F65 mean? Possibilities: It could be the decimal number 4,878,181 It could be the string "Joe" 'J' = 0x4A, 'o' = 0x6F, 'e' = 0x65 It could be the address of the 4,878,181 st byte in memory It could be an instruction to, say, increment (op code = 0x4A) a location (address = 0x6F65) by 1

What's In a Number? What does the hexadecimal number 0x4A6F65 mean? Possibilities: It could be the decimal number 4,878,181 It could be the string "Joe" 'J' = 0x4A, 'o' = 0x6F, 'e' = 0x65 It could be the address of the 4,878,181 st byte in memory It could be an instruction to, say, increment (op code = 0x4A) a location (address = 0x6F65) by 1 How do we know??????

What's In a Number? What does the hexadecimal number 0x4A6F65 mean? Possibilities: It could be the decimal number 4,878,181 It could be the string "Joe" 'J' = 0x4A, 'o' = 0x6F, 'e' = 0x65 It could be the address of the 4,878,181 st byte in memory It could be an instruction to, say, increment (op code = 0x4A) a location (address = 0x6F65) by 1 How do we know?????? We don't until we use it!

What's In a Number? What does the hexadecimal number 0x4A6F65 mean? Possibilities: It could be the decimal number 4,878,181 It could be the string "Joe" 'J' = 0x4A, 'o' = 0x6F, 'e' = 0x65 It could be the address of the 4,878,181 st byte in memory It could be an instruction to, say, increment (op code = 0x4A) a location (address = 0x6F65) by 1 How do we know?????? We don't until we use it! If we send it to a printer, it's a string. If we use it to access memory, it's an address. If we fetch it as an instruction, it's an instruction.

Computer Numbers as Shape-Shifters The ability of numbers to "morph" their meaning is very powerful. We can manipulate characters like numbers. We can change instructions on the fly. We can perform computation on addresses.

Danger Will Robinson! Danger! The ability of numbers to "morph" their meaning is very powerful. We can manipulate characters like numbers. We can change instructions on the fly. We can perform computation on addresses. BUT: What if we use a number other than intended: We get run-time errors (using an integer as an address). We get hard-to-fix bugs (executing data as instructions). We get weird printout (sending addresses to a printer).

Spiderman Is A "C" Programmer The ability of numbers to "morph" their meaning is very powerful. We can manipulate characters like numbers. We can change instructions on the fly. We can perform computation on addresses. BUT: What if we use a number other than intended: We get run-time errors (using an integer as an address). We get hard-to-fix bugs (executing data as instructions). We get weird printout (sending addresses to a printer). With great power comes great responsibility.

Pointers in C Consider the following two declarations: int i ; int *ip ;

Pointers in C Consider the following two declarations: int i ; int *ip ; "*" says that ip is a pointer, not an integer

Pointers in C Consider the following two declarations: int i ; int *ip ; The "*" is attached to the variable, not the type

Pointers in C Consider the following two declarations: int i ; int *ip ; int i, *ip ; Equivalent to these two declarations

Pointers in C Consider the following two declarations: int i ; int *ip ; On most systems, both allocate 32 bits for i and ip.

Pointers in C Consider the following two declarations: int i ; int *ip ; On most systems, both allocate 32 bits for i and ip. The difference? i's contents are treated as an integer just a number. ip's contents are treated as an address (where an integer can be found).

Pointers in C Consider the following two declarations: int i ; int *ip ; On most systems, both allocate 32 bits for i and ip. The difference? i's contents are treated as an integer. All we can manipulate is the integer value in i. ip's contents are treated as an address (where an integer can be found). We can manipulate the address (make it point elsewhere). We can manipulate the integer at the current address.

A Short Example double x = 3.14159 ; double y = 2.71828 ; double *dp ; NAME ADDR VALUE x 108 3.14159 y 116 2.71828 dp 124???????

A Short Example double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; NAME ADDR VALUE x 108 3.14159 y 116 2.71828 dp 124???????

A Short Example double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; NAME ADDR VALUE x 108 3.14159 y 116 2.71828 dp 124??????? & = "address of" The address of a variable is a pointer to the variable's type

A Short Example The Effect double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; NAME ADDR VALUE x 108 3.14159 y 116 2.71828 dp 124 108

A Short Example double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; x = *dp * 2.0 ; NAME ADDR VALUE x 108 3.14159 y 116 2.71828 dp 124 108

A Short Example double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; x = *dp * 2.0 ; NAME ADDR VALUE x 108 3.14159 y 116 2.71828 dp 124 108 * = "dereference" The value the pointer addresses, not the pointer itself

A Short Example The Effect double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; NAME ADDR VALUE x 108 6.28318 y 116 2.71828 dp 124 108 x = *dp * 2.0 ; // same as x = x * 2.0

double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; A Short Example NAME ADDR VALUE x 108 6.28318 y 116 2.71828 dp 124 108 x = *dp * 2.0 ; // same as x = x * 2.0 dp = &y ;

A Short Example The Effect double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; NAME ADDR VALUE x 108 6.28318 y 116 2.71828 dp 124 116 x = *dp * 2.0 ; // same as x = x * 2.0 dp = &y ;

double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; A Short Example NAME ADDR VALUE x 108 6.28318 y 116 2.71828 dp 124 116 x = *dp * 2.0 ; // same as x = x * 2.0 dp = &y ; *dp += x ;

A Short Example The Effect double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; NAME ADDR VALUE x 108 6.28318 y 116 9.00146 dp 124 116 x = *dp * 2.0 ; // same as x = x * 2.0 dp = &y ; *dp += x ;

Pointers Reference Parameters

Pointers Reference Parameters // Swap the wrong way void swap( grade_entry x, grade_entry y ) { grade_entry temp ; temp = x ; x = y ; y = temp ; } return ;

Pointers Reference Parameters // Swap the wrong way void swap( grade_entry x, grade_entry y ) { grade_entry temp ; temp = x ; x = y ; y = temp ; } return ; // Swap the right way void swap( grade_entry *x, grade_entry *y ) { grade_entry temp ; temp = *x ; *x = *y ; *y = temp ; } return ;

Pointers Call by Reference

Pointers Call by Reference // Array element exchange the wrong way swap( grade_list[ i ], grade_list[ j ] ) ;

Pointers Call by Reference // Array element exchange the wrong way swap( grade_list[ i ], grade_list[ j ] ) ; // Array element exchange the right way swap( &grade_list[ i ], &grade_list[ j ] ) ;