Week 9 Part 2 Kyle Dewey. Overview Announcement More with structs and memory Assertions Exam #2 Course review.

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Dynamic memory allocation
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
CS 450 Module R4. R4 Overview Due on March 11 th along with R3. R4 is a small yet critical part of the MPX system. In this module, you will add the functionality.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Dynamic Memory Allocation I Topics Simple explicit allocators Data structures Mechanisms Policies CS 105 Tour of the Black Holes of Computing.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts / : Introduction to Computer Systems 18 th Lecture, March 24, 2015 Instructors:
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 9 MEMORY MANAGEMENT.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB.
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:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Week #10 Kyle Dewey. Overview Meeting times Typos Project #3 tidbits Exam Review.
Informática II Prof. Dr. Gustavo Patiño MJ
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
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.
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Pointers Applications
CS 61C L04 C Structures, Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
C Course Lecture 4 This lecture we'll talk about: Multi-dimensional arrays. Pointer arithmetic. Pointers to structures. Multi-file programming. What is.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Outline Midterm results Static variables Memory model
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
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.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Trees and beyond Tutorial #3 CPSC 261. Trees are just an example The next two weeks in labs we are playing with trees – Trees are interesting – Trees.
Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.
THE PREPROCESSOR
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
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.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
CS 241 Discussion Section (02/02/2012). MP2 Overview  Task is simple  Reimplement malloc(), calloc(), realloc() and free()  A contest will be running.
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
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 *
Hank Childs, University of Oregon April 6 th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Dynamic Allocation in C
CSE 374 Programming Concepts & Tools
ENEE150 Discussion 07 Section 0101 Adam Wang.
Checking Memory Management
Memory Management III: Perils and pitfalls Mar 13, 2001
Dynamic Memory Allocation
Memory Allocation CS 217.
Programming in C Miscellaneous Topics.
Review & Lab assignments
C Programming Lecture-8 Pointers and Memory Management
Programming in C Advanced Pointers.
Module 13 Dynamic Memory.
CSE 303 Concepts and Tools for Software Development
Dynamic Data Structures
Presentation transcript:

Week 9 Part 2 Kyle Dewey

Overview Announcement More with structs and memory Assertions Exam #2 Course review

Next “Lab” No pre-lab The lab will be entirely review It will be graded

More with Structs

Nesting Structs We can also nest structs, like so: struct Point { int x; int y; }; struct Circle { struct Point center; int radius; };

struct Address { int streetNumber; char* street; int zip; int state; }; struct Date { int month; int day; int year; }; struct Person { struct Address address; char* name; struct Date birthday; };

Pointers to Structs Remember, we can also have pointers to other structs in a struct, or even pointers to a struct of the same type struct IntegerList { int integer; struct IntegerList* nextInteger; };

Returning Structs Structs can be returned just like any other data The same rules about structs versus pointers to structs apply

Returning Structs This code is perfectly valid: struct Foo { int x; int y; }; struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; }

Internal Representation struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; } f struct Foo x: undef y: undef

Internal Representation struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; } f struct Foo x: undef y: undef

Internal Representation struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; } f struct Foo x: undef y: undef

Internal Representation struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; } f struct Foo x: undef y: undef retval struct Foo x: 1 y: 2

Internal Representation struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; } f struct Foo x: undef y: undef retval struct Foo x: 1 y: 2

Internal Representation struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; } f struct Foo x: 1 y: 2 retval struct Foo x: 1 y: 2 copy

Internal Representation struct Foo doSomething( int a ) { struct Foo retval = { a, a + 1 }; return retval; } int main() { struct Foo f; f = doSomething( 1 ); return 0; } f struct Foo x: 1 y: 2

Returning Structs This code has an issue: struct Foo { int x; int y; }; struct Foo* doSomething( int a ) { struct Foo retval = { a, a + 1 }; return &retval; } int main() { struct Foo* f; f = doSomething( 1 ); return 0; }

Problem StackHeap struct Foo retval = { a, a + 1 }; retval struct Foo x: 1 y: 2

Problem StackHeap return &retval; retval struct Foo x: 1 y: 2 retval

Problem StackHeap > retval ????????????? ????????????? ????????????? ?????????

Problem StackHeap struct Foo* f = doSomething( 1 ); retval ????????????? ????????????? ????????????? ?????????

Returning Structs This code is ok: struct Foo { int x; int y; }; struct Foo* doSomething( int a ) { struct Foo* retval = malloc( sizeof( struct Foo ) ); retval->x = a; retval->y = a + 1; return retval; } int main() { struct Foo* f = doSomething( 1 ); free( f ); return 0; }

Why it’s Ok StackHeap struct Foo* retval = malloc( sizeof( struct Foo ) ); struct Foo x: undef y: undef retval

Why it’s Ok StackHeap retval->x = a; retval->y = a + 1; struct Foo x: 1 y: 2 retval

Why it’s Ok StackHeap return retval; struct Foo x: 1 y: 2 retval

Why it’s Ok StackHeap struct Foo* f = doSomething( 1 ); struct Foo x: 1 y: 2 retval f copy

Why it’s Ok StackHeap struct Foo* f = doSomething( 1 ); struct Foo x: 1 y: 2 f

Why it’s Ok StackHeap free( f ); ????????????? ????????????? ?????????? f

Recall The stack is automatically managed Allocation: variable / struct / array declaration Deallocation: leave a block The heap is manually managed Allocation: malloc / calloc / realloc Deallocation: free

Assertions

Used to assert that something is true at a given point If it is true, the program goes on If it is not true, then the program terminates

Using Assertions #include int main() { assert( 3 * 2 > 7 ); return 0; } Assertion failed: (3 * 2 > 7), function main, file assert.c, line 4.Abort trap: 6

Usefulness Great for debugging Can put assumptions into code Acts as executable documentation void doSomething( int* pointer ) { // assume pointer isn’t NULL assert( pointer != NULL ); printf( “%i\n”, *pointer ); }

Caveats They are intended as a debugging tool They can be shut off like so: #define NDEBUG #include int main() { assert( 3 * 2 > 7 ); return 0; }

Question #1 #include int main() { int x = 0; assert( x = 3 ); // what does x equal? return 0; }

Question #1 #include int main() { int x = 0; assert( x = 3 ); // x == 3 return 0; }

Question #2 #define NDEBUG #include int main() { int x = 0; assert( x = 3 ); // what does x equal? return 0; }

Question #2 #define NDEBUG #include int main() { int x = 0; assert( x = 3 ); // x = 0 return 0; }

The Point No work should be done in an assertion

Exam #2

Statistics Average: 78 Min: 52 Max: 97 A’s: 8 B’s: 12 C’s: 15 D’s: 4 F’s: 2

Course Review