Binghamton University. CS-211 Fall Pointers
|
|
- Neal Bennett
- 6 years ago
- Views:
Transcription
1 Pointers 1
2 Memory The act of keeping track of something over time Remembering is the concept of storing information A memory is no good unless you can retrieve that information In a computer, we remember information by writing bits (1/0) to memory We retrieve information by reading bits from memory
3 Memory is Different from Disk Storage All values in memory are forgotten when power is turned off Memory is really short-term memory Reading and Writing memory is much faster than reading or writing disk Memory is organized differently than disk
4 Computer Memory Organization Computers read and write memory in 1 byte (8 bit) chunks Think of memory as a big C vector of chars: char memory[2_142_240_768]; Like a vector, if we know the index of a byte of memory, we can either read or write to that byte: memory[1_684_501_289] = A ; printf( We stored %c\n,memory[1_684_501_289];
5 What is a pointer? Index into the memory vector Says I m not important what s important is over there Points AT or TO the real data in memory 5
6 Memory Vector of bytes Each byte has a value Each byte has an index or address Usually, the address is specified in hexadecimal xffff fffd xffff fffe xffff ffff x00 x04 x1c x02 x96 x00 x30 x04 6
7 Cheap Memory Between Moore s Law and brilliant OS parlor tricks, Virtual Memory is VERY cheap! Memory size depends on the size of the address Address Size Number of Bytes addressable 2 bytes (16 bits) 2 16 = 64K = 65,376 4 bytes (32 bits) 2 32 = 4G = 4,284,481,536 8 bytes (64 bits) 2 64 = 16EiB >1.8 x 10 19
8 C Variables A variable is a named piece of data Variables in C have A name (specified by the programmer) A value (may be unassigned/unknown) A location in memory (determined by the compiler) A type (size and interpretation) Variables must be declared before they are used! 8
9 Variable Concept Age Memory???????????????????????????????????????????????????????????????????????? First_Initial gpa 9
10 Variables In Memory Every variable starts at a specific location (address) in memory Type of variable tells how many bytes in memory First_Initial age x0034 ffe0 x004d 18c4 xffff fffd xffff fffe xffff ffff x00 x04 x1c x02 x54 x00 x00 x00 x39 x96 x00 x30 x04 10
11 Variable Address/Location Where is the value for the variable in memory? The address of First_Initial is x0034ffe0, which points to 0x54 = T First_Initial age x0034 ffe0 x004d 18c4 xffff fffd xffff fffe xffff ffff x00 x04 x1c x02 x54 x00 x00 x00 x39 x96 x00 x30 x04 11
12 Variable Address/Location Where is the value for the variable in memory? The address of age is x004d18c4, which points to the integer (4 byte) value 0x =57 age First_Initial x0034 ffe0 x004d 18c4 xffff fffd xffff fffe xffff ffff x00 x04 x1c x02 x54 x00 x00 x00 x39 x96 x00 x30 x04 12
13 Address Of (&) operator An ampersand (&) in front of a variable indicates address of char First_Initial= T ; int age=57; printf( First_Initial is in memory at %p\n,&first_initial); First_Initial is in memory at 0x34ffe0 First_Initial age x00 x04 x1c x02 x54 x00 x00 x00 x39 x96 x00 x30 x x0034 ffe0 x004d 18c4 xffff fffd xffff fffe xffff 13 ffff
14 Pointers in C Pointers are a special class of data type A variable may be declared as a pointer The size of a pointer is the size of an address The VALUE of a pointer is an address The TYPE of a pointer includes the type of value it is pointing to! pointer to character pointer to integer pointer to float pointer to struct date 14
15 Declaring a Pointer Same as normal variable but need asterisk (*) : pointer to char First_Initial= T ; char * fi_ptr; // pointer to char fi_ptr=&first_initial; // must be the address of char! printf( Value of fi_ptr is %p\n,fi_ptr); Value of fi_ptr is 0x34ffe0 First_Initial fi_ptr x00 x04 x1c x02 x54 x00 x34 xff xe0 x96 x00 x30 x x0034 ffe0 x004d 18c4 xffff fffd xffff fffe xffff 15 ffff
16 Pointers as References A pointer has a value an address in memory A pointer points to another value the data at that address Because we know what type the pointer is pointing to, we know how long the data at that address should be fi_ptr First_Initial 0x0034ffe0 x54 16
17 Using a Pointer Same as normal variable but need asterisk (*) : value at char First_Initial= T ; char * fi_ptr=&first_initial; // pointer to char printf( fi_ptr points at %c\n, (*fi_ptr) ); fi_ptr points at T First_Initial fi_ptr x00 x04 x1c x02 x54 x00 x34 xff xe0 x96 x00 x30 x x0034 ffe0 x004d 18c4 xffff fffd xffff fffe xffff 17 ffff
18 Terminology fi_ptr references First_Initial char First_Initial= T ; char * fi_ptr=&first_initial; // pointer to char printf( fi_ptr points at %c\n, (*fi_ptr) ); fi_ptr points at T *fi_ptr dereferences fi_ptr First_Initial fi_ptr x00 x04 x1c x02 x54 x00 x34 xff xe0 x96 x00 x30 x x0034 ffe0 x004d 18c4 xffff fffd xffff fffe xffff 18 ffff
19 Abuse of Symbols Ampersand (&) x & y // Bit-wise AND x && y // Logical AND &x // Address Of Asterix (*) x * y // multiplication int * x // pointer to (*x) // value at 19
20 Pointers as Aliases char First_Initial= T ; char * fi_ptr=&first_initial; (*fi_ptr)= A ; // Alias for First_Initial printf( First Initial: %c\n,first_initial); First_Initial *fi_ptr First Initial: A x54 20
21 Using NULL NULL is a special address whose value is 0x Beginning of Memory belongs to the operating system General programs can read at 0, but cannot write at 0 Therefore, we use NULL to indicate pointer to nothing Or pointer that we haven t set yet int *p=null; // p is a pointer to nothing (for now) p=&age; // Now p is a pointer to an integer 21
22 C Gotcha: Dereferencing a Null Pointer int *p=null; // p is a pointer to nothing (for now) int x=foo(); if (x>0) { p=&x; } (*p) = 5; Segmentation Violation when x<=0
23 Pointers point to Types int *x; // x is a pointer to an integer &z Type is: pointer to <type of z> (*myptr) Type is: type which myptr is pointing to e.g. int *myptr=&area; (*myptr)= a ; assigning char to int ASCII value of a is 0x61 area is now 0x
24 Void Pointers void * myptr; // myptr is a pointer to void myptr is a pointer, but I m not going to tell you what it points at Before you use myptr, you must cast it as a pointer to something printf( myptr points to %c\n,*(char *)myptr); void * used as a universal pointer a pointer to any type of data Programmer must know what type of data it s pointing at to cast correctly 24
25 The Power of Pointers Pointers are a Reference to what they are pointing at Rather than passing an entire <type> element, we can pass a pointer to that type. (Pointers are 8 bytes long.) Rather than passing an int, pass a pointer to an int Rather than passing a struct, pass a pointer to that struct Rather than passing an array, pass a pointer to that array If we pass a reference, then we can modify what we are pointing to, EVEN IF THE REFERENCE ITSELF IS A COPY!
26 Example of Pass by Reference int counter=0; void incr(int x) { x = x + 1; } incr(counter); printf( counter=%d\n,counter); int counter=0; void incr(int *x) { (*x) = (*x) + 1; } incr(&counter); printf( counter=%d\n,counter); counter=0 counter=1
27 Pointers to Structures It is so useful to pass structures by reference, we almost always do so C programmers got tired of coding: (*todayref).year Pointer to structure shorthand (*todayref).year is the same as. todayref->year
28 Pointers vs. Arrays 28
29 Array Values are Contiguous Right next to each other in memory int vec[6] int m [4][3]; vec[0] vec[1] vec[2] vec[3] vec[4] vec[5] m[0][0] m[0][1] m[0][2] m[1][0] m[1][1] m[1][2] m[2][0] m[2][1] m[2][2] m[3][0] m[3][1] m[3][2] 29
30 Example Array in Memory char name[8]= Thomas ; printf( name is at %p\n,&name[0]); name is at 0x34ffe0 name x00 x04 x1c x02 x54 x68 x6f x6d x61 x73 x00 x00 x96 x00 x30 x04 x0034 ffe x0034 ffe0 xffff fffd xffff fffe xffff 30 ffff
31 Pointer to Array in Memory char name[8]= Thomas ; char *np=&name[0]; printf( np is %p\n,np); np is at 0x34ffe0 name np x00 x04 x1c x02 x54 x68 x6f x6d x61 x73 x00 x00 x96 x00 x30 x04 x0034 ffe x0034 ffe0 xffff fffd xffff fffe xffff 31 ffff
32 What are we Pointing At? char name[8]= Thomas ; char *np=&name[0]; printf( np -> %c\n,(*np)); Question: Does np point to a single character? np -> T name np x00 x04 x1c x02 x54 x68 x6f x6d x61 x73 x00 x00 x96 x00 x30 x04 x0034 ffe x0034 ffe0 xffff fffd xffff fffe xffff 32 ffff
33 Pointers are just numbers char name[8]= Thomas ; char *np=&name[0]; printf( np=%x np+1=%x np+2=%x,np,np+1,np+2); np=x34ffeo np+1=x34ffe1 np+2=x34ffe2 name np x00 x04 x1c x02 x54 x68 x6f x6d x61 x73 x00 x00 x96 x00 x30 x04 x0034 ffe x0034 ffe0 xffff fffd xffff fffe xffff 33 ffff
34 What are we Pointing At? char name[8]= Thomas ; char *np=&name[0]; printf( np ->%c-%c-%c \n,*np,*(np+1),*(np+2)); np -> T-h-o name np Question: Does np point to a single character? Or an array of characters? x00 x04 x1c x02 x54 x68 x6f x6d x61 x73 x00 x00 x96 x00 x30 x04 x0034 ffe x0034 ffe0 xffff fffd xffff fffe xffff 34 ffff
35 What is a string? A string is just a vector of ASCII characters Followed by a null terminator a byte with the value 0x00 char str[14]= This a string ; { T, h, i, s,, a,, s, t, r, i, n, g, x00} Index ASCII T h i s a s t r i n g Hex x54 x68 x69 x73 x20 x61 x20 x73 x74 x72 x69 x6e x67 x00 35
36 What are we Pointing At? char name[8]= Thomas ; char *np=&name[0]; printf( np ->%s\n,np); np -> Thomas Question: Does np point to a single character? Or an array of characters? Or a string? name np x00 x04 x1c x02 x54 x68 x6f x6d x61 x73 x00 x00 x96 x00 x30 x04 x0034 ffe x0034 ffe0 xffff fffd xffff fffe xffff 36 ffff
37 A Pointer points to one or more elements of a specific type (Actually, zero or more, but who s counting) 37
38 Integer Vector in Memory int v[4]={11,12,13,14}; Vector v[0] v[1] v[2] v[3] Value x B x C x D x E Address x00 c x00 c x00 c0 00 1c x00 c int *vp=&v[0]; // vp=x00c printf( vp-> %d %d %d \n,*vp,*(vp+1),*(vp+2)); vp->
39 Pointer Arithmetic In C, if we add 1 to a pointer, that means, point to the next element If char * ptr points to a character, then ptr+1 points to the next character If int * iptr points to an integer, then iptr+1 points to the next integer If float *aptr[4] points to an array of 4 floats, then aptr+1 points to the next array of 4 floats. 39
40 Pointer Arithmetic In order to make this work, C uses funny arithmetic on pointers If char * ptr points to a character, printf( char@ %x, next char@ %x\n,ptr,ptr+1); prints: char@ 34ffe0 next 34ffe1 If int * iptr points to an integer, printf( %x next %x\n,iptr,iptr+1); prints: c00014 next c00018 If float *aptr[4] points to an array of 4 floats, printf( %x next %x\n,aptr,aptr+1); prints: a461a20 next a461a30 40
41 2D Arrays in Memory int m[4][3]={11,12,13,21,22,23,31,32,33,41,42,43}; m[0][0] m[0][1] m[0][2] m[1][0] m[1][1] m[1][2] m[2][0] m[2][1] m[2][2] m[3][0] m[3][1] m[3][2] c c c0 001c c c c c0 002c c c c c0 003c c int *mp=&m[0][0]; // mp=x00c printf( mp-> %d %d %d \n,*mp,*(mp+1),*(mp+2)); printf( row 3: %d %d %d\n,*(mp+6),*(mp+7),*(mp+8)); mp-> row 3:
42 What is an un-sub-scripted Array? char name[8]= Thomas ; printf( name[0] is at %p\n,&name[0]); printf( name value is %x\n,name); name[0] is at 0x23cb10 name value is 23cb10 42
43 array == &array[0] 43
44 What is a String? char name[8]= Thomas ; printf( name[0] is at %p\n,&name[0]); printf( name value is %x\n,name); printf( name string is %s\n,name); name[0] is at 0x23cb10 name value is 23cb10 name string is Thomas 44
45 A string is a pointer to one or more characters 45
46 What is a String? char *name= Thomas ; printf( name[0] is at %p\n,&name[0]); printf( name value is %x\n,name); printf( name string is %s\n,name); name[0] is at 0x23cb10 name value is 23cb10 name string is Thomas 46
47 In C, Pointers and Arrays are Virtually Interchangeable! 47
48 Array vs. Pointer Notation Array &array[0] array[i] Pointer array *(array+i) 48
49 strlen implementations Using array notation int strlen(char str[]) { } int i=0; while(str[i]!=x00) i++; return i; Using pointer notation int strlen(char *str) { } int i=0; while((*str)!=0) { } return i; i++; str++; 49
50 Pointers and Memory When you declare an int or array of ints, the compiler reserves memory for that int or array of ints in the invocation record When you declare a pointer to an int or array of ints, the compiler reserves memory for the POINTER, but does NOT reserve memory for the actual data being pointed to! Typical C bug: int char* name; strcpy(name, Tom Bartenstein ); Segmentation Violation
51 Initialized Pointers In some cases, it looks like pointers ARE reserving memory char * name = Tom Bartenstein ; In this case, the compiler is creating a LITERAL value in memory Possibly read only memory Possibly re-used with other instances of Tom Bartenstein in this code The name variable is a pointer to the LITERAL value May get segmentation violation if written to May get bizarre future errors if compiler re-used this literal
52 Resources Programming in C, Chapter 10 Wikepedia Pointers : C Pointer Tutorial : 52
Binghamton University. CS-211 Fall Pointers vs. Arrays
Pointers vs. Arrays 1 Array Values are Contiguous Right next to each other in memory int vec[6] int m [4][3]; vec[0] vec[1] vec[2] vec[3] vec[4] vec[5] m[0][0] m[0][1] m[0][2] m[1][0] m[1][1] m[1][2] m[2][0]
More informationBinghamton University. CS-211 Fall Pointers vs. Arrays
Pointers vs. Arrays 1 Pointers in C Pointers are a special data type The VALUE of a pointer is an address The TYPE of a pointer is pointer to pointer to character pointer to integer pointer
More informationBinghamton University. CS-211 Fall Pointers
Pointers 1 What is a pointer? Says I m not important what s important is over there Points AT or TO something else 2 Memory Array of bytes Each element has a value 0 1 2 3 xffff fffd xffff fffe xffff ffff
More informationC Arrays and Pointers
C Arrays and Pointers C Arrays List of contiguous values in memory Array Declaration: Type Name Count int vec[5]; Type: Type of each element Name: Identifier for the entire array Count: Number of elements
More informationMemory, Data, & Addressing II CSE 351 Spring
Memory, Data, & Addressing II CSE 351 Spring 2018 http://xkcd.com/138/ Review Questions 1) If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) a) 64
More informationMYcsvtu Notes LECTURE 34. POINTERS
LECTURE 34 POINTERS Pointer Variable Declarations and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain
More informationBinghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program
Syntax What the Compiler needs to understand your program 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line Possibly replacing it with other
More informationCS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018
CS 31: Intro to Systems Pointers and Memory Kevin Webb Swarthmore College October 2, 2018 Overview How to reference the location of a variable in memory Where variables are placed in memory How to make
More informationLecture 05 POINTERS 1
Lecture 05 POINTERS 1 Pointers Powerful, but difficult to master Simulate call-by-reference Close relationship with arrays and strings Pointer Variable vs. Normal Variable Normal variables contain a specific
More informationLecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings
Lecture 4: Outline I. Pointers A. Accessing data objects using pointers B. Type casting with pointers C. Difference with Java references D. Pointer pitfalls E. Use case II. Arrays A. Representation in
More informationComp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57
Comp 11 Lectures Mike Shah Tufts University June 26, 2017 Mike Shah (Tufts University) Comp 11 Lectures June 26, 2017 1 / 57 Please do not distribute or host these slides without prior permission. Mike
More informationArrays and Pointers in C. Alan L. Cox
Arrays and Pointers in C Alan L. Cox alc@rice.edu Objectives Be able to use arrays, pointers, and strings in C programs Be able to explain the representation of these data types at the machine level, including
More informationComputer Organization & Systems Exam I Example Questions
Computer Organization & Systems Exam I Example Questions 1. Pointer Question. Write a function char *circle(char *str) that receives a character pointer (which points to an array that is in standard C
More informationOperating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan
Operating Systems 2INC0 C course Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl) Containt Pointers Definition and Initilization Ponter Operators Pointer Arithmetic and Array Calling Functions by
More informationSyntax and Variables
Syntax and Variables What the Compiler needs to understand your program, and managing data 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line
More informationCS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016
CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers Kevin Webb Swarthmore College March 1, 2016 Overview Accessing things via an offset Arrays, Structs, Unions How complex structures are stored
More informationC Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations
55:017, Computers in Engineering C Pointers C Pointers Powerful C feature but challenging to understand Some uses of pointers include Call by reference parameter passage Dynamic data structures Data structures
More informationCS C Primer. Tyler Szepesi. January 16, 2013
January 16, 2013 Topics 1 Why C? 2 Data Types 3 Memory 4 Files 5 Endianness 6 Resources Why C? C is exteremely flexible and gives control to the programmer Allows users to break rigid rules, which are
More informationMemory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.
Memory and Addresses Memory is just a sequence of byte-sized storage devices. 1 The bytes are assigned numeric addresses, starting with zero, just like the indexing of the cells of an array. It is the
More informationCMSC202 Computer Science II for Majors
CMSC202 Computer Science II for Majors Lecture 04 Pointers Dr. Katherine Gibson Based on slides by Chris Marron at UMBC Last Class We Covered C++ Functions Parts of a function: Prototype Definition Call
More information3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type
Pointer Basics What is a pointer? pointer = memory address + type C Language III CMSC 313 Sections 01, 02 A pointer can contain the memory address of any variable type A primitive (int, char, float) An
More information10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013
Midterm Topic Review Pointer Basics C Language III CMSC 313 Sections 01, 02 1 What is a pointer? Why Pointers? Pointer Caution pointer = memory address + type A pointer can contain the memory address of
More informationHardware: Logical View
Hardware: Logical View CPU Memory Bus Disks Net USB Etc. 1 Hardware: Physical View USB I/O controller Storage connections CPU Memory 2 Hardware: 351 View (version 0) instructions? Memory CPU data CPU executes
More informationCS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C
CS2351 Data Structures Lecture 7: A Brief Review of Pointers in C 1 About this lecture Pointer is a useful object that allows us to access different places in our memory We will review the basic use of
More informationPointers. 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.
Pointers A pointer is a memory address of an object of a specified type, or it is a variable which keeps such an address. Pointer properties: P (pointer) 12316 12316 (address) Typed object A pointer value
More informationLecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?
Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static
More informationCMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013 TOPICS TODAY Reminder: MIDTERM EXAM on THURSDAY Pointer Basics Pointers & Arrays Pointers & Strings Pointers & Structs
More informationAdministrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6
CMSC 313 Introduction to Computer Systems Lecture 8 Pointers, cont. Alan Sussman als@cs.umd.edu Administrivia Project 2 posted, due October 6 public tests s posted Quiz on Wed. in discussion up to pointers
More informationPointers. Memory. void foo() { }//return
Pointers Pointers Every location in memory has a unique number assigned to it called it s address A pointer is a variable that holds a memory address A pointer can be used to store an object or variable
More informationTutorial 1: C-Language
Tutorial 1: C-Language Problem 1: Data Type What are the ranges of the following data types? int 32 bits 2 31..2 31-1 OR -2147483648..2147483647 (0..4294967295 if unsiged) in some machines int is same
More informationCS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016
CS 31: Intro to Systems Pointers and Memory Martin Gagne Swarthmore College February 16, 2016 So we declared a pointer How do we make it point to something? 1. Assign it the address of an existing variable
More informationCS 61c: Great Ideas in Computer Architecture
Arrays, Strings, and Some More Pointers June 24, 2014 Review of Last Lecture C Basics Variables, functioss, control flow, types, structs Only 0 and NULL evaluate to false Pointers hold addresses Address
More informationBinghamton University. CS-220 Spring Arrays in C
Arrays in C 1 One Dimensional Array (Vector) Ordered List of Values All of the same type Individual elements accessible by index Vector has a Size (Number of elements) 0 1 2 3 4 5 17.3 14.5 3.2 12.0 5.65
More informationDeclaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:
1 Programming in C Pointer Variable A variable that stores a memory address Allows C programs to simulate call-by-reference Allows a programmer to create and manipulate dynamic data structures Must be
More informationDECLARAING AND INITIALIZING POINTERS
DECLARAING AND INITIALIZING POINTERS Passing arguments Call by Address Introduction to Pointers Within the computer s memory, every stored data item occupies one or more contiguous memory cells (i.e.,
More informationComputer Systems Principles. C Pointers
Computer Systems Principles C Pointers 1 Learning Objectives Learn about floating point number Learn about typedef, enum, and union Learn and understand pointers 2 FLOATING POINT NUMBER 3 IEEE Floating
More informationPointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology
Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a
More informationCS113: Lecture 5. Topics: Pointers. Pointers and Activation Records
CS113: Lecture 5 Topics: Pointers Pointers and Activation Records 1 From Last Time: A Useless Function #include void get_age( int age ); int age; get_age( age ); printf( "Your age is: %d\n",
More informationCMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012 TOPICS TODAY Project 5 Pointer Basics Pointers and Arrays Pointers and Strings POINTER BASICS Java Reference In Java,
More informationPointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple
More informationCS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/
CS61C Machine Structures Lecture 4 C Structs & Memory Management 9/5/2007 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L04 C Structs (1) C String Standard Functions
More informationData Representation and Storage. Some definitions (in C)
Data Representation and Storage Learning Objectives Define the following terms (with respect to C): Object Declaration Definition Alias Fundamental type Derived type Use pointer arithmetic correctly Explain
More informationArrays, Strings, & Pointers
Arrays, Strings, & Pointers Alexander Nelson August 31, 2018 University of Arkansas - Department of Computer Science and Computer Engineering Arrays, Strings, & Pointers Arrays, Strings, & Pointers are
More informationIn Java we have the keyword null, which is the value of an uninitialized reference type
+ More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are
More informationCS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz
CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Bernhard Boser & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic
More informationLectures 13 & 14. memory management
Lectures 13 & 14 Linked lists and memory management Courtesy of Prof. Garcia (UCB) CS61C L05 Introduction to C (pt 3) (1) Review Pointers and arrays are virtually same C knows how to increment pointers
More informationMemory, Data, & Addressing II
Memory, Data, & Addressing II CSE 351 Autumn 2018 Instructor: Justin Hsia Teaching Assistants: Akshat Aggarwal An Wang Andrew Hu Brian Dai Britt Henderson James Shin Kevin Bi Kory Watson Riley Germundson
More informationAgenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture
CS 61C: Great Ideas in Computer Architecture Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c And in Conclusion, 2 Processor Control Datapath Components of a Computer PC Registers Arithmetic
More informationExercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!
Exercise 3 / Ch.7 Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways! Exercise 3 / Ch.8 Given the following array, write code
More informationLecture 8: Pointer Arithmetic (review) Endianness Functions and pointers
CSE 30: Computer Organization and Systems Programming Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers Diba Mirza University of California, San Diego 1 Q: Which of the assignment
More informationCS61C : Machine Structures
inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 C Memory Management Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia CS61C L05 C Structures, Memory Management (1) 2005-01-28 The
More informationChapter 6 - Pointers
Chapter 6 - Pointers Outline 1 Introduction 2 Pointer Variable Declarations and Initialization 3 Pointer Operators 4 Calling Functions by Reference 5 Using the const Qualifier with Pointers 6 Bubble Sort
More informationOutline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary
Pointers 1 2 Outline Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary 3 Computer Memory Revisited Computers store data in memory slots Each slot has an
More informationCS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz
CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic
More informationCourse organization. Course introduction ( Week 1)
Course organization Course introduction ( Week 1) Code editor: Emacs Part I: Introduction to C programming language (Week 2-9) Chapter 1: Overall Introduction (Week 1-3) Chapter 2: Types, operators and
More informationOutline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.
Outline Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples. 1 Introduction A pointer is a variable that contains a memory address Pointers
More informationC++ for Java Programmers
Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even
More informationCOMP26120: Pointers in C (2018/19) Lucas Cordeiro
COMP26120: Pointers in C (2018/19) Lucas Cordeiro lucas.cordeiro@manchester.ac.uk Organisation Lucas Cordeiro (Senior Lecturer, FM Group) lucas.cordeiro@manchester.ac.uk Office: 2.44 Office hours: 10-11
More informationAgenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011
CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture
More informationWHAT POINTERS REALLY ARE
POINTERS What is a pointer? The index of a book contains pointers. A URL (e.g., http://turing.ubishops.ca/home/cs318) is a pointer. A street address is a pointer. What is then a forwarding address? a pointer
More informationPIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics
PIC 10A Pointers, Arrays, and Dynamic Memory Allocation Ernest Ryu UCLA Mathematics Pointers A variable is stored somewhere in memory. The address-of operator & returns the memory address of the variable.
More informationIntroduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic
Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #33 Pointer Arithmetic In this video let me, so some cool stuff which is pointer arithmetic which helps you to
More informationCharacter Strings. String-copy Example
Character Strings No operations for string as a unit A string is just an array of char terminated by the null character \0 The null character makes it easy for programs to detect the end char s[] = "0123456789";
More informationC Pointers. CS 2060 Week 6. Prof. Jonathan Ventura
CS 2060 Week 6 1 Pointer Variables 2 Pass-by-reference 3 const pointers 4 Pointer arithmetic 5 sizeof 6 Arrays of pointers 7 Next Time Pointers The pointer is one of C s most powerful and important features.
More informationPOINTER & REFERENCE VARIABLES
Lecture 9 POINTER & REFERENCE VARIABLES Declaring data pointer variables Assignment operations with pointers Referring objects using pointer variables Generic pointers Operations with pointer variables
More informationPrinciples of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns
Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns What is an array? Pointers Memory issues The name of the array is actually a memory address. You can prove this by trying to print
More informationIntroduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument
Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #34 Function with pointer Argument (Refer Slide Time: 00:05) So, here is the stuff that we have seen about pointers.
More informationCS61C : Machine Structures
inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4 C Memory Management 2007-06-28 Scott Beamer, Instructor iphone Comes out Tomorrow CS61C L4 C Memory Management (1) www.apple.com/iphone
More informationLecture Notes on Memory Layout
Lecture Notes on Memory Layout 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 11 1 Introduction In order to understand how programs work, we can consider the functions,
More informationPointers in C/C++ 1 Memory Addresses 2
Pointers in C/C++ Contents 1 Memory Addresses 2 2 Pointers and Indirection 3 2.1 The & and * Operators.............................................. 4 2.2 A Comment on Types - Muy Importante!...................................
More informationCS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/
CS61C Machine Structures Lecture 5 C Structs & Memory Mangement 1/27/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L05 C Structs (1) C String Standard Functions
More informationCS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers
CS 61C: Great Ideas in Computer Architecture C Arrays, Strings, More Pointers Instructor: Justin Hsia 6/20/2012 Summer 2012 Lecture #3 1 Review of Last Lecture C Basics Variables, Functions, Flow Control,
More informationECE 15B COMPUTER ORGANIZATION
ECE 15B COMPUTER ORGANIZATION Lecture 13 Strings, Lists & Stacks Announcements HW #3 Due next Friday, May 15 at 5:00 PM in HFH Project #2 Due May 29 at 5:00 PM Project #3 Assigned next Thursday, May 19
More informationHere is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:
Pointer Manipulations Pointer Casts and Data Accesses Viewing Memory The contents of a block of memory may be viewed as a collection of hex nybbles indicating the contents of the byte in the memory region;
More informationLectures 5-6: Introduction to C
Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most
More informationSIGNED AND UNSIGNED SYSTEMS
EE 357 Unit 1 Fixed Point Systems and Arithmetic Learning Objectives Understand the size and systems used by the underlying HW when a variable is declared in a SW program Understand and be able to find
More informationCS31 Discussion 1E Spring 17 : week 08
CS31 Discussion 1E Spring 17 : week 08 TA: Bo-Jhang Ho bojhang@cs.ucla.edu Credit to former TA Chelsea Ju Project 5 - Map cipher to crib Approach 1: For each pair of positions, check two letters in cipher
More informationHW1 due Monday by 9:30am Assignment online, submission details to come
inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures Lecture #2 - C Pointers and Arrays Administrivia Buggy Start Lab schedule, lab machines, HW0 due tomorrow in lab 2009-06-24 HW1 due Monday by 9:30am
More informationCS 2461: Computer Architecture I
Next: Pointers, Arrays, Structs... : Computer Architecture I The real fun stuff in C.. Pointers and Arrays Read Chapters 16, 18 of text Functions, Arrays, Pointers Dynamic data structures Allocating space
More informationC Pointers. 7.2 Pointer Variable Definitions and Initialization
1 7 C Pointers 7.2 Pointer Variable Definitions and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain
More informationThe output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {
Memory A bit is a binary digit, either 0 or 1. A byte is eight bits, and can thus represent 256 unique values, such as 00000000 and 10010110. Computer scientists often think in terms of hexadecimal, rather
More informationa data type is Types
Pointers Class 2 a data type is Types Types a data type is a set of values a set of operations defined on those values in C++ (and most languages) there are two flavors of types primitive or fundamental
More informationPointer Basics. Lecture 13 COP 3014 Spring March 28, 2018
Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory
More informationC Pointers. 6th April 2017 Giulio Picierro
C Pointers 6th April 07 Giulio Picierro Functions Return type Function name Arguments list Function body int sum(int a, int b) { return a + b; } Return statement (return keyword
More informationVariables and literals
Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of
More informationLecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists
Lecture 5: Outline I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists Multidimensional arrays: 2D Declaration int a[3][4]; /*Conceptually 2D matrix
More informationProgramming in C - Part 2
Programming in C - Part 2 CPSC 457 Mohammad Reza Zakerinasab May 11, 2016 These slides are forked from slides created by Mike Clark Where to find these slides and related source code? http://goo.gl/k1qixb
More informationC BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.
C BOOTCAMP DAY 2 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Pointers 2 Pointers Pointers are an address in memory Includes variable addresses,
More informationCS61, Fall 2012 Midterm Review Section
CS61, Fall 2012 Midterm Review Section (10/16/2012) Q1: Hexadecimal and Binary Notation - Solve the following equations and put your answers in hex, decimal and binary. Hexadecimal Decimal Binary 15 +
More informationFirst of all, it is a variable, just like other variables you studied
Pointers: Basics What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the address (rather than the value)
More informationMalloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016
Malloc Lab & Midterm Solutions Recitation 11: Tuesday: 11/08/2016 Malloc 2 Important Notes about Malloc Lab Malloc lab has been updated from previous years Supports a full 64 bit address space rather than
More informationINTRODUCTION 1 AND REVIEW
INTRODUTION 1 AND REVIEW hapter SYS-ED/ OMPUTER EDUATION TEHNIQUES, IN. Programming: Advanced Objectives You will learn: Program structure. Program statements. Datatypes. Pointers. Arrays. Structures.
More informationArrays and Memory Management
Arrays and Memory Management 1 Pointing to Different Size Objects Modern machines are byte-addressable Hardware s memory composed of 8-bit storage cells, each has a unique address A C pointer is just abstracted
More informationCS360 Midterm 1 - February 21, James S. Plank. Put all answers on the answer sheet. In all of these questions, please assume the following:
CS360 Midterm 1 - February 21, 2017 - James S. Plank Put all answers on the answer sheet. In all of these questions, please assume the following: Pointers and longs are 4 bytes. The machine is little endian
More informationECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers
ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers School of Electrical and Computer Engineering Cornell University revision: 2017-09-23-11-06 1 Pointer Basics 2 2 Call by Value vs. Call
More informationPointers. 10/5/07 Pointers 1
Pointers 10/5/07 Pointers 1 10/5/07 Pointers 2 Variables Essentially, the computer's memory is made up of bytes. Each byte has an address, associated with it. 10/5/07 Pointers 3 Variable For example 1:#include
More informationChapter 11 Introduction to Programming in C
Chapter 11 Introduction to Programming in C C: A High-Level Language Gives symbolic names to values don t need to know which register or memory location Provides abstraction of underlying hardware operations
More informationIntroduction to Scientific Computing and Problem Solving
Introduction to Scientific Computing and Problem Solving Lecture #22 Pointers CS4 - Introduction to Scientific Computing and Problem Solving 2010-22.0 Announcements HW8 due tomorrow at 2:30pm What s left:
More information211: Computer Architecture Summer 2016
211: Computer Architecture Summer 2016 Liu Liu Topic: C Programming Data Representation I/O: - (example) cprintf.c Memory: - memory address - stack / heap / constant space - basic data layout Pointer:
More informationCS61C : Machine Structures
inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 C Memory Management Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia CS 61C L04 C Structures, Memory Management (1) 2004-09-10 Barry
More information