Similar documents
PRINCIPLES OF OPERATING SYSTEMS

CS61, Fall 2012 Section 2 Notes

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

Class Information ANNOUCEMENTS

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

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

Dynamic memory allocation (malloc)

The Preprocessor. CS 2022: Introduction to C. Instructor: Hussam Abu-Libdeh. Cornell University (based on slides by Saikat Guha) Fall 2011, Lecture 9

Lecture 8 Dynamic Memory Allocation

Understanding Pointers

Systems Programming and Computer Architecture ( )

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

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

CS 11 C track: lecture 5

Memory Management. CS449 Fall 2017

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

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

High Performance Programming Programming in C part 1

Fundamental of Programming (C)

Memory (Stack and Heap)

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Kurt Schmidt. October 30, 2018

Quick review pointer basics (KR ch )

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

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

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

CS201- Introduction to Programming Current Quizzes

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

CSE 303: Concepts and Tools for Software Development

CS 222: Pointers and Manual Memory Management

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

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

Arrays and Pointers in C. Alan L. Cox

Dynamic Data Structures. CSCI 112: Programming in C

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

EL2310 Scientific Programming

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

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

Dynamic memory allocation

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Processes. Johan Montelius KTH

A process. the stack

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

CS 5513 Entry Quiz. Systems Programming (CS2213/CS3423))

CSC 1600 Memory Layout for Unix Processes"

CS C Primer. Tyler Szepesi. January 16, 2013

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

pthreads CS449 Fall 2017

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Memory Allocation. General Questions

C Introduction. Comparison w/ Java, Memory Model, and Pointers

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

Intermediate Programming, Spring 2017*

CS201 - Lecture 1 The C Programming Language

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

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

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

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CS 314 Principles of Programming Languages. Lecture 11

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.

Language comparison. C has pointers. Java has references. C++ has pointers and references

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Section 2: Processes

Structured Programming. Functions and Structured Programming. Functions. Variables

ECE264 Fall 2013 Exam 1, September 24, 2013

Dynamic Memory Allocation and Command-line Arguments

SYSC 2006 C Winter 2012

CENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts

CA341 - Comparative Programming Languages

Quiz 0 Review Session. October 13th, 2014

Secure Programming Lecture 3: Memory Corruption I (Stack Overflows)

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Programming. Pointers, Multi-dimensional Arrays and Memory Management

EL2310 Scientific Programming

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

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

Programming in C - Part 2

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Heap Arrays. Steven R. Bagley

Lectures 5-6: Introduction to C

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

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

EL2310 Scientific Programming

Procedural programming with C

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

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

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

QUIZ on Ch.8. What is kit?

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

EC 413 Computer Organization

Transcription:

ntroduction to C CS 2022: ntroduction to C nstructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 1 ntroduction to C CS 2022, Fall 2011, Lecture 1

History of C Writing code in an assembler gets real old real fast Really low level (no loops, functions, if-then-else) Not portable (di erent for each architecture) BCPL (by Martin Richards): Grandparent of C Close to the machine Procedures, Expressions, Statements, Pointers,... B(byKenThompson):ParentofC Simplified BCPL Some types (int, char) ntroduction to C CS 2022, Fall 2011, Lecture 1

History of C C(byKernighanandRitchie) Much faster than B Arrays, Structures, more types Standardization Portability enhanced Parent of Objective C, Concurrent C, C*, C++ ntroduction to C CS 2022, Fall 2011, Lecture 1

When to use C Working close to hardware Operating System Device Drivers Need to violate type-safety Pack and unpack bytes nline assembly Cannot tolerate overheads No garbage collector No array bounds check No memory initialization No exceptions ntroduction to C CS 2022, Fall 2011, Lecture 1

When not to use C Use JAVA or C# for... Quick prototyping Python or Ruby are even better here Compile-once Run-Everywhere Reliability is critical, and performance is secondary Ccanbevery reliable, but requires tremendous programmer discipline For many programs, JAVA can match C performance, but not always ntroduction to C CS 2022, Fall 2011, Lecture 1

Hello World CS 2022: ntroduction to C nstructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 2 Hello World CS 2022, Fall 2011, Lecture 2

Environment OS: GNU/Linux Editor: vim Compiler: gcc Debugger: gdb Hello World CS 2022, Fall 2011, Lecture 2

Structure of a C Program Overall Program <some pre-processor directives> <global declarations> <global variables> <functions> Hello World CS 2022, Fall 2011, Lecture 2

Structure of a C Program Functions <function header> <local declarations> <statements> Hello World CS 2022, Fall 2011, Lecture 2

hello.c: Hello World #include <stdio.h> int main() { printf("hello World\n"); return 0; Hello World CS 2022, Fall 2011, Lecture 2

Compiling and Running $ gcc -o hello hello.c $./hello Hello World Hello World CS 2022, Fall 2011, Lecture 2

vars.c: Variables #include <stdio.h> int main() { int a, b, c; a = 10; b = 20; c = a * b; printf("a=%d b=%d c=%d\n", a, b, c); return 0; a=10 b=20 c=200 Hello World CS 2022, Fall 2011, Lecture 2

func.c: Functions #include <stdio.h> int add(int a, int b) { printf("a=%d b=%d\n", a, b); return a+b; int main() { printf("ret=%d\n", add(10, 20)); return 0; a=10 b=20 ret=30 Hello World CS 2022, Fall 2011, Lecture 2

cond.c: Conditionals #include <stdio.h> int main() { int i = 10; if (10 == i) { printf("equal to ten\n"); else { printf("not equal to ten\n"); return 0; equal to ten Hello World CS 2022, Fall 2011, Lecture 2

loop.c: Loops #include <stdio.h> int main() { int i; for (i = 0; i < 10; i++) { printf("%d ", i); printf("done.\n"); return 0; 0 1 2 3 4 5 6 7 8 9 done. Hello World CS 2022, Fall 2011, Lecture 2

rec.c: Recursion #include <stdio.h> void rec(int a) { printf("in %d\n", a); if (a > 0) rec(a-1); printf("out %d\n", a); int main() { rec(2); return 0; in 2 in 1 in 0 out 0 out 1 out 2 Hello World CS 2022, Fall 2011, Lecture 2

cmdarg.c: Command Line Args #include <stdio.h> int main(int argc, char **argv) { int n, m; n = atoi(argv[1]); m = atoi(argv[2]); printf("argument 1: %d\nargument 2: %d\n", n, m); return 0; Argument 1: 10 Argument 2: 20 Hello World CS 2022, Fall 2011, Lecture 2

Pointers CS 2022: ntroduction to C nstructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 3 Pointers CS 2022, Fall 2011, Lecture 3

Pointer Pointer A pointer is just another variable that points to another variable. A pointer contains the memory address of the variable it points to. int i; int *p; int **m; // nteger // Pointer to integer // Pointer to int pointer p = &i; // p now points to i printf("%p", p); // address of i (in p) m = &p; // m now points to p printf("%p", m); // address of p (in m) Pointers CS 2022, Fall 2011, Lecture 3

Pointers Pointers CS 2022, Fall 2011, Lecture 3

Pointers Pointers CS 2022, Fall 2011, Lecture 3

Pointers Pointers CS 2022, Fall 2011, Lecture 3

Pointers Pointers CS 2022, Fall 2011, Lecture 3

Pointers Pointers CS 2022, Fall 2011, Lecture 3

Pointers Pointers CS 2022, Fall 2011, Lecture 3

Pointers Pointers CS 2022, Fall 2011, Lecture 3

Pointers Pointers CS 2022, Fall 2011, Lecture 3

swap1.c: Swap #include <stdio.h> int main() { int a, b; int *p, *q; a = 10; b = 20; p = &a; q = &b; printf("before: %d, %d, %d, %d", a, b, *p, *q); Before: 10, 20, 10, 20 After: 10, 20, 20, 10 p = &b; q = &a; printf("after: %d, %d, %d, %d", a, b, *p, *q); return 0; Pointers CS 2022, Fall 2011, Lecture 3

swap2.c: Swap #include <stdio.h> int main() { int a, b; int *p, *q; a = 10; b = 20; p = &a; q = &b; printf("before: %d, %d, %d, %d", a, b, *p, *q); Before: 10, 20, 10, 20 After: 20, 10, 20, 10 a = 20; b = 10; printf("after: %d, %d, %d, %d", a, b, *p, *q); return 0; Pointers CS 2022, Fall 2011, Lecture 3

swap3.c: Swap #include <stdio.h> int main() { int a, b; int *p, *q; a = 10; b = 20; p = &a; q = &b; printf("before: %d, %d, %d, %d", a, b, *p, *q); Before: 10, 20, 10, 20 After: 20, 10, 10, 20 a = 20; b = 10; p = &b; q = &a; printf("after: %d, %d, %d, %d", a, b, *p, *q); return 0; Pointers CS 2022, Fall 2011, Lecture 3

Pointers to Pointers! #include <stdio.h> int main() { int a = 10, b = 20; int *p = &a, *q = &b; int **m = &p, **n = &q; printf("x: %d %d %d %d %d %d\n", **m, **n, *p, *q, a, b); *m = *n; m = n; *m = &a; n = &p; **n = 30; X: Y: printf("y: %d %d %d %d %d %d\n", **m, **n, *p, *q, a, b); return 0; Pointers CS 2022, Fall 2011, Lecture 3

Pointers to Pointers! #include <stdio.h> int main() { int a = 10, b = 20; int *p = &a, *q = &b; int **m = &p, **n = &q; printf("x: %d %d %d %d %d %d\n", **m, **n, *p, *q, a, b); *m = *n; m = n; *m = &a; n = &p; **n = 30; X: 10 20 10 20 10 20 Y: 10 30 30 10 10 30 printf("y: %d %d %d %d %d %d\n", **m, **n, *p, *q, a, b); return 0; Pointers CS 2022, Fall 2011, Lecture 3

Pointer Arithmetic Pointers CS 2022, Fall 2011, Lecture 3

Pointer Arithmetic Pointers CS 2022, Fall 2011, Lecture 3

Memory in C Variables ndependent variables are a figment of your imagination. When in C, think of memory cells. Each memory cell has an integer address. You can access any memory cell at any time from any function. Variable names are simply shortcuts for your convenience. Pointers CS 2022, Fall 2011, Lecture 3

Nameless Variables #include <stdlib.h> int main() { int *p = (int *)malloc(sizeof(int)); *p = 42; return 0; Pointers CS 2022, Fall 2011, Lecture 3

Apoorman sarray int * newarray(int siz) { return (int *)malloc(siz * sizeof(int)); void set(int *arr, int idx, int val) { *(arr+idx) = val; int get(int *arr, int idx) { return *(arr + idx); Pointers CS 2022, Fall 2011, Lecture 3

Multiple Return Values void getab(int *a, int *b) { *a = 10; *b = 20; int main() { int a, b; getab(&a, &b); Pointers CS 2022, Fall 2011, Lecture 3

Pointers Recap int *ptr; Pointers are variables that store memory addresses of other variables Type of variable pointed to depends on type of pointer: int *ptr points to an integer variable char *ptr points to a character variable Can cast between pointer types: my_int_ptr = (int *) my_other_ptr; void *ptr has an unspecified type; must be cast to a type before used Pointers CS 2022, Fall 2011, Lecture 3

Pointers Recap Two main operations: * dereference: gets the value at the memory location stored in a pointer & address of : gets the address of a variable int *my_ptr = &my_var; Pointer arithmetic: directly manipulate a pointer s content to access other memory locations Use with caution!: can crash your program due to bad memory accesses However, it is useful in accessing and manipulating data structures Pointers to pointers int **my_2d_array; Pointers CS 2022, Fall 2011, Lecture 3

Memory Model CS 2022: ntroduction to C nstructor: Hussam Abu-Libdeh (based on slides by Saikat Guha) Fall 2011, Lecture 4 Memory Model CS 2022, Fall 2011, Lecture 4

Memory addr=0xffffffff Function Stack Frame Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated main() fact(5) fact(4) Global Dynamic (Heap) Global Static Code return addr Local Var 1 Local Var 2 Local Var 3 Local Var 4 Arg 3 next fn Arg 2 next fn Arg 1 next fn addr=0x00000000 Memory Model CS 2022, Fall 2011, Lecture 4

The Stack Stores Function local variables Temporary variables Arguments for next function call Where to return when function ends Memory Model CS 2022, Fall 2011, Lecture 4

The Stack Managed by compiler One stack frame each time function called Created when function called Stacked on top (under) one another Destroyed at function exit Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; main() int main() { int res = fact(5); return 0; Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; int main() { int res = fact(5); return 0; main() 5 arg Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; int main() { int res = fact(5); return 0; main() fact(5) 5 arg Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; main() fact(5) 0 n==1 5 arg int main() { int res = fact(5); return 0; Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; main() 5 arg fact(5) 0 4 n==1 arg int main() { int res = fact(5); return 0; Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; main() 5 arg fact(5) 0 4 n==1 arg fact(4) int main() { int res = fact(5); return 0; Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; main() 5 arg fact(5) 0 4 n==1 arg fact(4) 0 3 n==1 arg int main() { int res = fact(5); return 0; Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; int main() { int res = fact(5); return 0; main() 5 arg fact(5) 0 4 n==1 arg fact(4) 0 3 n==1 arg fact(3) 0 2 n==1 arg Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; int main() { int res = fact(5); return 0; main() 5 arg fact(5) 0 4 n==1 arg fact(4) 0 3 n==1 arg fact(3) 0 2 n==1 arg fact(2) 0 1 n==1 arg Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; int main() { int res = fact(5); return 0; main() 5 arg fact(5) 0 4 n==1 arg fact(4) 0 3 n==1 arg fact(3) 0 2 n==1 arg fact(2) 0 1 n==1 arg fact(1) 1 n==1 Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; int main() { int res = fact(5); return 0; 1 res main() 5 arg fact(5) 0 4 n==1 arg fact(4) 0 3 n==1 arg fact(3) 0 2 n==1 arg fact(2) 0 n==1 Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; int main() { int res = fact(5); return 0; 2 res main() 5 arg fact(5) 0 4 n==1 arg fact(4) 0 3 n==1 arg fact(3) 0 n==1 Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; int main() { int res = fact(5); return 0; 6 res main() 5 arg fact(5) 0 4 n==1 arg fact(4) 0 n==1 Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; int main() { int res = fact(5); return 0; 24 res main() fact(5) 0 n==1 5 arg Memory Model CS 2022, Fall 2011, Lecture 4

The Stack int fact(int n) { int res; if (n == 1) return 1; res = fact(n-1); return n * res; int main() { int res = fact(5); return 0; 120 res main() Memory Model CS 2022, Fall 2011, Lecture 4

Stack games main() 5 arg Locate the stack Find the direction of stack growth Finding size of stack frame 2 res fact(5) 0 4 n==1 arg fact(4) 0 3 n==1 arg fact(3) 0 n==1 Memory Model CS 2022, Fall 2011, Lecture 4

What can go wrong Run out of stack space Unintentionally change values on the stack n some other function s frame Even return address from function Access memory even after frame is deallocated 2 res main() 5 arg fact(5) 0 4 n==1 arg fact(4) 0 3 n==1 arg fact(3) 0 n==1 Memory Model CS 2022, Fall 2011, Lecture 4

Memory Recap addr=0xffffffff Function Stack Frame Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated main() fact(5) fact(4) Global Dynamic (Heap) Global Static Code return addr Local Var 1 Local Var 2 Local Var 3 Local Var 4 Arg 3 next fn Arg 2 next fn Arg 1 next fn addr=0x00000000 Memory Model CS 2022, Fall 2011, Lecture 4

Heap Heap Needed for long-term storage that needs to persist across multiple function calls. Managed by programmer Created by ptr = malloc(size) Destroyed by free(ptr) MUST check the return value from malloc MUST explicitly free memory when no longer in use Memory Model CS 2022, Fall 2011, Lecture 4

Relevant Library #include <stdlib.h> ncludes definitions for malloc(), free(), and many other helpful functions. void * malloc(size t size); The malloc() function alocates size bytes of memory and returns a pointer to the allocated memory. void free(void *ptr); The free() function deallocates the memory allocation pointed to by ptr. Memory Model CS 2022, Fall 2011, Lecture 4

The Heap int main() { int *p, *q, *r; p = (int *)malloc(sizeof(int)); q = (int *)malloc( sizeof(int) * 10); r = (int *)malloc(sizeof(int)); if (p == NULL!q!r) {... do cleanup... return 1; free(p);... do other stuff... Memory Model CS 2022, Fall 2011, Lecture 4

The Heap int main() { int *p, *q, *r; p = (int *)malloc(sizeof(int)); q = (int *)malloc( sizeof(int) * 10); r = (int *)malloc(sizeof(int)); if (p == NULL!q!r) {... do cleanup... return 1; 4 bytes Metadata free(p);... do other stuff... Memory Model CS 2022, Fall 2011, Lecture 4

The Heap int main() { int *p, *q, *r; p = (int *)malloc(sizeof(int)); q = (int *)malloc( sizeof(int) * 10); r = (int *)malloc(sizeof(int)); if (p == NULL!q!r) {... do cleanup... return 1; 40 bytes Metadata 4 bytes Metadata free(p);... do other stuff... Memory Model CS 2022, Fall 2011, Lecture 4

The Heap int main() { int *p, *q, *r; p = (int *)malloc(sizeof(int)); q = (int *)malloc( sizeof(int) * 10); r = (int *)malloc(sizeof(int)); if (p == NULL!q!r) {... do cleanup... return 1; 4 bytes Metadata 40 bytes Metadata 4 bytes Metadata free(p);... do other stuff... Memory Model CS 2022, Fall 2011, Lecture 4

The Heap int main() { int *p, *q, *r; p = (int *)malloc(sizeof(int)); q = (int *)malloc( sizeof(int) * 10); r = (int *)malloc(sizeof(int)); if (p == NULL!q!r) {... do cleanup... return 1; 4 bytes Metadata 40 bytes Metadata free(p);... do other stuff... Memory Model CS 2022, Fall 2011, Lecture 4

Heap games Locate the heap How freespace is managed Find how memory is allocated How is fragmentation avoided 40 bytes Metadata 4 bytes Metadata Memory Model CS 2022, Fall 2011, Lecture 4

What can go wrong Run out of heap space malloc returns 0 Unintentionally change other heap data Or clobber heap metadata Access memory after free d free memory twice Create a memory leak 40 bytes Metadata 4 bytes Metadata Memory Model CS 2022, Fall 2011, Lecture 4