CSCI206 - Computer Organization & Programming

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 9: MIPS Instruction Set
Programming and Data Structure
Chapter 2 — Instructions: Language of the Computer — 1 Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
Computer Organization CS224
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Computer Architecture CSCE 350
Lecture 8: MIPS Instruction Set
Kernighan/Ritchie: Kelley/Pohl:
L5 – Addressing Modes 1 Comp 411 – Spring /29/08 Operands and Addressing Modes Where is the data? Addresses as data Names and Values Indirection.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
Pointers Applications
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
1 Pointers in C. 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
Lecture 4. Miscellaneous Addressing Mode, Memory Map, Pointer, and ASCII Prof. Taeweon Suh Computer Science Education Korea University ECM534 Advanced.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Computer Organization and Design Addressing Modes Montek Singh Wed, Sep 19, 2012 Lecture 6.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Pointers and Arrays in C and Assembly Language. Arrays Provide useful abstraction Match up to machine memory organization Array index computation –time.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Lecture 5 Pointers 1. Variable, memory location, address, value
CS1010 Programming Methodology
CS1010 Programming Methodology
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 8 Arrays, Strings and Pointers
Functions and Pointers
Pointers and Pass By Reference
2016.
COSC 220 Computer Science II
Procedures (Functions)
Pointers and Pointer-Based Strings
Pointers and Arrays in C and Assembly Language
Pointers.
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
Pointers.
Functions and Pointers
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
C-Programming, continued
CSCI206 - Computer Organization & Programming
Local Variables variables which are declared within a
Computer Organization and Design Addressing Modes
Pointers  Week 10.
Lecture 18 Arrays and Pointer Arithmetic
Operands and Addressing Modes
October 1 Programming Questions?
September 5 Fang-Yi’s Office Hours Friday 10am  12pm (and another TBD) Programming Language Issues (JAVA vs. C, Pointers vs. Arrays) Representations Instructions.
Pointers Call-by-Reference CSCI 230
Functions with arrays.
Outline Defining and using Pointers Operations on pointers
Introduction to Problem Solving and Programming
Pointers.
Initializing variables
Pointers and Pointer-Based Strings
Computer Organization and Design Addressing Modes
CSCE 206 Lab Structured Programming in C
Operands and Addressing Modes
9/27: Lecture Topics Memory Data transfer instructions
Pointer Syntax Review *ptrName Declaring Pointers
Conditional Branching (beq)
Presentation transcript:

CSCI206 - Computer Organization & Programming Introducing Pointers in C zyBook: 9.1, 9.2 prelab for lab 7

Pointers Powerful feature of C. Java/C++ “references” are pointers with extra safety checks. In C the programmer is responsible. * is the dereference operator, used to declare a pointer and access memory pointed to by a pointer. & is the address of operator, it returns a pointer.

Pointers int i = 7; int j = -1; int* pi = &i; int *pj = &j; printf(" i = %d\n", i); printf(" j = %d\n", j); printf(" pi= %p\n", pi); printf(" pj= %p\n", pj); printf(" data pointed to by pi= %d\n", *pi); printf(" data pointed to by pj= %d\n\n", *pj); *pi = 6; declare and initialize pointers

Pointers int i = 7; int j = -1; int *pi = &i; int *pj = &j; printf(" i = %d\n", i); printf(" j = %d\n", j); printf(" pi= %p\n", pi); printf(" pj= %p\n", pj); printf(" data pointed to by pi= %d\n", *pi); printf(" data pointed to by pj= %d\n\n", *pj); *pi = 6; dereference the pointer to access the value it points to

Pointers compared to assembly dereference (*pointer) lw $t0, pointer or sw $t0, pointer address of operator (&variable) la $t0, variable

Pointers allow pass by reference void addone(int i){ i = i + 1; } void addone(int *i){ *i = *i + 1; addone: addi $a0, $a0, 1 jr $ra lw $t0, 0($a0) addi $t0, $t0, 1 sw $t0, 0($a0) pass by copy pass by ref

Pass by pointer #include <stdio.h> #include <stdlib.h> void ConvFeetInches(int totDist, int inFeet, int inInches) { inFeet = totDist / 12; inInches = totDist % 12; return; } int main(void) { int initMeasure = 45; int resFeet = 0; int resIn = 0; ConvFeetInches(initMeasure, resFeet, resIn); printf("%d feet %d inches\n", resFeet, resIn); return 0; }

Pass by pointer #include <stdio.h> #include <stdlib.h> void ConvFeetInches(int totDist, int* inFeet, int* inInches) { *inFeet = totDist / 12; *inInches = totDist % 12; return; } int main(void) { int initMeasure = 45; int resFeet = 0; int resIn = 0; ConvFeetInches(initMeasure, &resFeet, &resIn); printf("%d feet %d inches\n", resFeet, resIn); return 0; }

Pointers vs arrays At the machine level, arrays are treated as pointers in C. int arr[10]; assert(arr == &arr[0]); // TRUE printf("%p\n", &arr[0]); printf("%p\n", arr); 0x7ffcc7cdc500

Pointer Arithmetic int array[10] = {30,31,32,33,34,35,36,37,38,39}; int *ptr; printf("Addr of array : %p\n", array); printf("ptr holds addr: %p\n", ptr); ptr = array; printf("ptr holds addr: %p\n\n", ptr); ptr = ptr + 1; printf("Addr of arr[1]: %p\n", &(array[1])); printf("ptr hold addr : %p\n", ptr); printf("ptr points to : %d\n\n", *ptr);

Pointer Arithmetic ptr = ptr + 4; printf("ptr hold addr: %p\n", ptr); printf("ptr points to: %d\n\n", *ptr); ptr--; // or ptr -= 1; double arr2[5] = {0.0, 0.5, 3.1415, 2.71, 1.41}; double *ptr2; ptr2 = arr2; printf("ptr2 holds addr: %p\n", ptr2); ptr2++; printf("ptr2 holds addr: %p\n\n", ptr2);

C array with unknown size This is often used to pass arrays into functions. Does not allocate space for the array! We don’t know input size at compile time. Must pass size as another parameter or use a sentinel value. Beginning of array is passed as a pointer. int arr[]; // valid, same as int *arr int *arr; // valid, same as above

Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) { int *p; for (p = &array[0]; p < &array[size]; p = p + 1) *p = 0; }

clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } move $t0,$zero # i = 0 loop1: sll $t1, $t0, 2 # $t1 = i * 4 add $t2, $a0, $t1 # $t2 = address of array[i] sw $zero, 0($t2) # array[i] = 0 addi $t0, $t0, 1 # i = i + 1 slt $t3, $t0, $a1 # $t3 = (i < size) bne $t3, $zero, loop1 # if (i < size) go to loop1 array access using indices requires multiply by 4 to convert index to array offset

clear2(int. array, int size) { int clear2(int *array, int size) { int *p; for (p = &array[0]; p < &array[size]; p = p + 1) *p = 0; } Clear2 move $t0, $a0 # p = address of array[0] sll $t1, $a1, 2 # $t1 = size * 4 add $t2, $a0, $t1 # $t2 = address of array[size] loop2: sw $zero, 0($t0) # Memory[p] = 0 addi $t0, $t0, 4 # p = p + 4 slt $t3, $t0, $t2 # $t3 = (p < &array[size]) bne $t3, $zero, loop2 # if (p < &array[size]) go to loop2 compute end address. increment pointer by 4 to get next array element.

Clear3 clear3(int *array, int size) { while (size--){ *array++ = 0; clear2(int *array, int size) { int *p; for (p = &array[0]; p < &array[size]; p = p + 1) *p = 0; } clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear3(int *array, int size) { while (size--){ *array++ = 0; } } book real world Few C programmers would actually use a for loop like this, instead consider a while loop.

Pointers vs arrays In assembly it is often easier to operate on pointers rather than arrays. Avoids converting indexes to memory addresses. However, this doesn’t mean operating on pointers in C is more efficient than arrays. The compiler is very good at optimizing array access! Array code can be more readable and less error prone!

Summary Pointers are just addresses of variables. Pointers have types such that ptr ++ means different amount of increment, depending on the type of the pointer.