Memory Layout C and Data Structures Baojian Hua

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

Algorithms and data structures
Dynamic Memory Management
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
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.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
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:
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
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’;
David Notkin Autumn 2009 CSE303 Lecture 10 "If it weren't for C, we'd be writing programs in BASI, PASAL, and OBOL."
Chapter 10 Storage management
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.
Run-Time Storage Organization
C and Data Structures Baojian Hua
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
Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Web:
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Runtime Environments Compiler Construction Chapter 7.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
Basic Semantics Associating meaning with language entities.
Topic 2d High-Level languages and Systems Software
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.
C++ Memory Overview 4 major memory segments Key differences from Java
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.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
COMP3190: Principle of Programming Languages
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
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.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
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.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Object Lifetime and Pointers
Day 03 Introduction to C.
Process Memory COMP 40: Machine Structure and
Introduction to Programming
Run-time organization
Day 03 Introduction to C.
Checking Memory Management
Lecture 4: Process Memory Layout
Storage.
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory A whole heap of fun….
Chien-Chung Shen CIS/UD
Programming Languages
C (and C++) Pointers April 4, 2019.
Program Execution in Linux
Dynamic Memory A whole heap of fun….
Dynamic Memory.
CSE 303 Lecture 10 C memory model; stack allocation
CS703 - Advanced Operating Systems
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Run-time environments
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

Memory Layout C and Data Structures Baojian Hua

Goals of Today ’ s Lecture Behind the scenes of running a program Code, executable, and process Memory layout for Linux processes, and relationship to C Explicit memory management in C void *malloc (int bytes); allocate memory from the heap free : deallocate memory from the heap

From C Code to Process C source files.c;.h Binary files.o Executables a.out Process Managed by OS binary files C source code process compiling running executable linking

Main Memory CPU Memory Disk Network Video Audio Data Bus shared by all processes

Virtual Memory Continuous memory space for all process each with its physical space pretends you the same virtual space 0xffffffff 0

Organization of Virtual Memory:.text Program code and constant binary form loaded libraries 0xffffffff 0 text

Organization of Virtual Memory:.text Program code and constant binary form loaded libraries known as “ text ” segment space calculated at compile- time 0xffffffff 0 text

Organization of Virtual Memory:.data Data: initialized global data in the program Ex: int size = 100; BSS: un-initialized global data in the program Ex: int length; 0xffffffff 0 text data bss

Organization of Virtual Memory: heap Heap: dynamically- allocated spaces Ex: malloc, free OS knows nothing about it space content dynamically grows as program runs 0xffffffff 0 text data bss heap

Organization of Virtual Memory: stack Stack: local variables in functions we ’ ll discuss stack soon support function call/return and recursive functions grow to low address 0xffffffff 0 text data bss heap stack

Summary text: program text data: initialized globals & static data bss: un-initialized globals & static data heap: dynamically managed memory stack: function local variables 0xffffffff 0 text data bss heap stack

Example char *string = “hello”; int iSize; char *f (int x) { char *p; iSize = 8; p = (char *)malloc (iSize); return p; } 0xffffffff 0 text data bss heap stack

Example char *string = “hello”; int iSize; char *f (int x) { char *p; iSize = 8; p = (char *)malloc (iSize); return p; } 0xffffffff 0 text data bss heap stack

Variable Lifetime text: program startup program finish data, bss: program startup program finish heap: dynamically allocated de-allocated (free) stack: function call function return 0xffffffff 0 text data bss heap stack

Example char *string = “hello”; int iSize; char *f (int x) { char* p; iSize = 8; p = (char *)malloc (iSize); return p; } 0xffffffff 0 text data bss heap stack program startup when f() is called live after allocation; till free() or program finish

Variable Initialization text: readonly data on program startup bss: un-initialized (though some systems initialize with 0) heap: un-initialized stack: un-initialized 0xffffffff 0 text data bss heap stack

Explicit Memory Management Heap management in C is explicit void *malloc (int bytes); free (void *p); It ’ s the programmers ’ responsibility to make sure that such a sequence of action is safe

Example int main() { int *p; p = (int *)malloc (sizeof (*p)); *p = 99; return 0; } 0xffffffff 0 text data bss heap stack p

Example int main() { int *p; p = (int *)malloc (sizeof (*p)); *p = 99; return 0; } 0xffffffff 0 text data bss heap stack p

Example int main() { int *p; p = (int *)malloc (sizeof (*p)); *p = 99; return 0; } 0xffffffff 0 text data bss heap stack 99 p

Aliasing int main() { int *p, *q; p = (int *)malloc (sizeof (*p)); *p = 99; q = p; return 0; } 0xffffffff 0 text data bss heap stack 99 p q

Aliasing int main() { int *p, *q; p = (int *)malloc (sizeof (*p)); *p = 99; q = p; *q = 88; return 0; } 0xffffffff 0 text data bss heap stack 88 p q

Aliasing int main() { int *p, *q; p = (int *)malloc (sizeof (*p)); *p = 99; q = p; *q = 88; free (q); return 0; } 0xffffffff 0 text data bss heap stack $%#^& p q

Dangling Reference int main() { int *p, *q; p = (int *)malloc (sizeof (*p)); *p = 99; q = p; *q = 88; free (q); *p = 77; return 0; } 0xffffffff 0 text data bss heap stack $%#^& p q

Memory Leaking int main() { int *p; p = (int *)malloc (sizeof (*p)); // make the above space unreachable p = (int *)malloc (sizeof (*p)); // even worse… while (1) p = (int *)malloc (sizeof (*p)); return 0; }

Memory Leaking void f (); void f () { int *p; p = (int *)malloc (sizeof (*p)); return; } int main () { f (); return 0; }

Summary Dangling pointers and memory leaking are evil sources of bugs: hard to debug may fire after a long time of run may far from the bug point hard to prevent especially by using the static methods Part of the reasons for the popularity of garbage collection