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

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

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

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

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

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

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

Type (1A) Young Won Lim 2/17/18

Pointers (1A) Young Won Lim 11/1/17

Applications of Pointers (1A) Young Won Lim 12/26/17

Pointers (1A) Young Won Lim 12/4/17

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

Pointers (1A) Young Won Lim 10/23/17

Arrays (1A) Young Won Lim 12/4/17

Arrays (1A) Young Won Lim 1/27/17

Applications of Arrays (1A) Young Won Lim 3/15/17

Overview (1A) Young Won Lim 9/25/17

Applications of Structures (1A) Young Won Lim 12/8/17

Overview (1A) Young Won Lim 9/14/17

Function Overview (1A) Young Won Lim 10/23/17

Overview (1A) Young Won Lim 9/9/17

Applications of Pointers (1A) Young Won Lim 3/14/18

Applications of Pointers (1A) Young Won Lim 3/21/18

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

Applications of Pointers (1A) Young Won Lim 2/27/18

Applications of Structures (1A) Young Won Lim 12/4/17

Applications of Pointers (1A) Young Won Lim 3/31/18

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

Applications of Arrays (1A) Young Won Lim 2/11/17

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

Variables (2D) Young Won Lim 3/28/18

Memory Arrays (4H) Gate Level Design. Young Won Lim 3/15/16

Structures (1A) Young Won Lim 12/4/17

Arrays and Strings (2H) Young Won Lim 3/7/18

Program Structure (2A) Young Won Lim 5/28/18

Memory (1A) Young Won Lim 9/7/17

Structure (1A) Young Won Lim 7/30/13

File (1A) Young Won Lim 11/25/16

Number System (1A) Young Won Lim 7/7/10

Expressions (2E) Young Won Lim 3/10/18

Structures (1A) Young Won Lim 11/8/16

Program Structure (2A) Young Won Lim 3/8/18

Pointers (2G) Young Won Lim 3/7/18

Expressions (2E) Young Won Lim 4/9/18

ELF (1A) Young Won Lim 10/22/14

Example 1. Young Won Lim 11/17/17

Example 1 : using 1-d arrays. Young Won Lim 12/13/17

Example 2. Young Won Lim 11/24/17

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

Example 3 : using a structure array. Young Won Lim 11/25/17

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

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

Byte Ordering. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Example 3. Young Won Lim 11/22/17

Pointers (2) Applications

ARM Assembly Exercise (1B) Young Won Lim 7/16/16

Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Accessibility (1A) Young Won Lim 8/22/13

Class (1A) Young Won Lim 9/8/14

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

Side Effects (3A) Young Won Lim 1/13/18

Algorithms Bubble Sort (1B) Young Won Lim 4/5/18

Structures (2I) Young Won Lim 4/17/18

Pointers, Arrays and Parameters

EC 413 Computer Organization

A few notes on using pointers in the C language

Side Effects (3B) Young Won Lim 11/27/17

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

CENG3420 Lecture 03 Review

Representation of Information

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

Distributed Systems 8. Remote Procedure Calls

Structures (2I) Young Won Lim 3/7/18

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

Lecture 4: Instruction Set Architecture

ECE 486/586. Computer Architecture. Lecture # 6

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

by Pearson Education, Inc. All Rights Reserved.

Side Effects (3B) Young Won Lim 11/20/17

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Side Effects (3B) Young Won Lim 11/23/17

Operating Systems. 18. Remote Procedure Calls. Paul Krzyzanowski. Rutgers University. Spring /20/ Paul Krzyzanowski

EL6483: Brief Overview of C Programming Language

What is an algorithm?

Distributed Systems. How do regular procedure calls work in programming languages? Problems with sockets RPC. Regular procedure calls

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

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

Bits, Bytes and Integers

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

ELF (1A) Young Won Lim 3/24/16

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

Bits and Bytes January 13, 2005

EL2310 Scientific Programming

Class (1A) Young Won Lim 11/20/14

Lecture 5: Instruction Set Architectures II. Take QUIZ 2 before 11:59pm today over Chapter 1 Quiz 1: 100% - 29; 80% - 25; 60% - 17; 40% - 3

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

CS429: Computer Organization and Architecture

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

CS222: Dr. A. Sahu. Indian Institute of Technology Guwahati

Transcription:

Pointers (1A)

Copyright (c) 2010-2018 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". Please send corrections (or suggestions) to youngwlim@hotmail.com. This document was produced by using LibreOffice.

Address and Data in a Memory address data address 10 bits 1K x 8 Memory data 8 bits 0x3FF 0x3FE 0x3FD 0x3FC 2 10 = 1024 0x003 0x002 0x001 0x000 HEX address 10 bits data 8 bits 3

Variables int a; a can hold an integer address &a data a a = 100; a holds an integer 100 address &a data a 100 4

Pointer Variables int * p; p holds an address int * p; p holds an address of a int type data &p p pointer to int int * p; *p holds an integer int p *p 5

Dereferencing The address of a variable : Address of operator & The content of a pointed location : Dereferencing operator * &p p p & p p * *p p * *p 6

Variables and their addresses address data int a; &a a int * p; &p p 7

Assignment of a variable address data int a; &a a = 111 int b; &b b = 8

Assignment of an address address data int a; &a a = 111 int * p; &p p = 9

Variables with initializations address data int a; &a a int * p = &a; &p p = &a 10

Pointed addresses : p address data int a; p a int * p = &a; &p p p &a 11

Dereferenced Variable : *p address data int a; p *p *p a int * p = &a; &p p p &a *p a 12

Another way to access a : *p address data &a &p a p *p =100 a = 100 1) Read/Write a 2) Read/Write *p 13

1. Pass by Reference 2. Arrays 14

Pass by Reference 15

Variable Scopes int func1 (int a, int b) { int i, int j;...... }...... i and j s variable scope cannot access each other Only top stack frame is active and its variable can be accessed Communications are performed only through the parameter variables int main () { int x, int y;...... x and y s variable scope func1 s Stack Frame int i, int j; int a, int b a b } func1 ( 10, 20 );...... main s Stack Frame ( 10, 20 ) int x, int y; x y 16

Pass by Reference int func1 (int* a, int* b) { int i, int j;...... x and y are made known to func1 func1 can read / write x and y through their addresses }...... *a *b a=&x b=&y int main () { int x, int y;...... } func1 ( &x, &y );...... func1 s Stack Frame int i, int j; int* a, int* b x and y s variable scope ( &x, &y ) main s Stack Frame int x, int y; a b x y *a *b 17

Swapping integers &a a = 111 &a a = 222 &b b = 222 &b b = 111 int a, b; swap( &a, &b ); swap( int *, int *); function call function prototype 18

Integer and Integer Pointer Types int *m int *n integer pointer declarations a way of thinking int * int * m n m n integer pointer variables int * int *m int *n *m *n treated as integer variables int 19

Pass by integer reference void swap(int *p, int *q) { int tmp; } tmp = *p; *p = *q; *q = tmp; int * p int *q int * p int *q int tmp int a, b; swap( &a, &b ); 20

Arrays 21

Accessing array elements using an address int x[5]; x holds the starting address of 5 consecutive int variables 5 int variables address data index data cannot change address x (constant) x x + 1 *x *(x+1) 0 1 2 3 4 x[0] x[1] x[2] x[3] x[4] x + 2 x + 3 x + 4 *(x+2) *(x+3) *(x+4) 22

Accessing an Array with a Pointer Variable int x [5] = { 1, 2, 3, 4, 5 }; int *p = x; &x x &p p x is a constant symbol cannot be changed p[0] p[1] p[2] p[3] p[4] x[0] x[1] x[2] x[3] x[4] 80 90 40 70 60 *(x+0) *(x+1) *(x+2) *(x+3) *(x+4) *(p+0) *(p+1) *(p+2) *(p+3) *(p+4) p is a variable can point to other addresses 23

Byte Address Little Endian Big Endian 24

Byte Address long a; Increasing address a &a? 25

Numbers in Positional Notation long a = 0x1020304050607080; 8 (bytes) a 7 a 6 a 5 a 4 a 3 a 2 a 1 a 0 Most Significant Byte Least Significant Byte a 7 = 0 x 10 a 6 = 0 x 20 a 5 = 0 x 30 a 4 = 0 x 40 a 3 = 0 x 50 a 2 = 0 x 60 a 1 = 0 x70 a 0 = 0 x 80 16 7 16 6 16 5 16 4 16 3 16 2 16 1 16 0 the highest weight the lowest weight 26

Little / Big Endian long a; Increasing address a MSByte Little Endian LSByte a 7 a 6 a 5 a 4 a 3 a 2 a 1 a 0 LSByte Big Endian MSByte a 0 a 1 a 2 a 3 a 4 a 5 a 6 a 7 27

Little Endian Byte Address Example Increasing address 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 long a; a 7 a 0 Increasing weight &a MSByte Little Endian LSByte 28

Big Endian Byte Address Example Increasing address 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 long a; a 0 a 7 Increasing weight &a LSByte Big Endian MSByte 29

Representations of Endianness downward, increasing address upward, increasing address &a &a Big Endian Little Endian 0x3000 a 7 0x3000 a 0 0x3007 a 0 0x3007 a 7 0x3001 0x3001 0x3006 0x3006 0x3002 0x3002 0x3005 0x3005 0x3003 0x3003 0x3004 0x3004 0x3004 0x3004 0x3003 0x3003 0x3005 0x3005 0x3002 0x3002 0x3006 0x3006 0x3001 0x3001 0x3007 a 0 0x3007 a 7 0x3000 a 7 0x3000 a 0 Big Endian Little Endian &a &a https://stackoverflow.com/questions/15620673/which-bit-is-the-address-of-an-integer 30

Little / Big Endian Processors Processor Endianness Motorola 68000 Big Endian PowerPC (PPC) Big Endian Sun Sparc Big Endian IBM S/390 Big Endian Intel x86 (32 bit) Little Endian Intel x86_64 (64 bit) Little Endian Dec VAX Little Endian Alpha (Big/Little) Endian ARM (Big/Little) Endian IA-64 (64 bit) (Big/Little) Endian MIPS (Big/Little) Endian http://www.yolinux.com/tutorials/endian-byte-order.html 31

Pointer Types 32

Integer Type Variables and Byte Addresses long a; Increasing address a int b; b &a short c; c &b char d; d &d &c Little Endian 33

Points to the LSByte Increasing address a &p p long *p; b &q q int *q; c &r r short *r; d &s s char *s; Little Endian 34

Sizes of Integer Types Increasing address long a; sizeof(long) 8 (bytes) a &a int b; sizeof(int) 4 (bytes) b &b short c; sizeof(short) 2 (bytes) c &c char d; sizeof(char) 1 (bytes) d Memory Alignment in the Little Endian &d 35

Pointer to integer values 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 int *p; 36

Pointer to short values 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 short *q; 37

Pointer to char values 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 char *r; 38

Initial pointer values Increasing address int *p; p short *q; q char *r; r 39

Incrementing / decrementing pointer values Increasing address int *p; p+1 p p-1 short *q; q+1 q q-1 char *r; 40 r+1 r r-1

Memory Alignment (1) - allocation of variables Memory Alignment: the data address is a multiple of the data size. enforced by compilers 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 efficient memory access int a; short b; char c; 41

Memory Alignment (2) integer multiple 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 int *p; k = 0,1, 2, 4 k short *q; char *r; 42 2 k 1 k

Memory Alignment (3) 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 int *p; short *q; char *r; 43

Memory Alignment (4) 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 10 20 30 40 50 60 70 80 int *p; p+1 p short *q; q+3 q+2 q+1 q char *r; r+7 r+6 r+5 r+4 r+3 r+2 r+1 r 44

Pointer Type Cast 45

Re-interpretation of memory data case I &a long a; 0x1020304050607080 int *p; 0x50607080 p = (int *) &a short *q; 0x7080 q = (short *) &a char *r; 80 r = (char *) &a 46

Pointer Type Cast &a long a; 0x10203040 0x50607080 starting address int *p; p p = (int *) &a short *q; char *r; 47

Integer Pointer Types &a long a; 0x1020 0x3040 0x5060 0x7080 int *p; short *q; q q = (short *) &a char *r; 48

Integer Pointer Types &a long a; 10 20 30 40 50 60 70 80 int *p; short *q; char *r; r r = (char *) &a 49

Re-interpretation of memory data case II char a; &a 10 20 30 40 50 60 70 80 Memory alignment constraint is not met long *o; o = (long *) &a int *p; p = (int *) &a short *q; q = (short *) &a char *r; r = &a In this case, the memory alignment constraint can be broken 50

const pointers 51

const type, const pointer type (1) const int * p; int * const q ; const int * const r ; read only integer value read only integer pointer read only integer value read only integer pointer 52

const type, const pointer type (2) const int * p; int * const q ; read only integer integer wr integer p address read only q q address wr 53

const type, const pointer type (3) const int * const r ; read only r r address wr read only integer integer wr 54

Pointer Types and Associated Data 8 bits data data data increasing address char val short val int val pc ps pi address address address char *pc; short *ps; int *pi; 55

Pointer Types 8 bits data data data increasing address pc ps pi address address address char *pc; short *ps; int *pi; 56

Little Endian Example 8 bits 8 bits data data &a increasing address &c &b c b a int a; short b; char c; the order of definition &b &c a b c increasing address &a 57

int *, short *, char * type variables int * pi; short * ps; char * pc; pi ps pc address Not a sized representation 58

Pointer Variable Assignment 8 bits &c &b &a data c b a pi ps pc address char * pc; short * ps; int * pi; int a; short b; char c; pi = &a; ps = &b; pc = &c; 59

Pointer Type Casting 8 bits data data data &a a *pc &a a *ps &a *pi a pc ps pi address address address char *pc; pc = (char *) &a short *ps; ps = (short *) &a int *pi; pi = (int *) &a 60

Accessing bytes of a variable 8 bits data data pc+3 *(pc+3) pc+3 a pc+2 pc+1 *(pc+2) *(pc+1) pc+2 pc+1 &a pc &a *(pc+0) pc pc pc address address char *pc; pc = (char *) &a char *pc; pc = (char *) &a 61

32-bit and 64-bit Address 32-bit machine : address : 4 bytes 64-bit machine : address : 8 bytes 8 bits 8 bits pi ps pc 64-bit machine address : 8 bytes 32-bit machine address : 4 bytes 62

64-bit machine : 8-byte address pi ps pc char *pc; short *ps; int *pi; 63

64-bit machine : 8-byte address & data buses 8 bits char *pc; pc 8 bits short *ps; ps 8 bits int *pi; pi 64

32-bit machine : 4-byte address pi ps pc char *pc; short *ps; int *pi; 65

64-bit machine : 8-byte address and data buses 8 bits pc char *pc; 8 bits ps short *ps; 8 bits pi int *pi; 66

References [1] Essential C, Nick Parlante [2] Efficient C Programming, Mark A. Weiss [3] C A Reference Manual, Samuel P. Harbison & Guy L. Steele Jr. [4] C Language Express, I. K. Chun 67