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



Similar documents
The C Programming Language course syllabus associate level

5 Arrays and Pointers

arrays C Programming Language - Arrays

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

Semantic Analysis: Types and Type Checking

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

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

Introduction to Data Structures

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

Object Oriented Software Design II

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Java Interview Questions and Answers

Stacks. Linear data structures

Arrays in Java. Working with Arrays

DATA STRUCTURES USING C

CMSC 202H. ArrayList, Multidimensional Arrays

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

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

ECS 165B: Database System Implementa6on Lecture 2

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

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

How To Write Portable Programs In C

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

C++ Programming Language

Jonathan Worthington Scarborough Linux User Group

Simple C Programs. Goals for this Lecture. Help you learn about:

No no-argument constructor. No default constructor found

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

Chapter 5 Names, Bindings, Type Checking, and Scopes

DNA Data and Program Representation. Alexandre David

Lecture 32: The Java Virtual Machine. The Java Virtual Machine

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

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

8.5. <summary> Cppcheck addons Using Cppcheck addons Where to find some Cppcheck addons

Moving from CS 61A Scheme to CS 61B Java

C++ INTERVIEW QUESTIONS

Lecture 22: C Programming 4 Embedded Systems

Contents. 9-1 Copyright (c) N. Afshartous

Object Oriented Software Design II

Answers to Review Questions Chapter 7

Java's garbage-collected heap

Crash Course in Java

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

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

The programming language C. sws1 1

Keil C51 Cross Compiler

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Data Structures using OOP C++ Lecture 1

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

CS 106 Introduction to Computer Science I

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Matrix Multiplication

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

CSCI E 98: Managed Environments for the Execution of Programs

Leak Check Version 2.1 for Linux TM

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

C Interview Questions

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

Lecture 5: Java Fundamentals III

6.S096 Lecture 1 Introduction to C

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

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

How to create/avoid memory leak in Java and.net? Venkat Subramaniam

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

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

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

CS 241 Data Organization Coding Standards

Explain the relationship between a class and an object. Which is general and which is specific?

Java CPD (I) Frans Coenen Department of Computer Science

Basic Programming and PC Skills: Basic Programming and PC Skills:

CS106A, Stanford Handout #38. Strings and Chars

El Dorado Union High School District Educational Services

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

The Function Pointer

7.1 Our Current Model

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

Habanero Extreme Scale Software Research Project

Operating Systems and Networks

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Lecture 11: Tail Recursion; Continuations

Evolution of the Major Programming Languages

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

SmartArrays and Java Frequently Asked Questions

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Java Basics: Data Types, Variables, and Loops

Chapter 13 Storage classes

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

C# and Other Languages

Introduction to Java

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

Bypassing Browser Memory Protections in Windows Vista

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Recursion vs. Iteration Eliminating Recursion

Comp151. Definitions & Declarations

Introduction to Java. CS 3: Computer Programming in Java

Trace-Based and Sample-Based Profiling in Rational Application Developer

Transcription:

Announcements Memory management Assignment 2 posted, due Friday Do two of the three problems Assignment 1 graded see grades on CMS Lecture 7 CS 113 Spring 2008 2 Safe user input If you use scanf(), include a maximum width in the format string char string[1024]; scanf( %1023s, string); Never use gets()! There s no way to use it safely Use fgets() instead char string[1024]; if(!fgets(string, 1024, stdin)) goto error; // remove newline character if(string[strlen(string)-1] == \n ) string[strlen(string)-1] = 0; Function pointers In C, you can also take the address of a function I.e. the memory location of the function s machine code Example of declaring a function pointer: int (*fctnptr)(int, int); This is a pointer to a function that takes two parameters of type int and returns an integer Use the & operator to take the address of a function, and the * operator to dereference a function pointer 3 4 Uses of function pointers Function pointer example Systems programming e.g. setting up interrupt vector tables Writing generic code A function can take a function as a parameter Example: a generic function to compute integrals double compute_integral(double a, double b, double (*f)(double)) /* lots of code */ double x_squared(double x) return x * x; double x_cubed(double x) return x * x * x; compute_integral(0, 1, x_squared); compute_integral(5, 9, x_cubed); 5 #include <stdio.h> void change_array( int *a, int size, int (*f)(int)) int j; for( j=0; j < size; j++) a[j] = f(a[j]); int add_one(int x) return x + 1; int square(int x) return x * x; int a[5] = 0, 1, 2, 3, 4 ; change_array(a, 5, add_one); // increment every element change_array(a, 5, square); // square every element 6

Other examples qsort(), heapsort(), mergesort() are standard library functions for generic sorting defined in stdlib.h They take a comparison function as a parameter They can sort any type of array, as long as an appropriate comparison function is given Also available: lsearch() : linear search bsearch() : binary search through a sorted array String headaches Remember that you are responsible for allocating enough space for strings! This code crashes: s1 isn t big enough to hold s1 + s2 This code works: char s1[] = Any person, ; char s2[] = any study. strcat(s1, s2); // crash char s1[1024] = Any person, ; char s2[] = any study. strcat(s1, s2); // OK 7 8 More string headaches Idea: what if we create a wrapper function for strcat? What is wrong here? char *my_strcat(char *s1, char *s2) Local variables Recall that local variables are stored on the stack Memory for local variables is deallocated when function returns Returning a pointer to a local variable is almost always a bug! char *my_strcat(char *s1, char *s2) // BUG! returns a pointer to a deallocated // memory region. C requires that the size of variables on the stack be known at compile time, so dynamically-sized arrays aren t possible 9 10 The Heap C can use space in another part of memory: the heap The heap is separate from the execution stack Heap regions are not deallocated when a function returns Note: this is completely unrelated to the Heap data structure The programmer requests storage space on the heap C never puts variables on the heap automatically But local variables might point to locations on the heap Heap space must be explicitly allocated and deallocated by the programmer malloc Library function in stdlib.h Stands for memory allocate Requests a memory region of a specified size Syntax: void *malloc(int size) address of memory region size in bytes void * is a generic pointer type It cannot be dereferenced But it can be cast to any other pointer type 11 int size = 4; char *char_array = (char *) malloc(size); // char_array now points to a memory region // of 4 bytes allocated to me 12

Using heap regions You can use the allocated memory however you d like e.g. treat it as an array, use pointer notation, etc. Example: int size = 4; char *buf = (char *) malloc(size); buf[0] = 7; scanf( %c, buf+1); Creating arrays using malloc malloc() s parameter is the size in bytes Not the number of elements in the array! e.g. an array with 100 integers typically requires 400 bytes Use the sizeof operator to find the size of a type, e.g.: float *array = (float *) malloc(element_count * sizeof(float)); malloc() might fail if there isn t enough memory available to satisfy the request malloc() returns NULL in this case 13 14 #include <assert.h> #include <stdlib.h> #include <stdio.h> int item_count = 0; float *array; Example: using malloc printf( How many items do you have? ); scanf( %d, &item_count); array = (float *) malloc(sizeof(float) * item_count); assert(array!= NULL); for(j = 0; j<item_count; j++) scanf( %f, array+j); free malloc() ed memory is not freed automatically Even after all pointers to it have been destroyed! Must call free()to deallocate memory when done using it void f() char *p; p = (char *) malloc(1000); while(1) f(); 15 16 free malloc() ed memory is not freed automatically Even after all pointers to it have been destroyed! Must call free() to deallocate memory when done using it void f() char *p; p = (char *) malloc(1000); /* some code */ free(p); while(1) f(); Memory leaks You need a call to free() for every call to malloc() Forgetting to call free() causes a memory leak The memory is not being used but still allocated Memory leaks are very common, but often difficult to find Other bad memory errors Calling free() more than once Calling free() on a pointer not returned by malloc() 17 18

strcat() wrapper strcat() wrapper Idea: what if we create a wrapper function for strcat? This version didn t work: Solution: allocate the result on the heap But the caller is responsible for free ing it char *my_strcat(char *s1, char *s2) 19 char *my_strcat(char *s1, char *s2) int length = strlen(s1) + strlen(s2) + 1; char *s3 = (char *) malloc(length); free(result); 20 An aside: Memory management in Java Java has similar memory management concepts Primitive types and references are stored on the stack You can allocate space on the heap using new String getstring() return new String(); void main() String s = getstring(); /* do stuff with the string */ Garbage collection Java uses a technique called garbage collection The JVM occasionally scans for heap space no longer in use It frees objects not pointed to by any reference This garbage collector is run as a background thread Java was specifically designed for garbage collection The JVM can figure out whether or not an object is in use This isn t possible in C because of pointer arithmetic But you don t have to worry about deallocating memory. Why? 21 22 Garbage collection in C Memory management: Java vs.. C Pointers make garbage collection difficult or impossible It s very difficult to determine whether memory is still being used char *s = (char *) malloc(1024); s = s - 10000; // nothing points to the allocated memory // region. Could it be garbage collected? s = s + 10000; // no, because now something points to it again! Java s references are a restricted form of pointers that don t allow arithmetic, just because of this issue There are garbage collecting libraries for C, but they aren t guaranteed to work with any program Java Pro Easier to program Memory leaks impossible Easier to write secure programs Con Slower No control over GC Not suitable for systems programming C Pro Programmer can control, optimize memory use Faster Con More difficult to learn Takes discipline to write safe, secure code 23 24

Multidimensional arrays C lets you create arrays of any dimensionality e.g. to create a 3-dimensional array on the stack int array[10][20][30]; You can also initialize multidimensional arrays, e.g. Accessing array elements Access array elements using brackets [ ] int i, j, k, array[10][20][30]; for(i=0; i<10; i++) for(j=0; j<20; j++) for(k=0; k<30; k++) array[i][j][k] = i+j+k; int array[][] = 1, 2, 3, 4, 5, 6 creates a 2-D array that looks like: 1 2 3 4 5 6 25 26 Multidimensional arrays and pointers Recall: a 1-D array is just a pointer A 2-D array is just an array of arrays int array2[10][20]; // type of array is int ** array2 is a pointer to a pointer to an integer e.g. *array2 or array2[0] has type int *, and is a pointer to the beginning of the first row of the array Multidimensional arrays on the heap Creating a multi-d array on the heap is more involved Setup the array of row pointers and each row array explicitly // create array with 10 rows, 20 columns int **array2 = (int **) malloc(10 * sizeof(int *)); for(i=0; i<10; i++) array2[i] = (int *) malloc(20 * sizeof(int)); array2 array2 27 28