Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Similar documents
Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

In Java we have the keyword null, which is the value of an uninitialized reference type

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CSC 1600 Memory Layout for Unix Processes"

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

C Tutorial. Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Dynamic Memory Allocation

EL2310 Scientific Programming

SYSC 2006 C Winter 2012

Intermediate Programming, Spring 2017*

CS61C : Machine Structures

CSC C69: OPERATING SYSTEMS

CS 11 C track: lecture 5

Quick review pointer basics (KR ch )

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

Dynamic memory allocation (malloc)

Section 2: Processes

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

o Code, executable, and process o Main memory vs. virtual memory

CS61C : Machine Structures

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Limitations of the stack

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet


Key C Topics: Tutorial Pointers, Dynamic Memory allocation, Valgrind and Makefile CS370

ME964 High Performance Computing for Engineering Applications

Memory Allocation. General Questions

C Tutorial Pointers, Dynamic Memory allocation, Makefile

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

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS 222: Pointers and Manual Memory Management

Arrays and Memory Management

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

Dynamic Allocation in C

Arrays, Pointers and Memory Management

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Welcome! COMP s1. Programming Fundamentals

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

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

Programming. Pointers, Multi-dimensional Arrays and Memory Management

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Lecture 8 Dynamic Memory Allocation

Recitation 2/18/2012

[0569] p 0318 garbage

CS61, Fall 2012 Section 2 Notes

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Understanding Pointers

CS24 Week 2 Lecture 1

Arrays in C. Data and File Structures Laboratory. DFS Lab (ISI) Arrays in C 1 / 1

Quiz 0 Review Session. October 13th, 2014

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Page 1. Today. Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right? Compiler requirements CPP Volatile

Class Information ANNOUCEMENTS

Week 6 Part 1. Kyle Dewey. Monday, July 30, 12

Heap Arrays and Linked Lists. Steven R. Bagley

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

PRINCIPLES OF OPERATING SYSTEMS

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

Arrays and Pointers in C. Alan L. Cox

Memory (Stack and Heap)

Dynamic Data Structures. CSCI 112: Programming in C

EL2310 Scientific Programming

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

CS201- Introduction to Programming Current Quizzes

Pointers, Dynamic Data, and Reference Types

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Systems Programming and Computer Architecture ( )

Dynamic Allocation in C

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Pointers, Arrays, Memory: AKA the cause of those Segfaults

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

Dynamic Memory Allocation and Command-line Arguments

CSE 333 Lecture 4 - malloc, free, struct, typedef

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!

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

Section Notes - Week 1 (9/17)

ECE 15B COMPUTER ORGANIZATION

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

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

Signals, Instruments, and Systems W3. C Programming & Memory Management in C

CS61C : Machine Structures

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Memory Analysis tools

LAB 6 (2017 June 22/27) Array of pointers. Dynamic memory allocation.

The 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 {

Important From Last Time

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Dynamic Memory Allocation and Linked Lists

Fundamental of Programming (C)

EL2310 Scientific Programming

Transcription:

Announcements assign due tonight No late submissions Labs start this week Very helpful for assign1

Goals for Today Pointer operators Allocating memory in the heap malloc and free Arrays and pointer arithmetic

Pointers In C++, from 16B/X Linked lists, trees, graphs Shared data Also in C Arrays, strings Pass by reference

Pointer Operators int i = 42, j = 17; int *p = &i; int *q = &j; & -- address-of & on int gives int * Address Memory 94 916 9 98 94 17 9 42 q p j i

Pointer Operators int i = 42, j = 17; int *p = &i; int *q = &j; printf( %d\n, *p); // 42 & -- address-of & on int gives int * * -- dereference * on int * gives int Address Memory 94 916 9 98 94 17 9 42 q p j i

Exercise int i = 42, j = 17; int *p = &i; int *q = &j; // What do these lines do? p = q; // (1) *p = *q; // (2) *p = q; // (3) p = *q; // (4) Address Memory 94 916 9 98 94 17 9 42 q p j i

Exercise (1a) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 9 p // What do these lines do? p = q; // (1) 98 94 17 j 9 42 i

Exercise (1b) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 94 p // What do these lines do? p = q; // (1) 98 94 17 j 9 42 i p and q are aliases

Exercise (2a) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 9 p // What do these lines do? *p = *q; // (2) 98 94 17 j 9 42 i

Exercise (2b) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 9 p // What do these lines do? *p = *q; // (2) 98 94 17 j 9 17 i

Exercise (3a) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 9 p // What do these lines do? *p = q; // (3) 98 94 17 j 9 42 i

Exercise (3b) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 9 p // What do these lines do? *p = q; // (3) 98 94 17 j 9 94 i warning: assignment makes integer from pointer without a cast

Exercise (4a) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 9 p // What do these lines do? p = *q; // (4) 98 94 17 j 9 42 i

Exercise (4b) Address Memory int i = 42, j = 17; int *p = &i; int *q = &j; 916 94 q 17 p // What do these lines do? p = *q; // (4) 98 94 17 j 9 42 i warning: assignment makes pointer from integer without a cast

Exercise (4c) int i = 42, j = 17; int *p = &i; int *q = &j; // What do these lines do? p = *q; // (4) printf( %d\n, *p); // seg fault Address Memory 94 916 17 98 94 17 9 42 q p j i

Exercise int i = 42, j = 17; int *p = &i; int *q = &j; p = q; // (1) *p = *q; // (2) *p = q; // (3) p = *q; // (4) Address Memory 94 916 9 98 94 17 9 42 q p j i

Uninitialized Pointers int i; int *p; // How bad are these? printf( %d\n, i); printf( %d\n, *p); Address Memory???? 94 9???? p i

Uninitialized Pointers int i; int *p; // How bad are these? printf( %d\n, i); // unpredictable, no crash printf( %d\n, *p); Address Memory???? 94 9???? p i

Uninitialized Pointers int i; int *p; // How bad are these? printf( %d\n, i); // unpredictable, no crash printf( %d\n, *p); // probably crash Address Memory???? 94 9???? p i

NULL int *p = NULL; printf( %d\n, *p); // seg fault if (p == NULL) { // ok } Address 9 Memory p

Pointee Types int i = 5; double d = 2.5; int *ip = &i; double *dp = &d; dp = ip; warning: assignment from incompatible pointer type

Allocating Memory So far: the stack Lasts until function returns Automatically cleaned up Now: the heap Recall new from C++

malloc malloc(size) Argument is number of bytes needed Returns pointer to space i nheap Memory stays around until freed

malloc malloc(size) Argument is number of bytes needed Returns pointer to space i nheap Memory stays around until freed sizeof(type) Number of bytes a given type needs

malloc and free malloc(size) Argument is number of bytes needed Returns pointer to space i nheap Memory stays around until freed sizeof(type) Number of bytes a given type needs free(ptr) Mark memory as no longer in use

Using malloc and free int *p = malloc(sizeof(int)); *p = 17; Address 9 Memory 2 p 2 17

Using malloc and free int *p = malloc(sizeof(int)); *p = 17; free(p); Address 9 Memory 2 p 2

Using malloc and free int *p = malloc(sizeof(int)); *p = 17; free(p); // BUGGY printf( %d\n, *p); *p = 35; free(p); Address 9 Memory 2 p 2

Using malloc and free int *p = malloc(sizeof(int)); *p = 17; int *q = p; free(p); free(q); // BUGGY Address 98 9 Memory 2 q 2 p 2

Arrays? Address int *p = malloc(4 * sizeof(int)); 9 Memory 2 p 2

Arrays? Address int *p = malloc(4 * sizeof(int)); // What if we could do p[] = 2; p[1] = 4; 9 p[2] = 6; p[3] = 8; Memory 2 p 2

Arrays! Address int *p = malloc(4 * sizeof(int)); p[] = 2; p[1] = 4; p[2] = 6; p[3] = 8; 9 Memory 2 p 2 8 6 4 2

Arrays Address int *p = malloc(4 * sizeof(int)); p[] = 2; p[1] = 4; p[2] = 6; p[3] = 8; 9 Memory 2 p 2 212 8 2 28 6 2 24 4 2 2

Pointer Arithmetic Address int *p = malloc(4 * sizeof(int)); p[] = 2; p[1] = 4; p[2] = 6; p[3] = 8; int *q = p + 2; Memory 98 2 9 2 212 8 2 28 6 2 24 4 2 2 q p

Pointer Arithmetic Address Memory int *p = malloc(4 * sizeof(int)); 28 q p[] = 2; p[1] = 4; 98 p[2] = 6; p[3] = 8; 2 p int *q = p + 2; printf( %d\n, *q); // 6 9 2 212 8 2 28 6 2 24 4 2 2

Pointer Arithmetic Address Memory int *p = malloc(4 * sizeof(int)); 28 q p[] = 2; p[1] = 4; 98 p[2] = 6; p[3] = 8; 2 p int *q = p + 2; printf( %d\n, *q); // 6 // p[2] == *(p + 2) 9 2 212 8 2 28 6 2 24 4 2 2

Pointer Arithmetic Address Memory int *p = malloc(4 * sizeof(int)); 28 q p[] = 2; p[1] = 4; 98 p[2] = 6; p[3] = 8; 2 p int *q = p + 2; printf( %d\n, *q); // 6 9 2 212 8 // p[2] == *(p + 2) // p[] == *(p + ) == *p 2 28 6 2 24 4 2 2

Pointer Arithmetic Address Memory int *p = malloc(4 * sizeof(int)); 28 q p[] = 2; p[1] = 4; 98 p[2] = 6; p[3] = 8; 2 p int *q = p + 2; printf( %d\n, *q); // 6 9 2 212 8 // p[2] == *(p + 2) // p[] == *(p + ) == *p 2 28 6 // &p[2] == p + 2 2 24 4 // &p[] == p 2 2

Subarrays Address Memory int *p = malloc(4 * sizeof(int)); 28 q p[] = 2; p[1] = 4; 98 p[2] = 6; p[3] = 8; 2 p int *q = p + 2; printf( %d\n, q[1]); // 8 9 2 212 8 2 28 6 2 24 4 2 2

Stack Arrays int arr[4]; arr[] = 5; arr[1] = 1; arr[2] = 15; arr[3] = 2; // arr == &arr[] Address Memory 912 2 98 94 15 1 9 5 arr 2

Strings Recall char * from Friday Called C string Pointer to character(s) How does printf know when to stop printing? \ (null terminator) at end char *str = hello ; str 1 h e l l o \

Summary Pointer operators Allocating memory in the heap malloc and free Arrays and pointer arithmetic Lab: Strings Next time: More pointer uses Pass by reference, stack vs. heap