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

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

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

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

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 12/4/17

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

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

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

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

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

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

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

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

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

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

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

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

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

Applications of Arrays (1A) Young Won Lim 3/15/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 3/31/18

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

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

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

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

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

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

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

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

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

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

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

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

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

Example 1. Young Won Lim 11/17/17

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

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

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

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

Example 2. Young Won Lim 11/24/17

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

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

Example 3. Young Won Lim 11/22/17

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

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

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

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

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

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

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

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

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

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

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

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

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

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

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

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

Algorithms Overview (1A) Young Won Lim 3/29/18

Pointers, Arrays and Parameters

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

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

Binary Search Tree (3A) Young Won Lim 6/2/18

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

Fundamentals of Programming Session 20

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

Binary Search Tree (2A) Young Won Lim 5/17/18

Polymorphism (1A) Young Won Lim 8/15/13

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

Pointers (2) Applications

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

by Pearson Education, Inc. All Rights Reserved.

Algorithms (7A) Young Won Lim 4/10/17

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

A few notes on using pointers in the C language

Algorithms (7A) Young Won Lim 4/18/17

Lecture 3: C Programm

Lecture 04 Introduction to pointers

EL6483: Brief Overview of C Programming Language

SYSC 2006 C Winter 2012

Eulerian Cycle (2A) Young Won Lim 4/26/18

Link 3. Symbols. Young W. Lim Mon. Young W. Lim Link 3. Symbols Mon 1 / 42

Monad (1A) Young Won Lim 6/9/17

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

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

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

Binary Search Tree (3A) Young Won Lim 6/4/18

Functions (4A) Young Won Lim 5/8/17

Day08 A. Young W. Lim Mon. Young W. Lim Day08 A Mon 1 / 27

RAM (1A) Young Won Lim 11/12/13

Structure (1A) Component

Background Functions (1C) Young Won Lim 5/26/18

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

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.

Byte 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 Byte Address HEX address 10 bits data 8 bits 3

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

Pointer Variables int * p; p holds an address p can hold the address of an int data *p can hold an integer value type variable int * p; pointer to int &p p int * p; 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 value address data int a; &a a = 111 int b; &b b = b = a; 8

Assignment of an address address data int a; &a a = 111 int * p; &p p = p = &a; 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 int * p = &a; &p p p &a *p a 12

Two way to access: a and *p address &a data a a = 100 &p p *p =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

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 ); 19

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 variables types 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 Addresses long a; Increasing address a 8-byte size data type &a? which byte? 25

Little / Big Endian Ordering of Bytes 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 Increasing weight LSByte Big Endian MSByte a 0 a 1 a 2 a 3 a 4 a 5 a 6 a 7 Increasing weight 26

Increasing address, Increasing weight downward, increasing address upward, increasing address &a &a Big Endian Little Endian Increasing address Increasing weight Increasing address Increasing weight Big Endian Little Endian &a &a https://stackoverflow.com/questions/15620673/which-bit-is-the-address-of-an-integer 27

Pointer Types 28

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

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 30

Aligning variables of different sizes long a; int b; short c; char d; 8-byte slots 4-byte slots 2-byte slots 1-bytes slots all these slots are aligned Increasing address a Memory Alignment in the Little Endian b &a &b c &c d &d 31

Possible addresses for int values 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 int *p; 4 k k = 0,1, 2, 32

Possible addresses for short values 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 short *q; 2 k k = 0,1, 2, 33

Possible addresses for char values 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 char *r; 1 k k = 0,1, 2, 34

Data size at an address the same address is assumed p = q = r Increasing address int *p; p short *q; q char *r; r 35

Associated data at the pointed addresses the same address is assumed p = q = r Increasing address int *p; *p p short *q; *q q char *r; *r r 36

Incrementing / decrementing pointers the same address is assumed p = q = r Increasing address int *p; p+1 p p 1 short *q; q+1 q q 1 char *r; 37 r+1 r r 1

Dereferencing the pointers the same address is assumed p = q = r Increasing address int *p; *(p+1) *p *(p-1) p short *q; *(q+1) *q *(q-1) char *r; *(r+1) q *r *(r-1) r 38

32-bit word view the same address is assumed p = q = r 1 word = 4 bytes = 4 (8 bits) = 32 bits Increasing address Increasing address Increasing address *(p-1) *(q-1) *p *(q+1) *(p+1) *q *r int *p; short *q; char *r; 39

64-bit word view Increasing address 1 word = 8 bytes = 8 (8 bits) = 64 bits the same address is assumed p = q = r *(p-1) *(p+1) *p int *p; *(q-1) *(q+1) *q short *q; *r char *r; 40

32-bit vs. 64-bit machines address data data unit per memory access 32-bit 64-bit word size 32-bit 64-bit max address space 2^32 2^64 word 41

Pointer Type Cast 42

Changing the associated data type of an address long a; &a address of a long value int * p; address of an int value short * q; address of a short value char * r; address of a char value 43

Pointer Type Casting long a; address of a long value &a int * p; address of an int value p = (int *) &a short * q; address of a short value q = (short *) &a char * r; address of a char value r = (char *) &a 44

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 45

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

const pointers 47

const type, const pointer type (1) const int * p; int * const q ; const int * const r ; constant integer value constant integer pointer constant integer value constant integer pointer constant must not be changed must not be updated must not be written must not be assigned 48

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 49

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

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 51