C – Scope, Lifetime, and the Stack

Slides:



Advertisements
Similar presentations
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Advertisements

1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
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.
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 Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
CS 536 Spring Run-time organization Lecture 19.
Intro to Computer Architecture
Run time vs. Compile time
Semantics of Calls and Returns
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
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 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Buffer overflows.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
10/16/2015IT 3271 All about binding n Variables are bound (dynamically) to values n values must be stored somewhere in the memory. Memory Locations for.
Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CS 155 Section 1 PP1 Eu-Jin Goh. Setting up Environment Demo.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
Int main( ) { x = a(); } int a() { y = b(); } int b() { z = c(); } int c() { } 1.
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.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Object Lifetime and Pointers
Chapter 14 Functions.
CS/COE 0449 (term 2174) Jarrett Billingsley
Overview 4 major memory segments Key differences from Java stack
CSE 374 Programming Concepts & Tools
Functions and the Stack
Function Calls in MIPS To call a function: jal func
© Craig Zilles (adapted from slides by Howard Huang)
Run-time organization
Checking Memory Management
Pointers and Dynamic Variables
Chapter 9 :: Subroutines and Control Abstraction
Functions Inputs Output
CS/COE 0449 Jarrett Billingsley
Activation Records and Function Calls
Overview 4 major memory segments Key differences from Java stack
CS 0449 Jarrett Billingsley
Pointers, Dynamic Data, and Reference Types
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Memory Allocation CS 217.
Understanding Program Address Space
Given the code to the left:
More Functions and the Stack
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Arrays an array of 5 ints is filled with 3,2,4,1,7
Conditionals and Functions
Where is all the knowledge we lost with information? T. S. Eliot
© Craig Zilles (adapted from slides by Howard Huang)
Pointers, Dynamic Data, and Reference Types
Topic 2b ISA Support for High-Level Languages
SPL – PS2 C++ Memory Handling.
CS/COE 0449 Jarrett Billingsley
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

C – Scope, Lifetime, and the Stack CS/COE 0449 Jarrett Billingsley

Class announcements project 2 due date has been pushed back a week to 10/12 since I'm swapping the gdb/malloc projects this term so now malloc is project 2 so you need more time heehee CS449

Scope and Lifetime CS449

Scope one.c two.c scope is "where a name can be seen" C has three levels of scope globals can be seen by any function in any file one.c two.c static globals can be seen by any function in one file locals can be seen by one function int main() { int x; } - "name" in this case refers to variables, but can also apply to functions in C, and other things in other languages. - we'll talk about static globals a little more when we talk about multi-file development CS449

Global variables are terrible and you should almost never use them. almost any problem where you think you need one can be instead solved by using a local and passing by reference. there are legitimate uses for them, but… unless you are being forced to use them… avoid them. - this goes for statics in Java too! CS449

Lifetime every variable takes up space in memory that memory must be allocated: reserved for the variable and when no longer needed, deallocated: released for other use lifetime is a little more subtle than scope... it's the time between allocation and deallocation global variables last from program start to program exit local variables only last as long as the enclosing function - both kinds of globals do. - lifetime and scope are not the same thing, although the lifetimes and scopes of locals and globals overlap. CS449

You're watching the Lifetime Channel int glob = 0x910B; void main() { int m = 10; func(); } void func() { int f = 10; } lifetime of glob lifetime of m lifetime of f (starting) main running func running (exiting) time CS449

Ownership ownership answers the question: who is responsible for deallocating a piece of memory? or: how do we determine when it's okay to deallocate memory? different languages deal with this in different ways. C mostly sticks its fingers in its ears, goes "LA LA LA," and pretends the problem doesn't exist. locals and globals are easy: locals are owned by functions globals are owned by the program but… - hey, C's a product of its time, and to be fair, ownership is a really tricky concept to get right! - Rust seems to be doing a pretty good job of it, though. CS449

If only it were that simple in this Java code: what is the scope of each variable? what is the object's lifetime? it starts in func… but ends… ……where??????? this is terrifying and we'll talk more about it next class public static void main(String[] args) { String s = func(); s = null; } String func() { String t = new String("hello"); return t; - we can't get around this. - global/local lifetimes are just not flexible/powerful enough for many tasks. - unfortunately, beyond globals/locals, C totally screws it all up, oops CS449

The Stack!!! CS449

caller callee The flow of control void fork() { knife(); spoon++; } when the caller calls a function, where do we go? when the callee's code is finished, where do we go? void fork() { knife(); spoon++; } void knife() { spork++; spatula--; } caller callee - it doesn't matter if you use a return or not, when the callee finishes, it goes back to the line after the call in the caller. CS449

What's the stack? Stack Memory it's an area of memory provided to your program by the OS when your program starts, it's already there the stack holds information about function calls. it's not a strict stack you can read and write any part of it but it grows and shrinks like a stack and we use "push" and "pop" to describe that each program* gets one stack cause only one function is running at a time Memory Stack * really it's each thread but that's not until the end of the semester CS449

Activation records (ARs) when a function is called, a bunch of data is pushed onto the stack this is the call's activation record (or "stack frame") it contains local variables (including arguments) and the return address Stack main's caller return address x y buf[5] garbage int main() { int x, y; char buf[5]; return 0; } main's AR - somebody calls main. there's actually a few functions which run before main gets a chance. - the return address is where to go in the caller, when this function returns. CS449

The low-level layout int x; char arr[3]; short y; Addr Value d02f d02e d02d d02c return d02b d02a d029 d028 d027 d026 y d025 d024 d023 d022 d021 d020 x d01f d01e d01d arr d01c d01b d01a d019 d018 each variable (including array variables) gets enough bytes in the AR to hold its value where the variable is located is up to the compiler when I compiled this on thoth, this is the arrangement I got: sizeof(int) == 4, so x gets 4 bytes sizeof(arr) == 3, so it gets 3 bytes sizezof(short) == 2, so.. yeah but what's all that grey space? idk lol, compiler does what it wants int x; char arr[3]; short y; - d028 is actually the saved ebp but we won't talk about that (yet) - I think the compiler is making the stack frame 16 bytes - but I don't know why, cause this is not a system that requires 16-byte stack pointer alignment - and this stack frame is not even aligned to 16 bytes - and I don't know why it chose to put arr right before x instead of down at d018 - THE COMPILER DOES SHIT, OKAY? CS449

Call = push, return = pop (animated) the stack grows when we call a function and shrinks when it exits main's caller return address x main int main() { int x; fork(&x); return 0; } void fork(int* ptr) { *ptr = knife(); int knife() { return 10; fork return address ptr knife return address CS449

Recursive functions (animated) recursive functions work by using the call stack as an implicit stack data structure fact(5)'s caller return address x = 5 fact(5) int fact(int x) { if(x <= 2) { return x; } else { return x*fact(x–1); } } fact(4) return address x = 4 fact(3) return address x = 3 fact(2) return address x = 2 - this is why anything recursion can do, iteration can do: - recursion uses the call stack to store its data - iteration can use a separate stack data structure to store the same data CS449

But they don't really go away. (animated) the stack is used constantly so it needs to be really fast so we implement it as a pointer the stack pointer (sp) main's caller return address argc = 1 argv = ... garbage sp address 10 25 garbage return address a = 10 b = 25 c = 10905928 garbage return address x = 10 y = 25 garbage push AR: sp -= (size of AR) pop AR: sp += (size of AR) but the AR's memory is still there. so if we call another function… this is where that garbage in uninitialized variables comes from! - this is why 1_variable_weirdness.c behaves the way it does - BUT YOU CANNOT COUNT ON THIS BEHAVIOR. YOU CAN'T USE THIS TO DO STUFF. CS449

Don't return stack arrays. I think I showed this before "function returns address of local variable" when func() returns, what happened to its variables? you have no idea you now have an invalid pointer it points to who-knows-what it might crash it might expose secrets who knows!! int main() { char* str = func(); printf("%s\n", str); return 0; } char* func() { char str[10] = "hi there"; char* dummy = str; return dummy; - 2_return_array_fail.c does something like this. CS449