CME 212: C programming II

Similar documents
The C Programming Language course syllabus associate level

/* File: blkcopy.c. size_t n

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

Illustration 1: Diagram of program function and data flow

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

System Calls and Standard I/O

C++ Programming Language

1 Abstract Data Types Information Hiding

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

Module 816. File Management in C. M. Campbell 1993 Deakin University

Example 1/24. Example

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

The programming language C. sws1 1

C++ INTERVIEW QUESTIONS

Operating Systems. Privileged Instructions

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

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)

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

El Dorado Union High School District Educational Services

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

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

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

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

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

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

Programming languages C

Object Oriented Software Design II

File Handling. What is a file?

CP Lab 2: Writing programs for simple arithmetic problems

C Interview Questions

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

Informatica e Sistemi in Tempo Reale

Pemrograman Dasar. Basic Elements Of Java

Chapter 13 - The Preprocessor

Fortify on Demand Security Review. Fortify Open Review

Tutorial on C Language Programming

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

LOW LEVEL FILE PROCESSING

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

10CS35: Data Structures Using C

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Java Interview Questions and Answers

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

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

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

7th Marathon of Parallel Programming WSCAD-SSC/SBAC-PAD-2012

Stacks. Linear data structures

Comp151. Definitions & Declarations

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

How To Write Portable Programs In C

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT COURSE CURRICULUM. Course Title: Advanced Computer Programming (Code: )

Leak Check Version 2.1 for Linux TM

Computer Programming Tutorial

Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11

C Programming Tutorial

C Library ABI for the ARM Architecture

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

Binary storage of graphs and related data

Objective-C Tutorial

DNA Data and Program Representation. Alexandre David

Lab 4: Socket Programming: netcat part

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Crash Course in Java

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Introduction to C using lcc-win

Building Embedded Systems

C PROGRAMMING FOR MATHEMATICAL COMPUTING

Top 10 Bug-Killing Coding Standard Rules

Object Oriented Software Design

An Overview of Java. overview-1

Introduction to Programming II Winter, 2014 Assignment 2

So far we have considered only numeric processing, i.e. processing of numeric data represented

The GenomeTools Developer s Guide

Chapter 13 Storage classes

Common Errors in C/C++ Code and Static Analysis

5 Arrays and Pointers

C++ Language Tutorial

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

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

Embedded Systems Design Course Applying the mbed microcontroller

Object Oriented Software Design

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

ELEC3730 Embedded Systems Lecture 1: Introduction and C Essentials

C++ for Safety-Critical Systems. DI Günter Obiltschnig Applied Informatics Software Engineering GmbH

Facebook Twitter YouTube Google Plus Website

Semantic Analysis: Types and Type Checking

Shared Memory Segments and POSIX Semaphores 1

Applying Clang Static Analyzer to Linux Kernel

CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

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

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

VB.NET Programming Fundamentals

CORBA Programming with TAOX11. The C++11 CORBA Implementation

CS 111 Classes I 1. Software Organization View to this point:

Rationale for International Standard Programming Languages C

Bonus ChapteR. Objective-C

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

Transcription:

CME 212: C programming II Lecture 4 January 20, 2012

Memory management in C Our programs normally don t know how much memory they are going to need before they start. Thus, we need to be able to dynamically allocate memory. C provides 2 mechanisms for this: variable length arrays memory management functions in <stdlib.h>

Variable length arrays 1 int do_something(int n) { 2 double a[n]; 3... 4 } Introduced in C99. Controversial feature. Not allowed in standard C++. If n is too large, will cause a segmentation fault with gcc, cannot check for error during runtime What happens if n is negative? My recommendation: avoid this in real code

Dynamic memory allocation We are asking the operating system for memory. The OS can say no, but is usually generous. 1 int *array; int i; 2 3 // ask for memory 4 array = (int *)malloc(20*sizeof(int)); 5 6 // check allocation 7 if (!array ) { 8 // memory not allocated, do something smart 9 } 10 11 for ( i = 0; i < 20; i++ ) { 12 array[i] = i; // or *(array+i) = i 13 } 14 15 free(array); // release the memory when done

malloc() & free() Remember to include <stdlib.h> void *malloc(size_t size) allocates an block of size bytes returns pointer to first byte if successful returns NULL if unsuccessful data is not initialized void free(void *ptr) ptr must have been allocated by malloc or associated functions

Other memory functions void *calloc(size_t count, size_t eltsize) note that function has 2 arguments sets all data to 0 void *realloc (void *ptr, size_t newsize) can be used to expand or contract the size of a block if ptr == NULL, behaves like malloc might need to copy data to another part of memory indicates failure by returning NULL, data associated with ptr is not affected void *memalign(size_t boundary, size_t size); returned address is an even multiple of boundary useful if you want data in a particular part of memory

Remember The programmer is responsible for freeing all memory. This is a common source of errors. The term memory leak refers to memory that a program allocates, but does not free. If you do bad things with memory, the program might not crash until you attempt to free the memory. A tool called Valgrind can help find these sorts of bugs.

Relationship between pointers and arrays array indexing and pointer arithmetic can be used on both pointers are mutable, arrays are not sizeof on an array returns the size of the array in bytes sizeof on a pointer returns the size of an address

Pointers vs. arrays example 1 int array[4]; 2 array[0] = 0; array[1] = 1; 3 *(array+2) = 2; *(array+3) = 3; 4 int *ptr = malloc(4*sizeof(int)); 5 ptr[0] = 0; ptr[1] = 1; 6 *(ptr+2) = 2; *(ptr+3) = 3; 7 8 ptr = array; // YES 9 array = ptr; // NO! 10 ptr = array[i]; // NO! 11 ptr++; // YES 12 array++; // NO! 13 14 size_t as = sizeof(array); // as = 16 15 size_t ps = sizeof(ptr); // ps = 8

Passing data to functions There are two ways to pass data to a function: pass-by-value Data is copied in to variables in the scope of the function. The variable in the function call is not changed. pass-by-reference A reference to data in the calling scope is passed to function. Function may change data in the calling scope. C is pass-by-value, but arrays are cast to pointers use pointers for pass-by-reference 1 int f(int a, int *b) { 2 *b = *b + 1; 3 // *b is now changed in scope of the call to f 4 5 a = a + 1; // does not change first argument to f 6 return a; 7 }

The const keyword Sometimes we don t want a function to change the data. 1 int f(const int a, const int *b, 2 int *const c, const int *const d) { 3 // cannot change a 4 // cannot change data b points to 5 // cannot change address stored in c 6 // cannot change data d points to 7 // cannot change address stored in d 8 return 0; 9 }

The C preprocessor C code is pre-processed before being sent to the compiler Carries out fairly simple, but tedious operations inserting code (conditionally) macros setting constants Most common preprocessor directives are #include: include a file #define: define constant, or macro One very important application: the header guard...

The header guard code.h 1 #ifndef CODE_H_ 2 #define CODE_H_ 3 4 // declare functions 5 6 #endif code.c 1 #include "other_code.h" 2 #include "code.h" 3 4 // define functions If other_code.h includes code.h, the code.h header will not be included twice in code.c. Do this in all of your headers.

Part of the C standard library header description assert.h Diagnostics ctype.h Character Class Tests errno.h Error Codes Reported by (Some) Library Functions float.h Implementation-defined Floating-Point Limits limits.h Implementation-defined Limits locale.h Locale-specific Information math.h Mathematical Functions setjmp.h Non-local Jumps signal.h Signals stdarg.h Variable Argument Lists stddef.h Definitions of General Use stdio.h Input and Output stdlib.h Utility functions string.h String functions time.h Time and Date functions (list is extended by newer C standards.)

Some important functions & constants functions library note atof(),atoi(),atol() <stdlib.h> ASCII to float, integer.. rand(),srand() <stdlib.h> Random numbers abort(),exit() <stdlib.h> Aborts or exits assert() <assert.h> Error checking strcpy(),strlen()... <string.h> Manipulating strings INT_MIN, INT_MAX <limits.h> Limits, bounds of ints DBL_EPSILON... <float.h> Limits, bounds of FP sin(),floor()... <math.h> Math functions, rounding printf,fscanf... <stdio.h> Input and Output

I/O: <stdio.h> The standard library uses the abstraction of a stream Can be connected to a file or the terminal There are 3 predefined streams stderr stdin stdout These are connected to the terminal

Using Streams fputs(char *s, FILE *stream) writes the string s to stream, which is of type FILE * puts(char *s) is predefined to print to stdout int fgetc(file *stream) reads a character from stream and returns it as an integer int getchar(void) reads from stdin

Formatting Output Using only fputc(), etc. will force you to manually convert the binary representation of your type to ASCII The standard library can help you with this conversion using so-called format strings These strings contains a code for how you want your conversion to be performed

Conversion Letters d u f,g e,e c s p signed integer unsigned integer floating point numbers FP in exponential format character null terminated string pointer

Format specification Apart from the conversion letter you can also control the output in many other ways. This is done by specifying formatting strings: "%d" "%12e" "%8.4f" "%-p" "%+le" prints an integer prints FP in 12 characters prints FP, width 8, 4 decimals prints pointer, left justified Always print the + sign Keep a reference handy!

printf() Prints the conversion of a variable using the format specification to stdout 1 int a = 5; 2 printf("a = %d\n",a);

Formatted Input The scanf() family of function scans a stream for data using the same formatting strings Remember to use pointers for the variables 1 int a; 2 int num_inputs; 3 num_inputs = scanf("%d",&a);

Working with files You can connect files to streams using fopen(), fclose() There are functions that control your position in the file fseek(): goto a position ftell(): get the position rewind(): goto beginning of file fgetpos(): more portable ftell fsetpos(): more portable fseek

Binary Files You can also dump the contents of the memory to a file, called a binary file Saves space Makes it hard for someone to read the file You must remember the representation of your data when reading Use: fread(), fwrite()

Binary files, example Write binary data: 1 size_t n = 100; 2 double a[n]; 3 for (size_t i = 0; i!= n; ++i) 4 a[i] = (double) i; 5 FILE *fp = fopen("data.bin","wb"); 6 size_t w1 = fwrite(&n,sizeof(size_t),1,fp); 7 size_t w2 = fwrite(a,sizeof(double),n,fp); 8 fclose(fp); Read binary data: 1 FILE *fp = fopen("data.bin","rb"); 2 size_t n; 3 size_t w1 = fread(&n,sizeof(size_t),1,fp); 4 double a[n]; 5 size_t w2 = fread(a,sizeof(double),n,fp); 6 fclose(fp);

Using errno If a call returned that an error occurred, the global variable errno is set to a negative integer to indicate what went wrong The errno variable is set to a predefined error code, see man errno With every errno code there is a corresponding error message which can be retrieved using the functions perror() and strerror()

errno, Example 1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(int argc, char *argv[]) { 4 FILE *f; 5 if ((f = fopen(*(argv+1), "r")) == NULL) 6 perror("fopen failed"); exit(1); 7 8 fclose(f); 9 return 0; 10 } $./errno bogus-file fopen failed: No such file or directory $./errno fopen failed: Bad address

Assertion The standard library includes a macro that can catch errors void assert(expression); If the expression is false, assert prints out the line and file of the assert statement that triggered myprogram.c :234: function: Assertion expression failed. If NDEBUG is defined before assert.h is included, all assertions will map to an empty macro

Assert, Example 1 int main(void) { 2 int *a; 3 a = (int *)malloc(100*sizeof(int)); 4 assert( a!= NULL ); 5 }

primitive types keyword void char int float double signed unsigned short long _Bool _Complex _Imaginary note C99 C99 C99

defined types keyword typedef struct enum union note define a type name define aggregate data type enumeration multiple type for same data

for types Table: type qualifiers keyword const volatile note hold constant do not optimize Table: storage classes keyword auto static extern register note also function qualifier hint to compiler

flow control keyword if else do while for switch case default break continue goto return

other keyword restrict inline sizeof note for optimization inline a function get size of type