Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of email daily. If you email me and.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
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.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
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:
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
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.
CS 536 Spring Run-time organization Lecture 19.
Pointers “Absolute C++” Section 10.1
C and Data Structures Baojian Hua
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
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.
Run-time Environment and Program Organization
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
CSc 352 C : Arrays, Structs, Pointers
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Memory Layout C and Data Structures Baojian Hua
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
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
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
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,
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
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.
Dynamic memory allocation and Pointers Lecture 4.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
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.
Announcements Assignment 1 will be regraded for all who’s score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.
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.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Hank Childs, University of Oregon April 6 th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Lecture 3 Translation.
Memory-Related Perils and Pitfalls in C
CSE 220 – C Programming malloc, calloc, realloc.
C Programming Types & Dynamic memory & bits & others
ENEE150 Discussion 07 Section 0101 Adam Wang.
Dynamic Memory Allocation
Checking Memory Management
Storage.
C – Scope, Lifetime, and the Stack
Dynamic Memory A whole heap of fun….
Dynamic Memory Allocation
Memory Allocation CS 217.
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
Run-time environments
Presentation transcript:

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 don't hear back within a day or two, feel free to again. I don't want to blow anyone off, but sometimes my messages get buried and forgotten. The output file name for the coverage target should be the same as for noncoverage. (e.g. dna and battleship) 1

Announcements When doing corrections, remember to change your code as little as possible to get as much possible partial credit as possible. Follow the spec! Use diff to check your answers against the supplied solutions. Remember with this assignment to include a Makefile, and a directory of test cases that "cover" your code sufficiently. 2

Make reminder Makefile isFib: isFib.c gcc -Wall -o isFib isFib.c clean: rm -f isFib *.o 3 1.Often a makefile will include a "clean" object to remove files associated with the project 2.Here typing make clean will cause the executable to be deleted. 3.Notice that if there is no file named "clean" in the current directory, the rm command will run everytime you type make clean.

Make reminder Makefile isFib: isFib.c gcc -Wall -o isFib isFib.c debug: gcc -Wall –g -o isFib isFib.c clean: rm -f isFib *.o 4 1.What happens when we type make debug followed by make?

Phony Targets A phony target is one that is not the name of a file, e.g.: “make clean” will remove a.out and *.o files 5 phony target clean: rm –f *.o a.out

Phony Targets This won’t work if we (accidentally) create a file named “clean” Fix:.PHONY: clean clean: rm *.o a.out 6 clean: rm –f *.o a.out cleanup actions will be executed even if there is a file named “clean”

Make reminder Makefile isFib: isFib.c gcc -Wall -o isFib isFib.c.PHONY: debug debug: gcc -Wall –g -o isFib isFib.c.PHONY: clean clean: rm -f isFib *.o 7 1.The.PHONY lines tell make to make the targets debug and clean even if files named debug and clean already exist. (In other words, ALWAYS run those commands.)

Two common pointer problems Uninitialized pointers – the pointer has not been initialized to point to a valid location Dangling pointers – the pointer points at a memory location that has actually been deallocated 8

Background: Runtime Memory Organization Layout of an executing process’s virtual memory: 9 code global data memory mapped files operating system stack (grows downwards) heap (grows upwards) high addresses low addresses 0xffffffff 0x

Background: Runtime Memory Organization Runtime stack: 10 Code: stack growth p’s caller’s stack frame p’s stack frame top of stack p(…) { … q(…); s(…); } q(…) { … r(…); } r(…) { … } s(…) { … }

Background: Runtime Memory Organization Runtime stack: 11 p(…) { … q(…); s(…); } q(…) { … r(…); } r(…) { … } Code: stack growth p’s caller’s stack frame p’s stack frame top of stack s(…) { … }

Background: Runtime Memory Organization Runtime stack: 12 p(…) { … q(…); s(…); } q(…) { … r(…); } r(…) { … } Code: stack growth p’s caller’s stack frame p’s stack frame top of stack q’s stack frame s(…) { … }

Background: Runtime Memory Organization Runtime stack: 13 p(…) { … q(…); s(…); } q(…) { … r(…); } r(…) { … } Code: stack growth p’s caller’s stack frame p’s stack frame top of stack q’s stack frame r’s stack frame s(…) { … }

Background: Runtime Memory Organization Runtime stack: 14 p(…) { … q(…); s(…); } q(…) { … r(…); } r(…) { … } Code: stack growth p’s caller’s stack frame p’s stack frame top of stack q’s stack frame r’s stack frame (deallocated) s(…) { … }

Background: Runtime Memory Organization Runtime stack: 15 p(…) { … q(…); s(…); } q(…) { … r(…); } r(…) { … } Code: stack growth p’s caller’s stack frame p’s stack frame top of stack q’s stack frame r’s stack frame s(…) { … }

Background: Runtime Memory Organization Runtime stack: 16 p(…) { … q(…); s(…); } q(…) { … r(…); } r(…) { … } Code: stack growth p’s caller’s stack frame p’s stack frame top of stack q’s stack frame r’s stack frame s(…) { … } s’s stack frame

Uninitialized pointers: Example 17 str was never initialized to point to anything fix: initialize str

Dangling pointers What’s wrong with this code? 18

Dangling pointers What’s wrong with this code? 19 runtime stack main my_read read_string buf string str

Dangling pointers What’s wrong with this code? 20 runtime stack main my_read read_string a c b a buf string str

Dangling pointers What’s wrong with this code? 21 runtime stack main my_read read_string a c b a buf string str

Dangling pointers What’s wrong with this code? 22 runtime stack main my_read read_string a c b a buf string str dangling pointer!

Dynamic memory allocation We can’t always anticipate how much memory to allocate – too little  program doesn’t work – too much  wastes space Solution: allocate memory at runtime as necessary – malloc(), calloc() allocates memory in the heap area – free() deallocates previously allocated heap memory block program memory layout reserved code global variables, strings heap stack reserved 23 low addr high addr

Dynamic memory allocation: usage 24 void * : “generic pointer” Usage: int *iptr = malloc(sizeof(int))// one int char *str = malloc(64)// an array of 64 chars // ( sizeof(char) = 1 by definition ) int *iarr = calloc(40, sizeof(int))// a 0-initialized array of 40 ints

Dynamic memory allocation: example 1 25 ALWAYS check the return value of any system call that may fail

Dynamic memory allocation: example 1 26

Dynamic memory allocation: example 2 27 figure out the total size of the concatenated strings allocate space concatenate the strings into buf

Dynamic memory allocation: example 2 28