Week 7 - Friday.  What did we talk about last time?  Allocating 2D arrays.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

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.
The University of Adelaide, School of Computer Science
Dynamic Memory Management
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Dynamic Memory Allocation I Topics Simple explicit allocators Data structures Mechanisms Policies CS 105 Tour of the Black Holes of Computing.
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
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.
Memory management.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 Memory Allocation Professor Jennifer Rexford COS 217.
User-Level Memory Management in Linux Programming
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
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.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng
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{
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Malloc Recitation Section K (Kevin Su) November 5 th, 2012.
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’;
Lab 3: Malloc Lab. “What do we need to do?”  Due 11/26  One more assignment after this one  Partnering  Non-Honors students may work with one other.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Run-time Environment and Program Organization
1 Inner Workings of Malloc and Free Professor Jennifer Rexford COS 217.
Memory Layout C and Data Structures Baojian Hua
Pointers Applications
Memory Allocation CS Introduction to Operating Systems.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
Week 8 - Monday.  What did we talk about last time?  Memory layout in the system  Lab 7.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Week 7 - Wednesday.  What did we talk about last time?  scanf()  Memory allocation  malloc()  free()
1 Writing a Good Program 8. Elementary Data Structure.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
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.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
ECE Application Programming
CSCI 156: Lab 11 Paging. Our Simple Architecture Logical memory space for a process consists of 16 pages of 4k bytes each. Your program thinks it has.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
Carnegie Mellon 1 Malloc Lab : Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
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
Dynamic Memory Allocation
The Hardware/Software Interface CSE351 Winter 2013
Circular Buffers, Linked Lists
Optimizing Malloc and Free
CS Introduction to Operating Systems
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Memory Allocation CS 217.
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Dynamic Memory A whole heap of fun….
Dynamic Allocation in C
EECE.2160 ECE Application Programming
CSCI 380: Operating Systems William Killian
Dynamic Memory A whole heap of fun….
Dynamic Memory – A Review
EECE.2160 ECE Application Programming
Week 7 - Friday CS222.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Week 7 - Friday

 What did we talk about last time?  Allocating 2D arrays

In theory, theory and practice are the same. In practice, they’re not. Yoggi Berra In theory, theory and practice are the same. In practice, they’re not. Yoggi Berra

 Dynamically allocate an 8 x 8 array of char values  Loop through each element in the array  With 1/8 probability, put a 'Q' in the element, representing a queen  Otherwise, put a ' ' (space) in the element  Print out the resulting chessboard  Use | and – to mark rows and columns  Print out whether or not there are queens that can attack each other

 There are really low level functions brk() and sbrk() which essentially increase the maximum size of the heap  You can use any of that space as a memory playground  malloc() gives finer grained control  But also has additional overhead

 malloc() sees a huge range of free memory when the program starts  It uses a doubly linked list to keep track of the blocks of free memory, which is perhaps one giant block to begin with  As you allocate memory, a free block is often split up to make the block you need  The returned block knows its length  The length is usually kept before the data that you use Allocated Space Length Returned pointer

 The free list is a doubly linked list of available blocks of memory  Each block knows its length, the next block in the list, and the previous block  In a 32-bit architecture, the length, previous, and next data are all 4 bytes  Free block  Allocated block Free Space Length Previous Next Allocated Space Length

 Here's a visualization of the free list  When an item is freed, most implementations will try to coalesce two neighboring free blocks to reduce fragmentation  Calling free() can be time consuming Head Allocated L L Free L L P P N N L L P P N N NULL

 void* calloc(size_t items, size_t size);  Clear and allocate items items with size size  Memory is zeroed out  void* realloc(void* pointer, size_t size);  Resize a block of memory pointed at by pointer, usually to be larger  If there is enough free space at the end, realloc() will tack that on  Otherwise, it allocates new memory and copies over the old  void* alloca(size_t size);  Dynamically allocate memory on the stack (at the end of the current frame)  Automatically freed when the function returns  You need to #include

 Layout for 32-bit architecture  Could only address 4GB  Modern layouts often have random offsets for stack, heap, and memory mapping for security reasons Text Data BSS Heap Memory Mapping Stack Kernel Space 1GB 3GB 0xc x x x Only for Linux kernel Memory for function calls Addresses for memory mapped files Dynamically allocated data Uninitialized globals Initialized globals Program code

 The Linux machines in this lab use 64-bit processors with 64-bit versions of Ubuntu  Our version of gcc supports 64-bit operations  Our pointers are actually 8 bytes in size  But 64-bit stuff is confusing  They're still working out where the eventual standard will be  64-bit addressing allows 16,777,216 terabytes of memory to be addressed (which is far beyond what anyone needs)  Current implementations only use 48 bits  User space (text up through stack) gets low 128 terabytes  Kernel space gets the high 128 terabytes

#include int global = 10; int main() { int stack = 5; int* heap = (int*)malloc(sizeof(int)*100); printf("Stack: %p\n", &stack); printf("Heap: %p\n", heap); printf("Global: %p\n", &global); printf("Text: %p\n", main); return 0; } #include int global = 10; int main() { int stack = 5; int* heap = (int*)malloc(sizeof(int)*100); printf("Stack: %p\n", &stack); printf("Heap: %p\n", heap); printf("Global: %p\n", &global); printf("Text: %p\n", main); return 0; }

 Software engineering

 Keep working on Project 3  Have a good Spring Break!