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 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
Run-Time Storage Organization
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
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Memory Layout C and Data Structures Baojian Hua
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-
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Outline Midterm results Static variables Memory model
Runtime Environments Compiler Construction Chapter 7.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
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
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.
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
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
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.
Object Lifetime and Pointers
Day 03 Introduction to C.
Process Memory COMP 40: Machine Structure and
Run-time organization
Day 03 Introduction to C.
Checking Memory Management
CSC 253 Lecture 8.
Lecture 4: Process Memory Layout
Storage.
CSC 253 Lecture 8.
Dynamic Memory A whole heap of fun….
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory A whole heap of fun….
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
Name Binding and Object Lifetimes
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Run-time environments
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

C and Data Structures Baojian Hua bjhua@ustc.edu.cn Memory Layout C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Goals of Today’s Lecture Behind the scenes of running a program Code, executable, and process Memory layout for UNIX processes, and relationship to C Explicit memory management in C malloc: allocate memory from the heap free: deallocate memory from the heap

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

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

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

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

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

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; text data bss 0xffffffff

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

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 text data bss heap stack 0xffffffff

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

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

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

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

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

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

Explicit Memory Management Heap management in C is explicit “malloc” to request a region “free” to recycle a region It’s the programmers’ responsibility to make sure that such a sequence of action is safe

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

Example int main() { int *p; p = malloc (4); *p = 99; return 0; } text text data bss heap #@%*& p stack 0xffffffff

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

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

Example int main() { int *p; p = malloc (4); *p = 99; int *q; q = p; printf (“%d\n”, *q); return 0; } text data bss heap 99 q p stack 0xffffffff

Example int main() { int *p; p = malloc (4); *p = 99; int *q; q = p; printf (“%d\n”, *q); free (q); return 0; } text data bss heap q p stack 0xffffffff

Dangling Pointer Reference int main() { int *p, *q; q = p = (int *) malloc (4); free (q); *p; return 0; }

Dangling Dereference int main() { int *p, *q; q = p = (int *) malloc (4); free (q); *p; return 0; } text data bss heap #@%*& p q stack 0xffffffff

Dangling Dereference int main() { int *p, *q; q = p = (int *) malloc (4); free (q); *p; return 0; } text data bss heap p q stack 0xffffffff

Dangling Dereference int main() { int *p, *q; q = p = (int *) malloc (4); free (q); *p; // no this memory!!! return 0; } text data bss heap p q stack 0xffffffff

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

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

Summary Dangling pointers and memory leak 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