CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Pointers Applications
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Pointers CSE 2451 Rong Shi.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Stack and Heap Memory Stack resident variables include:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Pointers OVERVIEW.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Object-Oriented Programming in C++
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
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:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Pointers *, &, array similarities, functions, sizeof.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
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
CSE 220 – C Programming malloc, calloc, realloc.
Dynamic Allocation in C
Memory allocation & parameter passing
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
ENEE150 Discussion 07 Section 0101 Adam Wang.
Lecture 6 C++ Programming
Dynamic Memory Allocation
Memory Allocation CS 217.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Pointers and Arrays Beyond Chapter 16
Presentation transcript:

CS50 SECTION: WEEK 4 Kenny Yu

Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future problem set feedback before Thursday.  Quiz 0 on Wed 10/12; details announced in lecture this week  Previous year’s quizzes are available:  Section resources:  Section feedback poll:   What do you want to review in section for Quiz 0 next week? 

Agenda 1. RAM 2. Pointers 1. Declaring Pointers 2. Address Operator 3. Dereferencing Pointers 4. Pointer Assignment 5. WARNING 3. Arrays Revisited 4. Heap and Stack 5. Malloc

RAM (Random Access Memory)  “Active” memory  Imagine RAM as being a very very big array (on a 4GB RAM machine, you have 2^32 buckets)  Each address represents one byte in memory. RAM (hex) … 0A05FF00 Address (dec): …

RAM (Random Access Memory)  Whenever you declare a variable, space is allocated for it in RAM. The location of the variable in RAM is the variable’s address. RAM (hex) … 6105FF00 Address: … char c = ‘a’; c Address of c is 508.

Pointers Pointers are variables that store addresses. Pointer (value = address of Variable) Pointer (value = address of Variable) Variable (value = 5) Variable (value = 5) RAM (Random Access Memory)

Pointer Syntax > Declaring Pointers  Declaring a pointer:  var_type* pointer_name;  Example:  int* ptr; // ptr has type int*; // ptr points to int types

Pointer Syntax > Address Operator  The & operator returns the address of a variable (the memory location of the variable in RAM, it is actually just an integer)  Example:  int x = 5; // declare and assign x’s value to 5 int* ptr; // declare a pointer to int types ptr = &x; // ptr’s value is the address of x (i.e. when ptr is dereferenced, it will return the value of x, which is 5)

Pointer Syntax > Dereferencing  When we dereference a pointer, we retrieve the value of the variable at the address stored by the pointer.  We dereference a pointer by using the * operator  Example: int x = 5; // declare and assign x to 5 int* ptr; // declare a pointer to int types ptr = &x; // ptr contains the address of x int y = *ptr; // declare y and assign it to the value pointed by ptr. Now y and x are both 5.

Pointer Syntax > Assignment  We can also change the value at the address pointed to by a pointer, using the * operator on the left side of an assignment  Example:  int x = 5; // declare and assign x to 5 int* ptr; // declare a pointer to int types ptr = &x; // ptr contains the address of x *ptr = 6; // change the value pointed to by ptr Now x is equal to 6, and if we dereference ptr, that will also return 6.

Putting it all together int x = 5; // declare and assign x to 5 RAM (Random Access Memory) x Type: int Address: Value: 5 x Type: int Address: Value: 5

Putting it all together int x = 5; // declare and assign x to 5 int *ptr; // declare a pointer to int types x Type: int Address: Value: 5 x Type: int Address: Value: 5 RAM (Random Access Memory) ptr Type: int * Address: Value: GARBAGE ptr Type: int * Address: Value: GARBAGE

Putting it all together int x = 5; // declare and assign x to 5 int *ptr; // declare a pointer to int types ptr = &x; // ptr contains the address of x x Type: int Address: Value: 5 x Type: int Address: Value: 5 RAM (Random Access Memory) ptr Type: int * Address: Value: ptr Type: int * Address: Value: 56789

Putting it all together int x = 5; // declare and assign x to 5 int *ptr; // declare a pointer to int types ptr = &x; // ptr contains the address of x int y = *ptr; // declare y and dereference ptr. We have two copies of 5. x Type: int Address: Value: 5 x Type: int Address: Value: 5 RAM (Random Access Memory) ptr Type: int * Address: Value: ptr Type: int * Address: Value: y Type: int Address: Value: 5 y Type: int Address: Value: 5

Putting it all together int x = 5; // declare and assign x to 5 int *ptr; // declare a pointer to int types ptr = &x; // ptr contains the address of x int y = *ptr; // declare y and dereference ptr. We have two copies of 6. *ptr = 6; // change the value pointed to by ptr. x is now 6, y is still 5 x Type: int Address: Value: 6 x Type: int Address: Value: 6 RAM (Random Access Memory) ptr Type: int * Address: Value: ptr Type: int * Address: Value: y Type: int Address: Value: 5 y Type: int Address: Value: 5

A note on style  I’ve been typing this:  int* ptr;  But most people program like this:  int *ptr;  These are exactly the same! Just remember that you should interpret both of these as  (int *) ptr; // ptr’s type is int* ( a pointer to an int )

WARNING > * syntax Do not confuse the * syntax!!!!!! 1. Declarations: * is part of the type of the pointer int* ptr; // ptr has type (int *). This is equivalent to: int* ptr; 2. Dereferencing: retrieves the value at the address that the pointer points to (i.e. follow the arrow) int x = 5; ptr = &x; // ptr points to x int y = *ptr; // dereference operator, y == 5 now 3. Assignment: changes the value at the address that the pointer points to *ptr = 6; // change the value of x; if ptr is dereferenced now, it will return 6. y is still 5

WARNING > Uninitialized Pointers int *ptr; // ptr not yet initialized // ptr = &x; initializes it. GARBAGE? Huh? Pointers that have not yet been assigned an address (i.e. ptr = &x ) will point to a random place in RAM. Dereferencing an uninitialized pointer is a BIG NONO. The operating system will not let you read/write to memory that you are not allowed to touch; this leads to Segmentation Faults. TIP: Always initialize a pointer before dereferencing it ptr Type: int * Address: Value: GARBAGE ptr Type: int * Address: Value: GARBAGE

NULL  C has a sentinel address called NULL which is memory address 0  Used to signal to programmers that the pointer is currently not set Useful in building linkedlists, binary search trees, etc.  Note: Pointers are NOT automatically initialized to the value NULL  WARNING Dereferencing a pointer pointing to NULL leads to a segmentation fault

NULL int *ptr = NULL; // declare ptr and set it to NULL... if (!ptr) { // !ptr evaluates to true if ptr == 0 (NULL) printf(“ptr is pointing to nothing!\n”); int pointee = *ptr; // try to dereference ptr: THIS WILL // CAUSE A SEGMENTATION FAULT } else { printf(“ptr is pointing to address %d\n”,ptr); int pointee = *ptr; printf(“the value pointed to by ptr is %d\n,pointee); }

NULL vs. ‘\0’  NULL is a sentinel memory address; NULL == 0  Used to indicate a pointer is pointing to nothing  ‘\0’ is a character who’s ASCII value is 0  Used to indicate the end of a string NULL is an ADDRESS (a fake one) ‘\0’ is a VALUE (a character)

Pointer Practice abcpapbpc a = b * c; a *= c; b = *pa; pc = pa; *pb = b * c; c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;

Pointer Practice abcpapbpc a = b * c;20 45&a&b&c a *= c; b = *pa; pc = pa; *pb = b * c; c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;

Pointer Practice abcpapbpc a = b * c;20 45&a&b&c a *= c; 10045&a&b&c b = *pa; pc = pa; *pb = b * c; c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;

Pointer Practice abcpapbpc a = b * c;20 45&a&b&c a *= c; 10045&a&b&c b = *pa; 100 5&a&b&c pc = pa; *pb = b * c; c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;

Pointer Practice abcpapbpc a = b * c;20 45&a&b&c a *= c; 10045&a&b&c b = *pa; 100 5&a&b&c pc = pa; 100 5&a&b&a *pb = b * c; c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;

Pointer Practice abcpapbpc a = b * c;20 45&a&b&c a *= c; 10045&a&b&c b = *pa; 100 5&a&b&c pc = pa; 100 5&a&b&a *pb = b * c; &a&b&a c = (*pa) * (*pc); *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;

Pointer Practice abcpapbpc a = b * c;20 45&a&b&c a *= c; 10045&a&b&c b = *pa; 100 5&a&b&c pc = pa; 100 5&a&b&a *pb = b * c; &a&b&a c = (*pa) * (*pc); &a&b&a *pc = a * (*pb); int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;

Pointer Practice abcpapbpc a = b * c; 2045&a&b&c a *= c; 10045&a&b&c b = *pa; 100 5&a&b&c pc = pa; 100 5&a&b&a *pb = b * c; &a&b&a c = (*pa) * (*pc); &a&b&a *pc = a * (*pb); &a&b&a int a = 3, b = 4, c = 5; int *pa = &a, *pb = &b, *pc = &c;

Pointer Arithmetic int x = 5; int *ptr = &x; int y = *ptr; // y gets 5 int z = *(ptr + 1); // z gets the stuff (?) in memory at address &x + 1 * sizeof(int) Could potentially segfault! So don’t use pointer arithmetic unless you know something you allocated is there.

Pointer Arithmetic int *ptr = &x; What are these addresses? ptr + 2 == ptr + 0 == ptr + 13 ==

Pointer Arithmetic int *ptr = &x; What are these addresses? ptr + 2 == &x + 2 * sizeof(int) ptr + 0 == &x ptr + 13 == &x + 13 * sizeof(int) We need to multiply the offset by the size of the type the pointer is pointing to (for int *, we multiply by sizeof(int) )

Arrays are pointers  int scores[3] = {100, 99, 98};  score is actually a pointer to it’s first element, score[0] == 100  int second = scores[2];  EXACTLY THE SAME AS: int second = *(scores + 2);  int zeroth = scores[0];  EXACTLY THE SAME AS: int zeroth = *scores;

Why does string == char * ?  h h In cs50.h:... /* * Our own data type for string variables */ typedef char *string;...

Strings  We alias string to be really a char *, a pointer to a character  Arrays are really just pointers  So a string is really just a pointer to the first character in a sequence of contiguous characters, terminated with ‘\0’

Strings char *s = GetString(); // user enters “cat” GetString() returns a pointer to the first character of a char array in memory. Type: char Address: Value: ‘c’ Type: char Address: Value: ‘c’ RAM (Random Access Memory) s Type: char * Address: Value: s Type: char * Address: Value: Type: char Address: Value: ‘a’ Type: char Address: Value: ‘a’ Type: char Address: Value: ‘t’ Type: char Address: Value: ‘t’ Type: char Address: Value: ‘\0’ Type: char Address: Value: ‘\0’

Strings QUESTION: But wait, where is the space being allocated for the string?

Heap and Stack Heap Contains local variables. Function calls create new ‘frames’ on the stack. Memory belonging to process. Stack Lower Memory Addresses Higher Memory Addresses Contains global variables. Dynamically allocated memory reserved on heap.

Heap and Stack Heap In main: // user enters “cat” char *s = GetString(); Memory belonging to process. Stack Lower Memory Addresses Higher Memory Addresses Space is dynamically allocated for “cat” in the heap s s ‘c’ ‘a’ ‘t’ ‘\0 ’

Heap vs. Stack  Heap memory persists  Memory allocated in the heap by one function will still be there after the function return (unless the memory is freed)  Stack memory does not persist  Memory allocated in the stack (with stack frames, e.g. local variables) by a function will not persist after the function returns

Dynamically allocating memory  malloc(int size) : memory allocation  On the terminal, type “man 3 malloc”  takes an integer parameter and returns a pointer to an array in the heap of the requested size, or returns NULL to signal a failure occurred

This won’t work int arrsize = GetInt(); int arr[arrsize]; GCC must know at compile time the size of arrays, but arrsize is computed at runtime through user input. => Can’t allocate variable-sized arrays!

A fix: int arrsize = GetInt(); int *arr = (int *) malloc(sizeof(int) * arrsize); malloc returns a pointer to an int array of size arrsize. This array will be located in the heap. => Can allocate memory dynamically with malloc!

Should make sure malloc worked int arrsize = GetInt(); int *arr = (int *) malloc(sizeof(int) * arrsize); if (arr == NULL) { printf(“malloc failed! No memory left!\n”); } Malloc will return NULL if it fails to allocate the requested amount of memory.

Malloc’ed memory must be freed!  ALL memory allocated by malloc MUST be freed  Otherwise this will lead to memory leaks – VERY BAD!  Have you ever wondered why your computer seems to slow down even if you have no programs open?  Use the free(ptr) function; ptr must be a pointer returned by a malloc call! int arrsize = GetInt(); int *arr = (int *) malloc(sizeof(int) * arrsize); if (arr == NULL) return -1; // do some stuff free(arr);

GetString()  GetString() calls malloc to allocate the memory in the heap for the string  So every time you called GetString() and didn’t free the memory, you created a memory leak!!!!  From now on, you must free all your strings. char *s = GetString(); // do stuff free(s);

Swap int main(void) { int x = 5; int y = 6; swap(x,y); printf(“x: %d, y: %d\n”, x, y); // what are x and y after swap? } void swap(int a, int b) { int temp = a; a = b; b = temp; }

Swap int main(void) { int x = 5; int y = 6; swap(x,y); printf(“x: %d, y: %d\n”, x, y); // x = 5, y = 6, WHAT??! } void swap(int a, int b) { int temp = a; a = b; b = temp; }

Swap: Stack frames int main(void) { int x = 5; int y = 6; swap(x,y); printf(“x: %d, y: %d\n”, x, y); } void swap(int a, int b) { int temp = a; a = b; b = temp; } main swap x = 5 y = 6 a = 5 b = 6 Swap has it’s own copies of 5 and 6!! You cannot change x and y inside swap.

Swap: Stack frames int main(void) { int x = 5; int y = 6; swap(x,y); printf(“x: %d, y: %d\n”, x, y); } void swap(int a, int b) { int temp = a; a = b; b = temp; } main swap x = 5 y = 6 a = 5 b = 6 Swap has it’s own copies of 5 and 6!! You cannot change x and y inside swap. This is an example of call-by-value: the values of the parameters are copied from the caller into the callee.

Swap: fix int main(void) { int x = 5; int y = 6; swap(&x,&y); printf(“x: %d, y: %d\n”, x, y); // x = 6, y = 5 } void swap(int *a, int *b) { // a and b are pointers int temp = *a; // temp gets the old value at the address in a *a = *b; // assign the address in a to the value at the // address in b *b = temp; // assign the address in b to temp }

Swap: Stack frames int main(void) { int x = 5; int y = 6; swap(&x,&y); printf(“x: %d, y: %d\n”, x, y); } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } main swap x = 5 y = 6 a = &x b = &y

Swap: Stack frames int main(void) { int x = 5; int y = 6; swap(&x,&y); printf(“x: %d, y: %d\n”, x, y); } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } main swap x = 5 y = 6 a a b b temp = 5 a = &x b = &y

Swap: Stack frames int main(void) { int x = 5; int y = 6; swap(&x,&y); printf(“x: %d, y: %d\n”, x, y); } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } main swap x = 6 y = 6 a a b b temp = 5 a = &x b = &y

Swap: Stack frames int main(void) { int x = 5; int y = 6; swap(&x,&y); printf(“x: %d, y: %d\n”, x, y); } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } main swap x = 6 y = 5 a a b b temp = 5 a = &x b = &y

int main(void) { int x = 5; int y = 6; swap(&x,&y); printf(“x: %d, y: %d\n”, x, y); } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } Swap: Stack frames main swap x = 6 y = 5 temp = 5 b = &ya = &x

int main(void) { int x = 5; int y = 6; swap(&x,&y); printf(“x: %d, y: %d\n”, x, y); } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } Swap: Stack frames main swap x = 6 y = 5 temp = 5 This is a call-by-reference: instead of passing in a value, we pass a pointer so that the callee can read/write to the given address in the pointer. b = &ya = &x

So why use pointers?  Pointers allow us to dynamically allocate memory  With malloc  Must be freed  Can allocate variables in heap to be preserved between function calls  Pointers allow us to call by reference  Allows us to pass pointers into arguments and affect variables through multiple functions  Swap function

Fun fun fun  eek4.c