CS 110 Computer Architecture. Lecture 4: Introduction to C, Part III. Instructor: Sören Schwertfeger.

Similar documents
CS 61C: Great Ideas in Computer Architecture Introduction to C, Part III

CS 61C: Great Ideas in Computer Architecture C Memory Management, Usage Models

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part III

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part III

Review of Last Lecture

Review. You Are Here! Recap: The Stack 11/8/12. CS 61C: Great Ideas in Computer Architecture C Memory Management. Recap: C Memory Management

Arrays and Memory Management

CS 61C: Great Ideas in Computer Architecture. Lecture 4: Memory Management. Bernhard Boser & Randy Katz

Agenda. Recap: C Memory Management. Agenda. Recap: Where are Variables Allocated? Recap: The Stack 11/15/10

Agenda. Pointer Arithmetic. Pointer Arithmetic pointer + number, pointer number 6/23/2011. Pointer Arithmetic: Peer Instruction Question

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

Instructor: Randy H. Katz Fall Lecture #22. Warehouse Scale Computer

An example Request R1 for 100 bytes Request R2 for 1 byte Memory from R1 is freed Request R3 for 50 bytes What if R3 was a request for 101 bytes?

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

More On Memory (mis)-management

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CS 110 Computer Architecture Lecture 5: Intro to Assembly Language, MIPS Intro

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

CS 61C: Great Ideas in Computer Architecture. Memory Management and Usage

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II

Arrays and Pointers in C. Alan L. Cox

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

CS 61C: Great Ideas in Computer Architecture Redundant Arrays of Inexpensive Disks and C Memory Management

Lectures 13 & 14. memory management

CS61C : Machine Structures

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

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II. Control. Consider memory to be a single huge array

CSC 1600 Memory Layout for Unix Processes"

Lecture 8 Dynamic Memory Allocation

Understanding Pointers

Dynamic memory allocation (malloc)

CS 11 C track: lecture 5

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

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

C Arrays, Strings, More Pointers Instructor: Steven Ho

ECE 15B COMPUTER ORGANIZATION

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

CS 61c: Great Ideas in Computer Architecture

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C : Machine Structures

Programming. Pointers, Multi-dimensional Arrays and Memory Management

CS 102 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

CS61C Midterm Review on C & Memory Management

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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

Kurt Schmidt. October 30, 2018

Memory (Stack and Heap)

Quick review pointer basics (KR ch )

CS61, Fall 2012 Section 2 Notes

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

HW1 due Monday by 9:30am Assignment online, submission details to come

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

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

CS 110 Computer Architecture Review for Midterm I

Heap Arrays. Steven R. Bagley

CS61C : Machine Structures

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

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

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

Fundamental of Programming (C)

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

Lecture 7: Procedures and Program Execution Preview

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

Intermediate Programming, Spring 2017*

Programs in memory. The layout of memory is roughly:

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS61C : Machine Structures

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

CS61C : Machine Structures

Memory Management. CSC215 Lecture

Memory Management. CS449 Fall 2017

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

Lecture 10: Program Development versus Execution Environment

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

Dynamic Data Structures. CSCI 112: Programming in C

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

CS 222: Pointers and Manual Memory Management

Systems Programming and Computer Architecture ( )

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

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 {

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

C Structures & Dynamic Memory Management

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

Dynamic memory allocation

Dynamic Memory Allocation: Basic Concepts

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Memory, Arrays & Pointers

Arrays and Pointers (part 1)

Instructor: Randy H. Katz hbp://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #4. Agenda

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

Low-Level C Programming. Memory map Pointers Arrays Structures

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

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

Dynamic Allocation of Memory

Transcription:

CS 110 Computer Architecture Lecture 4: Introduction to C, Part III Instructor: Sören Schwertfeger http://shtech.org/courses/ca/ School of Information Science and Technology SIST ShanghaiTech University Slides based on UC Berkley's CS61C 1

Agenda Pointers C Memory Management 2

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 memory address Type declaration tells compiler how many bytes to fetch on each access through pointer E.g., 32-bit integer stored in 4 consecutive 8-bit bytes short *y int *x char *z 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 Byte address 16-bit short stored in two bytes 32-bit integer stored in four bytes 8-bit character stored in one byte 3

sizeof() operator sizeof(type) returns number of bytes in object But number of bits in a byte is not standardized In olden times, when dragons roamed the earth, bytes could be 5, 6, 7, 9 bits long By definition, sizeof(char)==1 Can take sizeof(arr), or sizeof(structtype) We ll see more of sizeof when we look at dynamic memory management 4

Pointer Arithmetic pointer + number e.g., pointer + 1 pointer number adds 1 something to a pointer char *p; char a; char b; int *p; int a; int b; p = &a; p += 1; In each, p now points to b (Assuming compiler doesn t reorder variables in memory. Never code like this!!!!) p = &a; p += 1; Adds 1*sizeof(char) to the memory address Adds 1*sizeof(int) to the memory address Pointer arithmetic should be used cautiously 5

Arrays and Pointers Passing arrays: Array» pointer to the initial (0th) array element a[i] º *(a+i) An array is passed to a function as a pointer The array size is lost! Usually bad style to interchange arrays and pointers Avoid pointer arithmetic! Really int *array int foo(int array[], { int unsigned int size) array[size - 1] main(void) { int a[10], b[5]; Must explicitly pass the size foo(a, 10) foo(b, 5) 6

Arrays and Pointers int foo(int array[], unsigned int size) { printf( %d\n, sizeof(array)); int main(void) { int a[10], b[5]; foo(a, 10) foo(b, 5) printf( %d\n, sizeof(a)); What does this print?... because array is really a pointer (and a pointer is architecture dependent, but likely to be 8 on modern machines!) What does this print? 4 40 7

Arrays and Pointers int i; int array[10]; int *p; int array[10]; for (i = 0; i < 10; i++) { array[i] = ; for (p = array; p < &array[10]; p++) { *p = ; These code sequences have the same effect! 8

Quiz: int x[] = { 2, 4, 6, 8, 10 ; int *p = x; int **pp = &p; (*pp)++; (*(*pp))++; printf("%d\n", *p); Result is: A: 2 B: 3 C: 4 D: 5 E: None of the above 9

C Strings String in C is just an array of characters char string[] = "abc"; How do you tell how long a string is? Last character is followed by a 0 byte (aka null terminator ) int strlen(char s[]) { int n = 0; while (s[n]!= 0) n++; return n; 10

Concise strlen() int strlen(char *s) { char *p = s; while (*p++) ; /* Null body of while */ return (p s 1); What happens if there is no zero character at end of string? 11

Point past end of array? Array size n; want to access from 0 to n-1, but test for exit by comparing to address one element past the array int ar[10], *p, *q, sum = 0;... p = &ar[0]; q = &ar[10]; while (p!= q) /* sum = sum + *p; p = p + 1; */ sum += *p++; Is this legal? C defines that one element past end of array must be a valid address, i.e., not cause an error

Valid Pointer Arithmetic Add an integer to a pointer. Subtract 2 pointers (in the same array) Compare pointers (<, <=, ==,!=, >, >=) Compare pointer to NULL (indicates that the pointer points to nothing) Everything else illegal since makes no sense: adding two pointers multiplying pointers subtract pointer from integer

Arguments in main() To get arguments to the main function, use: int main(int argc, char *argv[]) What does this mean? argc contains the number of strings on the command line (the executable counts as one, plus one for each argument). Here argc is 2: sort myfile argv is a pointer to an array containing the arguments as strings 14

Example foo hello 87 argc = 3 /* number arguments */ argv[0] = "foo", argv[1] = "hello", argv[2] = "87" Array of pointers to strings 15

In the News: AMD Ryzen 2000 (Zen+) AMD s newest processor Ryzen 7 2700X (USD 369): 8 cores with SMT -> 16 threads 3.7GHz (Turbo: 4.35GHz) Cache: L2: 4MB, L3 20MB TDP: 120W 12 nm FinFET Up to 8 channels of DDR4 memory 16

17

18

19

20

C Memory Management How does the C compiler determine where to put all the variables in machine s memory? How to create dynamically sized objects? To simplify discussion, we assume one program runs at a time, with access to all of memory. Later, we ll discuss virtual memory, which lets multiple programs all run at same time, each thinking they own all of memory. 21

C Memory Management Program s address space contains 4 regions: stack: local variables inside functions, grows downward heap: space requested for dynamic data via malloc(); resizes dynamically, grows upward static data: variables declared outside functions, does not grow or shrink. Loaded when program starts, can be modified. code: loaded when program starts, does not change Memory Address (32 bits assumed here) ~ FFFF FFFF hex ~ 0000 0000 hex stack heap static data code 22

Where are Variables Allocated? If declared outside a function, allocated in static storage If declared inside function, allocated on the stack and freed when function returns main() is treated like a function int myglobal; main() { int mytemp; 23

The Stack Every time a function is called, a new frame is allocated on the stack Stack frame includes: Return address (who called me?) Arguments Space for local variables Stack frames contiguous blocks of memory; stack pointer indicates start of stack frame When function ends, stack frame is tossed off the stack; frees memory for future stack frames We ll cover details later for MIPS processor Stack Pointer fooa() { foob(); foob() { fooc(); fooc() { food(); fooa frame foob frame fooc frame food frame 24

Stack Animation Last In, First Out (LIFO) data structure main () { a(0); void a (int m) { b(1); void b (int n) Stack Pointer Stack grows down stack { c(2); void c (int o) { d(3); void d (int p) { 25

Managing the Heap C supports five functions for heap management: malloc() allocate a block of uninitialized memory calloc() allocate a block of zeroed memory free() free previously allocated block of memory realloc() change size of previously allocated block careful it might move! 26

Malloc() void *malloc(size_t n): Allocate a block of uninitialized memory NOTE: Subsequent calls might not yield blocks in contiguous addresses n is an integer, indicating size of allocated memory block in bytes size_t is an unsigned integer type big enough to count memory bytes sizeof returns size of given type in bytes, produces more portable code Returns void* pointer to block; NULL return indicates no more memory Think of pointer as a handle that describes the allocated block of memory; Additional control information stored in the heap around the allocated block! Cast operation, changes type of a variable. Here changes (void *) to (int *) Examples: int *ip; ip = (int *) malloc(sizeof(int)); typedef struct { TreeNode; TreeNode *tp = (TreeNode *) malloc(sizeof(treenode)); 27

Managing the Heap void free(void *p): Releases memory allocated by malloc() p is pointer containing the address originally returned by malloc() int *ip; ip = (int *) malloc(sizeof(int));....... free((void*) ip); /* Can you free(ip) after ip++? */ typedef struct { TreeNode; TreeNode *tp = (TreeNode *) malloc(sizeof(treenode));....... free((void *) tp); When insufficient free memory, malloc() returns NULL pointer; Check for it! if ((ip = (int *) malloc(sizeof(int))) == NULL){ printf( \nmemory is FULL\n ); exit(1); /* Crash and burn! */ When you free memory, you must be sure that you pass the original address returned from malloc() to free(); Otherwise, system exception (or worse)! 28

typedef struct node { int key; struct node *left; struct node *right; Node; Node *root = 0; Using Dynamic Memory Node *create_node(int key, Node *left, Node *right) { Node *np; if ( (np = (Node*) malloc(sizeof(node))) == NULL) { printf("memory exhausted!\n"); exit(1); else { np->key = key; np->left = left; np->right = right; return np; void insert(int key, Node **tree) { if ( (*tree) == NULL) { (*tree) = create_node(key, NULL, NULL); return; Root Key=10 if (key <= (*tree)->key) insert(key, &((*tree)->left)); else insert(key, &((*tree)->right)); 29 Left Left Key=5 Right Right Left Left Key=11 Key=16 Right Right

Observations Code, Static storage are easy: they never grow or shrink Stack space is relatively easy: stack frames are created and destroyed in last-in, first-out (LIFO) order Managing the heap is tricky: memory can be allocated / deallocated at any time 30

Question! int x = 2; int result; int foo(int n) { int y; if (n <= 0) { printf("end case!\n"); return 0; else { y = n + foo(n-x); return y; result = foo(10); Right after the printf executes but before the return 0, how many copies of x and y are there allocated in memory? A: #x = 1, #y = 1 B: #x = 1, #y = 5 C: #x = 5, #y = 1 D: #x = 1, #y = 6 E: #x = 6, #y = 6 31

How are Malloc/Free implemented? Underlying operating system allows malloc library to ask for large blocks of memory to use in heap (e.g., using Unix sbrk() call) C standard malloc library creates data structure inside unused portions to track free space 32

Simple Slow Malloc Implementation Initial Empty Heap space from Operating System Free Space Malloc library creates linked list of empty blocks (one block initially) Object 1 Free First allocation chews up space from start of free space Free After many mallocs and frees, have potentially long linked list of odd-sized blocks Frees link block back onto linked list might merge with neighboring free space 33

Faster malloc implementations Keep separate pools of blocks for different sized objects Buddy allocators always round up to powerof-2 sized chunks to simplify finding correct size and merging neighboring blocks: 34

Power-of-2 Buddy Allocator used free 35

Malloc Implementations All provide the same library interface, but can have radically different implementations Uses headers at start of allocated blocks and space in unallocated memory to hold malloc s internal data structures Rely on programmer remembering to free with same pointer returned by malloc Rely on programmer not messing with internal data structures accidentally! 36

Common Memory Problems Using uninitialized values Using memory that you don t own Deallocated stack or heap variable Out-of-bounds reference to stack or heap array Using NULL or garbage data as a pointer Improper use of free/realloc by messing with the pointer handle returned by malloc/calloc Memory leaks (you allocated something you forgot to later free) 37

Using Memory You Don t Own What is wrong with this code? Using pointers beyond the range that had been malloc d May look obvious, but what if mem refs had been result of pointer arithmetic that erroneously took them out of the allocated range? int *ipr, *ipw; void ReadMem() { int i, j; ipr = (*int) malloc(4 * sizeof(int)); i = *(ipr - 1000); j = *(ipr + 1000); free(ipr); void WriteMem() { ipw = (*int) malloc(5 * sizeof(int)); *(ipw - 1000) = 0; *(ipw + 1000) = 0; free(ipw); 38

Faulty Heap Management What is wrong with this code? int *pi; void foo() { pi = malloc(8*sizeof(int)); free(pi); void main() { pi = malloc(4*sizeof(int)); foo(); 39

Faulty Heap Management Memory leak: more mallocs than frees int *pi; void foo() { pi = malloc(8*sizeof(int)); /* Allocate memory for pi */ /* Oops, leaked the old memory pointed to by pi */ free(pi); /* foo() is done with pi, so free it */ void main() { pi = malloc(4*sizeof(int)); foo(); /* Memory leak: foo leaks it */ 40

Faulty Heap Management What is wrong with this code? int *plk = NULL; void genplk() { plk = malloc(2 * sizeof(int)); plk++; 41

Faulty Heap Management Potential memory leak handle has been changed, do you still have copy of it that can correctly be used in a later free? int *plk = NULL; void genplk() { plk = malloc(2 * sizeof(int)); plk++; 42

Faulty Heap Management What is wrong with this code? void FreeMemX() { int fnh = 0; free(&fnh); void FreeMemY() { int *fum = malloc(4 * sizeof(int)); free(fum+1); free(fum); free(fum); 43

Faulty Heap Management Can t free non-heap memory; Can t free memory that hasn t been allocated void FreeMemX() { int fnh = 0; free(&fnh); void FreeMemY() { int *fum = malloc(4 * sizeof(int)); int arr[4]; *(fum+1) free(fum+1); free(fum); free(fum); 44

Using Memory You Haven t Allocated What is wrong with this code? void StringManipulate() { const char *name = Safety Critical"; char *str = malloc(10); strncpy(str, name, 10); str[10] = '\0'; printf("%s\n", str); 45

Using Memory You Haven t Allocated Reference beyond array bounds void StringManipulate() { const char *name = Safety Critical"; char *str = malloc(10); strncpy(str, name, 10); str[10] = '\0'; /* Write Beyond Array Bounds */ printf("%s\n", str); /* Read Beyond Array Bounds */ 46

Using Memory You Don t Own What s wrong with this code? char *append(const char* s1, const char *s2) { const int MAXSIZE = 128; char result[128]; int i=0, j=0; for (j=0; i<maxsize-1 && j<strlen(s1); i++,j++) { result[i] = s1[j]; for (j=0; i<maxsize-1 && j<strlen(s2); i++,j++) { result[i] = s2[j]; result[++i] = '\0'; return result; 47

Using Memory You Don t Own Beyond stack read/write char *append(const char* s1, const char *s2) { const int MAXSIZE = 128; char result[128]; result is a local array name int i=0, j=0; stack memory allocated for (j=0; i<maxsize-1 && j<strlen(s1); i++,j++) { result[i] = s1[j]; for (j=0; i<maxsize-1 && j<strlen(s2); i++,j++) { result[i] = s2[j]; result[++i] = '\0'; return result; Function returns pointer to stack memory won t be valid after function returns 48

Using Memory You Don t Own What is wrong with this code? typedef struct node { struct node* next; int val; Node; int findlastnodevalue(node* head) { while (head->next!= NULL) { head = head->next; return head->val; 49

Using Memory You Don t Own Following a NULL pointer to mem addr 0! typedef struct node { struct node* next; int val; Node; int findlastnodevalue(node* head) { while (head->next!= NULL) { head = head->next; return head->val; 50

Managing the Heap realloc(p,size): Resize a previously allocated block at p to a new size If p is NULL, then realloc behaves like malloc If size is 0, then realloc behaves like free, deallocating the block from the heap Returns new address of the memory block; NOTE: it is likely to have moved! E.g.: allocate an array of 10 elements, expand to 20 elements later int *ip; ip = (int *) malloc(10*sizeof(int)); /* always check for ip == NULL */ ip = (int *) realloc(ip,20*sizeof(int)); /* always check for ip == NULL */ /* contents of first 10 elements retained */ realloc(ip,0); /* identical to free(ip) */ 51

Using Memory You Don t Own What is wrong with this code? int* init_array(int *ptr, int new_size) { ptr = realloc(ptr, new_size*sizeof(int)); memset(ptr, 0, new_size*sizeof(int)); return ptr; int* fill_fibonacci(int *fib, int size) { int i; init_array(fib, size); /* fib[0] = 0; */ fib[1] = 1; for (i=2; i<size; i++) fib[i] = fib[i-1] + fib[i-2]; return fib; 52

Using Memory You Don t Own Improper matched usage of mem handles int* init_array(int *ptr, int new_size) { ptr = realloc(ptr, new_size*sizeof(int)); memset(ptr, 0, new_size*sizeof(int)); return ptr; Remember: realloc may move entire block int* fill_fibonacci(int *fib, int size) { int i; /* oops, forgot: fib = */ init_array(fib, size); /* fib[0] = 0; */ fib[1] = 1; for (i=2; i<size; i++) fib[i] = fib[i-1] + fib[i-2]; return fib; What if array is moved to new location? 53

And In Conclusion, All data is in memory Each memory location has an address to use to refer to it and a value stored in it Pointer is a C version (abstraction) of a data address * follows a pointer to its value & gets the address of a value Arrays and strings are implemented as variations on pointers C is an efficient language, but leaves safety to the programmer Variables not automatically initialized Use pointers with care: they are a common source of bugs in programs 54

And In Conclusion, Pointers are an abstraction of machine memory addresses Pointer variables are held in memory, and pointer values are just numbers that can be manipulated by software In C, close relationship between array names and pointers Pointers know the type of the object they point to (except void *) Pointers are powerful but potentially dangerous 55

And In Conclusion, C has three main memory segments in which to allocate data: Static Data: Variables outside functions Stack: Variables local to function Heap: Objects explicitly malloc-ed/free-d. Heap data is biggest source of bugs in C code 56