CSE 303 Concepts and Tools for Software Development

Slides:



Advertisements
Similar presentations
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.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
1 Memory Allocation Professor Jennifer Rexford COS 217.
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:
Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015 Some slides.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
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.
C and Data Structures Baojian Hua
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Memory Layout C and Data Structures Baojian Hua
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Lecture Contents Arrays and Vectors: Concepts of pointers and references. Pointer declarations and initialization. Pointer Operators. Dynamic Memory Allocation.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
C++ Memory Overview 4 major memory segments Key differences from Java
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
CSE 220 – C Programming malloc, calloc, realloc.
Dynamic Allocation in C
Object Lifetime and Pointers
CS/COE 0449 (term 2174) Jarrett Billingsley
Dynamic Storage Allocation
Memory allocation & parameter passing
Computer Organization and Design Pointers, Arrays and Strings in C
Overview 4 major memory segments Key differences from Java stack
5.13 Recursion Recursive functions Functions that call themselves
CSE 374 Programming Concepts & Tools
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Day 03 Introduction to C.
CSE 374 Programming Concepts & Tools
ENEE150 Discussion 07 Section 0101 Adam Wang.
C Programming Language Review and Dissection IV
CSE 303 Concepts and Tools for Software Development
CSE 374 Programming Concepts & Tools
Pointers and Memory Overview
Day 03 Introduction to C.
Dynamic Memory Allocation
Dynamically Allocated Memory
CSC 253 Lecture 8.
Lecture 4: Process Memory Layout
CSC 253 Lecture 8.
Overview 4 major memory segments Key differences from Java stack
Overview of Memory Layout in C++
Dynamic Memory Allocation
Memory Allocation CS 217.
CSE 303 Concepts and Tools for Software Development
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
EECE.2160 ECE Application Programming
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Dynamic Memory A whole heap of fun….
Dynamic Allocation in C
EECE.2160 ECE Application Programming
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Chapter 10-1: Dynamic Memory Allocation
COP 3330 Object-oriented Programming in C++
Introduction to Data Structures and Software Engineering
Dynamic Memory – A Review
Classes and Objects Object Creation
CSE 303 Concepts and Tools for Software Development
SPL – PS2 C++ Memory Handling.
Presentation transcript:

CSE 303 Concepts and Tools for Software Development CSE 303 Lecture 10 10/20/2006 CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/20/2006 Lecture 10 – Structs and the Heap

Administravia Optional Exercise for Assignment 3 Write 4 programs that average numbers Input on command line, all in main Make input come from stdin Move computation into function (static array) Make function take heap allocated array Don't turn this in! 10/20/2006 CSE 303 Lecture 10

Tip of the Day Searching in man pages less (used by man) Emacs To search for word, type /word To repeat previous search, type / Backspace to quit Emacs M-x man Search as usual 10/20/2006 CSE 303 Lecture 10

Last Lecture Syntax Command-line Arguments Type names Arrays Strings 10/20/2006 CSE 303 Lecture 10

Today Defining New Types Manual Memory Management Structs Enumerations The Heap 10/20/2006 CSE 303 Lecture 10

Java Types vs. C Types Java C Define enumeration (Java 5) Define class Define struct 10/20/2006 CSE 303 Lecture 10

Enumerations enum defines global integer constants Default: first constant has value 0 Subsequent constants have increasing values Values of individual constants can be set Enumerations are awful No type safety No namespace No pretty printing Examples in enum.c 10/20/2006 CSE 303 Lecture 10

C Structs vs. Java Classes Similarities Both define a named type Both have <type, name> pairs Differences All struct fields "public" Structs not automatically grouped w/functions Structs can be allocated on the stack Don't need "new" (and can't be "null") Example in struct.c 10/20/2006 CSE 303 Lecture 10

Struct Syntax Defining Declaring Accessing struct rec { int t; char *n; long x[4]; }; Declaring struct rec r, *pr; Accessing long var1 = r.t; long var2 = pr->t; same as long var2 = (*pr).t; 10/20/2006 CSE 303 Lecture 10

Structs in Expressions Can be locations (left) or values (right) Field: r.t, pr->t Whole struct: r, *pr Examples r.t = pr->t; r = (*pr); *pr is temporarily allocated on stack, copied to r (*pr) = r; *pr is location; no temporary allocation 10/20/2006 CSE 303 Lecture 10

Typedef creates new type name Defining structs with typedef typedef struct _rec { int t; char *n; } rec; Declaring rec r, *pr; Compare struct.c with structTypedef.c Optional 10/20/2006 CSE 303 Lecture 10

Manual Memory Management Up to now, all data has been on the stack Allocated when declared Reclaimed when function/block ends Array size is constant (C90 only) Heap allocation Allocated with a function call Reclaimed with a function call Array size determined at run time 10/20/2006 CSE 303 Lecture 10

Remember the Heap? 0xFFFFFFFF Stack (Dynamically Allocated) Heap (Dynamically Allocated) Address Space Static Data (“Data Segment”) Code (“Text Segment”) 0x00000000 10/20/2006 CSE 303 Lecture 10

malloc Library function: malloc Behavior Takes a number Allocates that many bytes Returns a pointer to newly allocated memory Behavior returns NULL on failure Does not initialize the memory Great for hackers! 10/20/2006 CSE 303 Lecture 10

Using malloc Allocating one object Allocating an array of objects type *t = (type*)malloc(sizeof(type)); Allocating an array of objects type *t = (type*)malloc(n*sizeof(type)); Variants calloc: allocate arrays and initialize to 0 realloc: attempts to resize a block 10/20/2006 CSE 303 Lecture 10

malloc vs. Java's new malloc is missing some functionality Fields of structures not initialized No automatic call to a constructor But both return a pointer/reference! 10/20/2006 CSE 303 Lecture 10

Freeing Memory Heap allocated objects "live" forever Quick way to run out of memory! Solutions: Java: garbage collector discards unused stuff C: Explicitly discard using free Forget to free memory? "Memory leak" 10/20/2006 CSE 303 Lecture 10

Examples int *p = (int*)malloc(sizeof(int)); p = NULL; //LEAK!! int *q = (int*)malloc(sizeof(int)); free(q); free(q); // VERY BAD!!! int *r = (int*)malloc(sizeof(int)); free(r); int *s = (int*)malloc(sizeof(int)); free(s); *s = 19; *r = 17; // EVEN WORSE!! *s may be 17 !? 10/20/2006 CSE 303 Lecture 10

Memory Management Rules For every run-time call to malloc Make sure there is a run-time call to free Burn this into your mind!!! Avoid dangling pointers Avoid memory leaks We'll see lots more of this 10/20/2006 CSE 303 Lecture 10

Summary Defining New Types Manual Memory Management Structs Enumerations Manual Memory Management The Heap 10/20/2006 CSE 303 Lecture 10

Reading Programming in C Chapter 9: Structs pp240-244: Pointers and Structures Chapter 14: More on Data Types pp383-388: Dynamic memory allocation 10/20/2006 CSE 303 Lecture 10

Next Time A "real" data type Linked lists 10/20/2006 CSE 303 Lecture 10