CPSC 213. C vs. Java. Introduction to Computer Systems. Reading. Java Hello World... C Hello World... C, Pointers, and Dynamic Allocation.



Similar documents
Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Semantic Analysis: Types and Type Checking

Leak Check Version 2.1 for Linux TM

Crash Course in Java

Jonathan Worthington Scarborough Linux User Group

CSC230 Getting Starting in C. Tyler Bletsch

ECS 165B: Database System Implementa6on Lecture 2

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

The C Programming Language course syllabus associate level

The Advantages of Dan Grossman CSE303 Spring 2005, Lecture 25

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

How To Write Portable Programs In C

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Introduction to Java

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

CS 106 Introduction to Computer Science I

Chapter 5 Names, Bindings, Type Checking, and Scopes

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

public static void main(string[] args) { System.out.println("hello, world"); } }

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Get the Better of Memory Leaks with Valgrind Whitepaper

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

X86-64 Architecture Guide

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Object Oriented Software Design II

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

The programming language C. sws1 1

Chapter 7D The Java Virtual Machine

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Programming Embedded Systems

Java Crash Course Part I

5 Arrays and Pointers

Illustration 1: Diagram of program function and data flow

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Getting Started with the Internet Communications Engine

Hacking Techniques & Intrusion Detection. Ali Al-Shemery arabnix [at] gmail

An Incomplete C++ Primer. University of Wyoming MA 5310

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Moving from CS 61A Scheme to CS 61B Java

Lecture 22: C Programming 4 Embedded Systems

Chapter 2: Elements of Java

Introduction to Data Structures

Introduction to Programming System Design. CSCI 455x (4 Units)

Programmierpraktikum

C++FA 3.1 OPTIMIZING C++

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

Class Notes for CSCI 104: Data Structures and Object-Oriented Design

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Purify User s Guide. Version 4.1 support@rational.com

Next, the driver runs the C compiler (cc1), which translates main.i into an ASCII assembly language file main.s.

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Computer Programming. Course Details An Introduction to Computational Tools. Prof. Mauro Gaspari:

Object Oriented Software Design II

Applying Clang Static Analyzer to Linux Kernel

MISRA-C:2012 Standards Model Summary for C / C++

1 Abstract Data Types Information Hiding

Sources: On the Web: Slides will be available on:

6.s096. Introduction to C and C++

C Interview Questions

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Virtual Servers. Virtual machines. Virtualization. Design of IBM s VM. Virtual machine systems can give everyone the OS (and hardware) that they want.

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

System Calls Related to File Manipulation

Java from a C perspective. Plan

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

1 The Java Virtual Machine

Input/Output Subsystem in Singularity Operating System

Building Embedded Systems

Bypassing Browser Memory Protections in Windows Vista

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

Computer Programming Tutorial

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

LINKED DATA STRUCTURES

Lesson 06: Basics of Software Development (W02D2

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

Implementation Aspects of OO-Languages

Tutorial: Getting Started

Lab Experience 17. Programming Language Translation

Java Interview Questions and Answers

The AVR Microcontroller and C Compiler Co-Design Dr. Gaute Myklebust ATMEL Corporation ATMEL Development Center, Trondheim, Norway

CS61: Systems Programing and Machine Organization

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

1001ICT Introduction To Programming Lecture Notes

Stacks. Linear data structures

C# and Other Languages

Programmation 2. Introduction à la programmation Java

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Transcription:

Reading CPSC 213 Textbook New to C, Understanding Pointers, The malloc and free Functions, Why Dynamic Memory Allocation 2ed: "New to C" sidebar of 34, 310, 991-992 1ed: "New to C" sidebar of 34, 311,1091-1092 Introduction to Computer Systems Unit 1f C, Pointers, and Dynamic Allocation 1 Java Hello World 2 C vs Java import javaio*; public class HelloWorld { public static void main (String[] args) { Systemoutprintln("Hello world"); C Hello World #include <stdioh> main() { printf("hello world\n"); 3 4

Java Syntax vs C Syntax source files source files java is source file c is source file including packages in source import javaio* printing Systemoutprintln("blah blah"); compile and run javac foojava java foo at command line (Linux, Windows, Mac) edit, compile, run, debug (IDE) Eclipse h is header file including headers in source #include <stdioh> printing printf("blah blah\n"); compile and run gcc -g -o foo fooc /foo at Unix command line shell prompt (Linux, Mac Terminal, Sparc, Cygwin on Windows) debug gdb foo Pointers in C 5 6 New in C: Pointers C and Java Arrays and Pointers pointers: addresses in memory 0x00000000 In both languages locations are first-class citizens in C can go back and forth between location and value! pointer declaration: <type>* int* b; // b is a POINTER to an INT 0x00000001 0x00000002 0x00000003 0x00000004 0x00000005 an array is a list of items of the same type array elements are named by non-negative integers start with 0 syntax for accessing element i of array b is b[i] In Java getting address of object: & 0x00000006 variable a stores a pointer to the array int a; // a is an INT int* b = &a; // b is a pointer to a de-referencing pointer: * a = 10; // assign the value 10 to a *b = 10; // assign the value 10 to a type casting is not typesafe 0x3e47ad40 0x3e47ad41 0x3e47ad42 b[x] = 0 means m[m[b] + x * sizeof(array-element)] 0 In C variable a can store a pointer to the array or the array itself b[x] = 0 meansm[b + x * sizeof(array-element)] 0 or m[m[b] + x * sizeof(array-element)] 0 dynamic arrays are just like all other pointers char a[4]; *((int*) a) = 1; // a 4 byte array // treat those four bytes as an INT 0xffffffff - stored in TYPE* - access with either a[x] or *(a+x) 7 8

Example Example The following two C programs are identical The following two C programs are identical a[4] = 5; *(a+4) = 5; a[4] = 5; *(a+4) = 5; For array access, the compiler would generate this code For array access, the compiler would generate this code r[0] a r[1] 4 r[2] 5 m[r[0]+4*r[1]] r[2] ld $a, r0 ld $4, r1 ld $5, r2 st r2, (r0,r1,4) r[0] a r[1] 4 r[2] 5 m[r[0]+4*r[1]] r[2] ld $a, r0 ld $4, r1 ld $5, r2 st r2, (r0,r1,4) multiplying the index 4 by 4 (size of integer) to compute the array offset So, what does this tell you about pointer arithmetic in C? multiplying the index 4 by 4 (size of integer) to compute the array offset So, what does this tell you about pointer arithmetic in C? Adding X to a pointer of type Y*, adds X * sizeof(y) to the pointer s memory-address value Pointer Arithmetic in C 9 Question (from S3-C-pointer-mathc) 9 Its purpose int *c; an alternative way to access dynamic arrays to the a[i] Adding or subtracting an integer index to a pointer results in a new pointer of the same type value of the pointer is offset by index times size of pointer s referent for example - adding 3 to an int* yields a pointer value 12 larger than the original Subtracting two pointers of the same type results in an integer // c = (int *) malloc (10*sizeof(int)); // c = &c[3]; *c = *&c[3]; // What is the equivalent Java statement to gives number of referent-type elements between the two pointers for example - (& a[7]) - (& a[2])) == 5 == (a+7) - (a+2) other operators & X the address of X [A] c[0] = c[3]; [B] c[3] = c[6]; [C] there is no typesafe equivalent [D] not valid, because you can t take the address of a static in Java * X the value X points to 10 11

Looking more closely Looking more closely c = &c[3]; *c = *&c[3]; r[0] 0x2000 # r[0] = &c r[1] m[r[0]] # r[1] = c r[2] 12 # r[2] = 3 * sizeof(int) r[2] r[2]+r[1] # r[2] = c + 3 m[r[0]] r[2] # c = c + 3 r[3] 3 # r[3] = 3 r[4] m[r[2]+4*r[3]] # r[4] = c[3] m[r[2]] r[4] # c[0] = c[3] c = &c[3]; *c = *&c[3]; r[0] 0x2000 # r[0] = &c r[1] m[r[0]] # r[1] = c r[2] 12 # r[2] = 3 * sizeof(int) r[2] r[2]+r[1] # r[2] = c + 3 m[r[0]] r[2] # c = c + 3 r[3] 3 # r[3] = 3 r[4] m[r[2]+4*r[3]] # r[4] = c[3] m[r[2]] r[4] # c[0] = c[3] Before 0x2000: 0x3000 0x3000: 0 0x3004: 1 0x3008: 2 0x300c: 3 0x3010: 4 0x3014: 5 0x3018: 6 0x301c: 7 0x3020: 8 Before 0x2000: 0x3000 0x3000: 0 0x3004: 1 0x3008: 2 0x300c: 3 0x3010: 4 0x3014: 5 0x3018: 6 0x301c: 7 0x3020: 8 After 0x2000: 0x300c 0x3000: 0 0x3004: 1 0x3008: 2 0x300c: 6 0x3010: 4 c[0] = c[3] 0x3014: 5 0x3018: 6 0x301c: 7 0x3020: 8 And in assembly language 12 Example: Endianness of a Computer 12 r[0] 0x2000 # r[0] = &c r[1] m[r[0]] # r[1] = c r[2] 12 # r[2] = 3 * sizeof(int) r[2] r[2]+r[1] # r[2] = c + 3 m[r[0]] r[2] # c = c + 3 r[3] 3 # r[3] = 3 r[4] m[r[2]+4*r[3]] # r[4] = c[3] m[r[2]] r[4] # c[0] = c[3] #include <stdioh> int main () { char a[4]; *((int*)a) = 1; ld $0x2000, r0 # r0 = &c ld (r0), r1 # r1 = c ld $12, r2 # r2 = 3*sizeof(int) add r1, r2 # r2 = c+3 st r2, (r0) # c = c+3 printf("a[0]=%d a[1]=%d a[2]=%d a[3]=%d\n",a[0],a[1],a[2],a[3]); ld $3, r3 # r3 = 3 ld (r2,r3,4), r4 # r4 = c[3] st r4, (r2) # c[0] = c[3] 13 14

Dynamic Allocation in C and Java Programs can allocate memory dynamically Dynamic Allocation allocation reserves a range of memory for a purpose in Java, instances of classes are allocated by the new statement in C, byte ranges are allocated by call to malloc function Wise management of memory requires deallocation memory is a scare resource deallocation frees previously allocated memory for later re-use Java and C take different approaches to deallocation How is memory deallocated in Java? Deallocation in C programs must explicitly deallocate memory by calling the free function free frees the memory immediately, with no check to see if its still in use Considering Explicit Delete 15 Let's extend the example to see 16 what might happen in bar() Let's look at this example and why a subsequent call to bat() would expose a serious bug struct MBuf* mbuf = (struct MBuf*) malloc (sizeof (struct MBuf)); struct MBuf* mbuf = (struct MBuf*) malloc (sizeof (struct MBuf)); is it safe to free mb where it is freed? void MBuf* amb; what bad thing can happen? void bar (MBuf* mb) { amb = mb; void bat () { amb->x = 0; This statement writes to unallocated (or re-allocated) memory 17 18

Dangling Pointers Avoiding Dangling Pointers in C A dangling pointer is Understand the problem a pointer to an object that has been freed when allocation and free appear in different places in your code could point to unallocated memory or to another object for example, when a procedure returns a pointer to something it allocates Why they are a problem Avoid the problem cases, if possible program thinks its writing to object of type X, but isn t restrict dynamic allocation/free to single procedure, if possible it may be writing to an object of type Y, consider this sequence of events don t write procedures that return pointers, if possible (1) Before free: amb: 0x2000 0x2000: a struct mbuf (2) After free: amb: 0x2000 0x2000: free memory dangling pointer use local variables instead, where possible - since local variables are automatically allocated on call and freed on return through stack Engineer for memory management, if necessary define rules for which procedure is responsible for deallocation, if possible (3) After another malloc: amb: 0x2000 0x2000: another thing dangling pointer that is really dangerous implement explicit reference counting if multiple potential deallocators define rules for which pointers can be stored in data structures use coding conventions and documentation to ensure rules are followed Avoiding dynamic allocation 19 Reference Counting 20 If procedure returns value of dynamically allocated object Use reference counting to track object use allocate that object in caller and pass pointer to it to callee any procedure that stores a reference increments the count good if caller can allocate on stack or can do both malloc / free itself any procedure that discards a reference decrements the count the object is freed when count goes to zero struct MBuf* mbuf = (struct MBuf*) malloc (sizeof (struct MBuf)); void receive (struct MBuf* mbuf) { struct MBuf mb; receive (&mb); struct MBuf* malloc_mbuf () { struct MBuf* mb = (struct MBuf* mb) malloc (sizeof (struct MBuf)); mb->ref_count = 1; return mb; void keep_reference (struct MBuf* mb) { mb->ref_count ++; void free_reference (struct MBuf* mb) { mb->ref_count --; if (mb->ref_count==0) 21 22

The example code then uses reference counting like this Garbage Collection struct MBuf* mbuf = malloc_mbuf (); In Java objects are deallocated implicitly the program never says free the runtime system tracks every object reference free_reference (mb); void MBuf* amb = 0; void bar (MBuf* mb) { if (amb!= 0) free_reference (amb); amb = mb; keep_reference (amb); when an object is unreachable then it can be deallocated a garbage collector runs periodically to deallocate unreachable objects Advantage compared to explicit delete no dangling pointers MBuf receive () { MBuf mbuf = new MBuf (); MBuf mb = receive (); Discussion 23 Memory Management in Java 24 What are the advantages of C s explicit delete Memory leak occurs when the garbage collector fails to reclaim unneeded objects memory is a scarce resource and wasting it can be a serous bug its huge problem for long-running programs where the garbage accumulates How is it possible to create a memory leak in Java? Java can only reclaim an object if it is unreachable What are the advantages of Java s garbage collection but, unreachability is only an approximation of whether an object is needed an unneeded object in a hash table, for example, is never reclaimed The solution requires engineering just as in C, you must plan for memory deallocation explicitly unlike C, however, if you make a mistake, you can not create a dangling pointer in Java you remove the references, Java reclaims the objects Is it okay to ignore deallocation in Java programs? Further reading http://javasuncom/docs/books/performance/1st_edition/html/jpappgcfmhtml 25 26