CSE 333 Lecture 2 Memory

Similar documents
C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Link 4. Relocation. Young W. Lim Wed. Young W. Lim Link 4. Relocation Wed 1 / 22

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

EL2310 Scientific Programming

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CMPSC 311- Introduction to Systems Programming Module: Introduction to C

Lecture 03 Bits, Bytes and Data Types

CSC 1600 Memory Layout for Unix Processes"

Deep C by Olve Maudal

Fundamentals of Programming

Kurt Schmidt. October 30, 2018

Understanding Pointers

Computers Programming Course 5. Iulian Năstac

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Final CSE 131B Spring 2004

CSE 333 Lecture 7 - final C details

CS 0449 Sample Midterm

Chapter 2. Procedural Programming

C Introduction. Comparison w/ Java, Memory Model, and Pointers

High Performance Programming Programming in C part 1

Lecture 3: C Programm

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Informatica e Sistemi in Tempo Reale

C programming basics T3-1 -

CSE 374 Programming Concepts & Tools

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Link 4. Relocation. Young W. Lim Thr. Young W. Lim Link 4. Relocation Thr 1 / 26

Variables and literals

EL2310 Scientific Programming

Link 3. Symbols. Young W. Lim Mon. Young W. Lim Link 3. Symbols Mon 1 / 42

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Memory Allocation in C

Systems Programming and Computer Architecture ( )

Contents of Lecture 3

CS Programming In C

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Arrays and Memory Management

CSE 333 Midterm Exam July 24, Name UW ID#

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

PRINCIPLES OF OPERATING SYSTEMS

Class Information ANNOUCEMENTS

Arrays and Pointers in C. Alan L. Cox

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Dynamic Data Structures. CSCI 112: Programming in C

Lecture 2: C Programming Basic

Data types, arrays, pointer, memory storage classes, function call

Contents. A Review of C language. Visual C Visual C++ 6.0

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

CSE 333 Lecture 2 - gentle re-introduction to C

CSE 333 Lecture 2 - arrays, memory, pointers

Reminder of midterm 1. Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations

CSE 333 Lecture C++ final details, networks

CSE 333 Lecture 3 - arrays, memory, pointers

CS61, Fall 2012 Section 2 Notes

Work relative to other classes

CPSC 427: Object-Oriented Programming

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

5.Coding for 64-Bit Programs

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CSE 333 Autumn 2013 Midterm

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

Intermediate Programming, Spring 2017*

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

A brief introduction to C programming for Java programmers

In Java we have the keyword null, which is the value of an uninitialized reference type

FUNCTIONS POINTERS. Pointers. Functions

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Principles of C and Memory Management

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Programming in C. Pointers and Arrays

A Fast Review of C Essentials Part I

Fundamental of Programming (C)

Introduction to Programming Block Tutorial C/C++

Dynamic Memory Allocation and Command-line Arguments

Lecture 2: C Programm

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

EL2310 Scientific Programming

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Basic Types and Formatted I/O

CSE 333 Midterm Exam 7/29/13

Hacking in C. The C programming language. Radboud University, Nijmegen, The Netherlands. Spring 2018

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Functions. Systems Programming Concepts

C Programming Review CSC 4320/6320

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

ch = argv[i][++j]; /* why does ++j but j++ does not? */

CSCI 171 Chapter Outlines

CS429: Computer Organization and Architecture

Transcription:

CSE 333 Lecture 2 Memory John Zahorjan Department of Computer Science & Engineering University of Washington

Today s goals - some terminology - review of memory resources - reserving memory - type checking

Some Terminology virtual address space A set of addresses naming values, usually related to physical memory addresses through address translation stack, heap Software managed regions of main memory distinguished by how memory is allocated and free d identifier A programmer assigned name in a program, e.g., main or total

Some Terminology declaration Creating an identifier (and often giving it a type), e.g., int x; void foo(int y); definition Declaring a name and giving it substance for functions, giving code; for variables, causing a decision about allocating space to be made int x; void foo(int y) { use The use of an identifier (not a declaration or definition) x = 2; foo(6);

Some Terminology binding Associating an identifier with a memory address lifetime The time between the allocation of space for the thing named by an identifier and when that space may be reallocated for other purposes in C, locals live for the duration of the enclosing scope in C, globals live from program load time to program termination scope The region of code over which an identifier has the same binding

Example int check(char *candidatepassword) { return 0; int main(int argc, char *argv[]) { char password[7]; for (password[0]='a'; password[0]<='z'; password[0]++) { for (password[1]='a'; password[1]<='z'; password[1]++) { check(password) return 0; check and main are global password is local

Review of memory resources OS This model is supported by the compiler, the linker, the loader, and the OS Virtual address space stack heap global data static data code segment sp pc

Reserving memory: basic rules Identifier definitions cause memory to be reserved A function body reserves the required amount of text segment memory A local variable definition reserves space in the stack frame A global variable definition reserves space in the global data area To generate code for uses of identifiers, the compiler must have some idea how to access the memory they name Where, exactly, is it? The compiler knows exactly where local are (relative to the sp). It doesn t know exactly where globals will be. - That isn t known until link time

When are exact addresses assigned? Code - the linker collects all code and lays it out in the text segment Local variables - the compiler knows (a) the stack frame layout, and (b) that the stack pointer register points at the stack Global data area - the linker collects all globals and puts them in some order Static data area - the linker collects all literals that require storage and puts them in some order

Example int globalx; char *globalstring; void sub(int x) { return x; OS main sub sp int main(int argc, char *argv[]) { char *prompt = literal ; int y = sub(2); return y; globalx globalstring literal sub compiler generates code main moving sp to allocate space for prompt and y on entry to main Compiler generates instructions that move sp to allocate space for x in sub (and assigns it value 2)

Example uses int globalx; char *globalstring; int sub(int x) { return x; Compiler understands stack frame layout, so knows how to reference local variables (as offset from sp) int main(int argc, char *argv[]) { char *prompt = str ; int y = sub(2); return y; Compiler can t know where sub will be located, so leaves an annotation for linker to patch instructions

Symbols in object files main.c separate $ gcc -std=c11 -c linkermain.c $ objdump -t linkermain.o linkermain.o: file format elf64-x86-64 SYMBOL TABLE: 0000000000000000 l df *ABS* 0000000000000000 linkermain.c 0000000000000000 l d.text 0000000000000000.text 0000000000000000 l d.data 0000000000000000.data 0000000000000000 l d.bss 0000000000000000.bss 0000000000000000 l d.rodata 0000000000000000.rodata 0000000000000000 l d.note.gnu-stack 0000000000000000.note.GNU-stack 0000000000000000 l d.eh_frame 0000000000000000.eh_frame 0000000000000000 l d.comment 0000000000000000.comment 0000000000000004 O *COM* 0000000000000004 globalx 0000000000000008 O *COM* 0000000000000008 globalstring 0000000000000000 g F.text 0000000000000029 main 0000000000000000 *UND* 0000000000000000 sub

Symbols in object files sub.c separate $ gcc -std=c11 -c linkersub.c $ objdump -t linkersub.o linkersub.o: file format elf64-x86-64 SYMBOL TABLE: 0000000000000000 l df *ABS* 0000000000000000 linkersub.c 0000000000000000 l d.text 0000000000000000.text 0000000000000000 l d.data 0000000000000000.data 0000000000000000 l d.bss 0000000000000000.bss 0000000000000000 l d.note.gnu-stack 0000000000000000.note.GNU-stack 0000000000000000 l d.eh_frame 0000000000000000.eh_frame 0000000000000000 l d.comment 0000000000000000.comment 0000000000000004 O *COM* 0000000000000004 globalx 0000000000000008 O *COM* 0000000000000008 globalstring 0000000000000000 g F.text 000000000000000c sub

Symbols in object filesmain/sub one file $ gcc -std=c11 -c linkermainsub.c $ objdump -t linkermainsub.o linkersub.o: file format elf64-x86-64 SYMBOL TABLE: 0000000000000000 l df *ABS* 0000000000000000 linkersub.c 0000000000000000 l d.text 0000000000000000.text 0000000000000000 l d.data 0000000000000000.data 0000000000000000 l d.bss 0000000000000000.bss 0000000000000000 l d.note.gnu-stack 0000000000000000.note.GNU-stack 0000000000000000 l d.eh_frame 0000000000000000.eh_frame 0000000000000000 l d.comment 0000000000000000.comment 0000000000000004 O *COM* 0000000000000004 globalx 0000000000000008 O *COM* 0000000000000008 globalstring 0000000000000000 g F.text 000000000000000c sub 000000000000000c g F.text 0000000000000029 main

Types: Why? int x; int Y[10]; char *str; What is the point of the type specification in each declaration? It indicates how much space to reserve It allows some type checking of operations x Y str

Types: Sizes sizeof #include <stdio.h> void sub(char s[]) { printf("sizeof(s) = %lu\n", sizeof(s)); // 8 int main(int argc, char *argv[]) { char *pstring = "literal string"; char string[] = "literal string"; char string20[20] = "literal string"; printf("sizeof(int) = %lu\n", sizeof(int)); // 4 printf("sizeof(argc) = %lu\n", sizeof(argc)); // 4 printf("sizeof(pstring) = %lu\n", sizeof(pstring)); // 8 printf("sizeof(string) = %lu\n", sizeof(string)); // 15 printf("sizeof(string20) = %lu\n", sizeof(string20)); // 20 sub(string); return 0;

Types: Sizes pointers What does this print? char stringarray[] = example string ; char *pstring = stringarray; printf( %lu\n, sizeof(pstring));

Primitive types in C typical sizes integer types - char, int floating point - float, double modifiers - short [int] - long [int, double] - signed [char, int] - unsigned [char, int] type bytes (32 bit) bytes (64 bit) 32 bit range printf char 1 1 [0, 255] %c short int 2 2 [-32768,32767] %hd unsigned short int 2 2 [0, 65535] %hu int 4 4 [-214748648, 2147483647] %d unsigned int 4 4 [0, 4294967295] %u long int 4 8 long long int 8 8 [-2147483648, 2147483647] %ld [-9223372036854775808, 9223372036854775807] %lld float 4 4 approx [10-38, 10 38 ] %f double 8 8 approx [10-308, 10 308 ] %lf long double 12 16 approx [10-4932, 10 4932 ] %Lf pointer 4 8 [0, 4294967295] %p

C99 extended integer types Portable, known size integers #include <stdint.h> void foo(void) { int8_t w; // exactly 8 bits, signed int16_t x; // exactly 16 bits, signed int32_t y; // exactly 32 bits, signed int64_t z; // exactly 64 bits, signed uint8_t a;...etc. // exactly 8 bits, unsigned

Types: Type Checking Type information is used to determine which version of an operator to apply: long int x; float y; struct { int a; int b; z; x = y; y = x + y; x = y + x; x = z; // error

Types: Explicit conversion casting Type information is used to determine which version of an operator to apply: long int x; float y; struct { int a; int b; z; x = y; y = x + y; x = y + x; x = (long int)z; // error x = *(long int*)&z; // okay!

Types: Implicit Conversions While it s best to explicitly cast, C will implicitly convert operands so that it can successfully apply operators Hierarchy of types char < short < int < long < long long < float < double < long double Integer promotion If operand is of rank lower than int, promote to int

Types: Implicit Conversions Usual arithmetic conversions If either operand is a floating point type, convert operand of lower rank to same rank as floating type If both are integer types, use integer promotion (promote to integer, and if that doesn t work try unsigned int, and ) Note: conversion from int to float can lose precision!

Arithmetic Conversions roundoff error #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int x = 123456789; float y; y = x; x = y; printf("%d\n", x); return EXIT_SUCCESS; This compiles with no warnings It prints 123456792

Non-arithmetic conversions Arrays are converted to pointer to the base type, except when the array is the operand of sizeof() the array is the operand of the address operator & when a string literal is used to initialize an array of char Function names are implicitly converted to a pointer to a function except when used as operand of the address operator &

Implicit Pointer Conversion Any pointer can be implicitly converted to void* And vice versa A pointer of a given type can be converted to a pointer of a more qualified type int* to const int* A null pointer constant (NULL) can be converted to any pointer type

Implicit Pointer Conversion #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { void *p = NULL; int i; int *pi; const int *cpi; // none of the statements generate errors or warnings pi = &i; cpi = pi; pi = p; pi = malloc(sizeof(int)); if ( p == cpi ) printf("equal\n"); else printf("not equal\n"); return EXIT_SUCCESS;

Explicit Pointer Conversion You can cast any pointer type to any other type It s guaranteed that if you eventually cast it back to the original pointer type, it has the same value p == (int*)(float*)(char*)p is always true

Conversions: When? #include <stdio.h> #include <stdlib.h> double sub(int x) { return x; // conversion to return type int main(int argc, char *argv[]) { int i = 2.0; // conversion on initialization double d = 2.0; d = i + d; // conversion on operator (+): i -> double i = sub(d); // conversion of argument on call // conversion of result on assignment printf("%d\n", i);// prints 4 return EXIT_SUCCESS;

Conversions: Performance Sit in a loop calling passing a double argument to either double sub(double x){return x; or int sub(double x){return x; The version that doesn t have to convert is about 25% faster than the version that does.

Revisiting Declarations scope segment An identifier declared outside of any code block has global scope An identifier declared within a block has scope that is the rest of that block int globalx; int main(int argc, char *argv[]) { for (int i=0; i<10; i++) { int y = i; return 0;

Scope example int globalx; int main(int argc, char *argv[]) { for (int i=0; i<10; i++) { int y = i; printf("y = %d\n", y); for (i=0; i<4; i++) { // error y += i; // error return EXIT_SUCCESS; int globalx; float globalx; int globalx() { return 2; // okay // error // error

The Keyword static globals Applied to a global, it means the identifier has file scope it has meaning only within the rest of that file that identifier will not be exported to the linker, so there s no way to use it in some other file static int globalx; static void privatefunc(char *str) {

The Keyword static locals Applied to a local, it means reserve memory in the global data segment, not on the stack the scope is still local but a new copy of the variable is not created each time the block is entered void sub() { static int counter = 4; printf( %d, counter); if ( counter-- > 0 ) sub(); When invoked the first time print 4 3 2 1 0 When invoked the second time prints -1S

For next time There is a new exercise out, due Monday The first project assignment is out Next time: the preprocessor pointers: arrays, strings, functions