Slide: 1 CAMP 06: Maturing Minds Programming in C Recap Camp 06 Maturing Minds.

Slides:



Advertisements
Similar presentations
©2002, Ed Skoudis Format String Stack View main() { char user_input[100]; char buffer[100]; int x; … /*get user_input*/ … snprintf(buffer, sizeof buffer,
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
For(int i = 1; i
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
OPTIMIZING C CODE FOR THE ARM PROCESSOR Optimizing code takes time and reduces source code readability Usually done for functions that are critical for.
CS492B Analysis of Concurrent Programs Memory Hierarchy Jaehyuk Huh Computer Science, KAIST Part of slides are based on CS:App from CMU.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
23-Apr-15 Abstract Data Types. 2 Data types I We type data--classify it into various categories-- such as int, boolean, String, Applet A data type represents.
Section 3.2 Homework Questions?. Section Concepts 3.2 Factoring Trinomials of the Form x 2 + bx + c Slide 2 Copyright (c) The McGraw-Hill Companies, Inc.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Topic 6A – The char Data Type. CISC 105 – Topic 6A Characters are Numbers The char data type is really just a small (8 bit) number. As such, each symbol.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Variables and constants Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 3: Objects, Types, and Values 1 Based on slides.
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
J-1 University of Washington Computer Programming I Switch Statement © 2000 UW CSE.
Indices Tomasz Bartoszewski. Inverted Index Search Construction Compression.
Operator Overloading in C++. Operator Overloading It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it.
Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
C++ Pointers Review. Overview  What is a pointer  Why do I care?  What can be 'pointed to'?  Example.
School of Computing and Mathematics, University of Huddersfield CHA2545: WEEK 4 LECTURE: DENOTIONAL SEMANTICS OF A SIMPLE LANGUAGE TUTORIAL: Do exercises.
Data Type. Syntax Rules Recap keywords breakdoubleifsizeofvoid caseelseintstatic..... Identifiers not#me123th scanfprintf _idso_am_igedd007 Constant ‘a’‘+’
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Lesson 4.5 Concept: How to Multiply Integers Guidelines: When multiplying integers….. If the integer signs are the same, the product is positive ( + +)
Department of Electronic & Electrical Engineering Types and Memory Addresses Pointers & and * operators.
Data Types Always data types will decide which type of information we are storing into variables In C programming language we are having 3 types of basic.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
C is a high level language (HLL)
Two’s Complement The language of machines, part II.
Binary IO Writing and Reading Raw Data. Files Two major flavors of file: Text Binary.
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
CS 2130 Lecture 6 Pointers and Arrays. Pointers are like jumps, leading wildly from one part of the data structure to another. Their introduction into.
Windows Programming Lecture 03. Pointers and Arrays.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Introduction to Sketching
Pointers in C.
EPSII 59:006 Spring 2004.
continued on next slide
C Basics.
Languages and Compilers (SProg og Oversættere)
Programming Languages and Paradigms
References and Pointers
                                                                                                                                                                                                                                                
Data Structures and Analysis (COMP 410)
continued on next slide
continued on next slide
Introduction to the C Language
Lecture 8.
Relational Operators Operator Meaning < Less than > Greater than
Compilers B V Sai Aravind (11CS10008).
The Big 6 Research Model Step 3: Location and Access
Differences between Java and C

1st language words & bits 2nd language words & bits
LANGUAGE EDUCATION.
Lecture 8.
Pointer Arithmetic By Anand George.
continued on next slide
C# Language & .NET Platform 12th Lecture
continued on next slide
Presentation transcript:

Slide: 1 CAMP 06: Maturing Minds Programming in C Recap Camp 06 Maturing Minds

Slide: 2 CAMP 06: Maturing Minds Goals We review different constructs of C language We discuss typical syntactic and semantic pitfalls We discuss some complex programming concepts like function pointers We discuss some guidelines that will help you in writing good C code

Slide: 3 CAMP 06: Maturing Minds How big is an integer? long is not always 32 bits. int is not always 32 bits. The only truth is: sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) sizeof(char) is always 1. Sizes of float, double variables are implementation dependent.

Slide: 4 CAMP 06: Maturing Minds What happens when an integer operation overflows? If either operand is unsigned, the result is unsigned and is defined to be modulo 2 n. If both operands are signed, the result is undefined. a and b are two big integers which are multiplied to obtain a number that may be stored in a long. The following, however, is incorrect. long c = a * b; //Why? Integer overflow // a and b are int [positive] if ((a + b) < 0) complain(); //might not work if ((int) ((unsigned)a + (unsigned)b) < 0) complain(); // will work

Slide: 5 CAMP 06: Maturing Minds Are characters signed? Whether it is signed or unsigned is implementation dependent. Still, for programming purposes char is neither signed char nor unsigned char.

Slide: 6 CAMP 06: Maturing Minds Operator precedence Operator precedence may give surprises sometimes. if (flags & OF_FLAG != 1) {...}; // what does this mean? != binds more tightly than &. if ((flags & OF_FLAG) != 1) {...}; Use braces properly in expressing yourself

Slide: 7 CAMP 06: Maturing Minds Operator precedence Two int vars, h and l are in [0,15] range. r is formed as (h,l). r = h<<4 + l; // is it correct? r = h << (4 + l); //same as above + binds more tightly than <<. Dont trust spaces!!! Trust braces. r = (h << 4) + l; //correct

Slide: 8 CAMP 06: Maturing Minds Expression evaluation sequence i = 0; while ( i < n) { y[i] = x[i++]; } There is no guarantee that address of y[i] will be evaluated before i is incremented. Only operators &&, ||, ?:, and, specify an order of evaluation. i = 0; // This works while (i < n) { y[i] = x[i]; ++i; } Keep things simple and they will be reliable. Expression order for function parameters.

Slide: 9 CAMP 06: Maturing Minds Shift operators In right shift are vacated bits filled with zeros or sign bit? If item is unsigned zeros are filled in. If item is signed, implementation is permitted to fill either zero, or sign bit.

Slide: 10 CAMP 06: Maturing Minds Shift operators What values are permitted for a shift count? For an n-bit number Shift count >= 0 Shift count < n [strictly] Otherwise result is undefined !

Slide: 11 CAMP 06: Maturing Minds Shift operators Is right shift same as divide by 2? Well mostly yes. For signed numbers sign bit should be copied in vacated bits. But not Always! Try this! (-1)>>1

Slide: 12 CAMP 06: Maturing Minds Are Assignment statements expressions? Is the following statement valid? a = b = c+d*e; Assignment statements are expressions as well. Expressions have operators, operands and a result. The result (like operators and operands) do have a type and a value. The value and type of an expression is the same as the type and value of the lhs of an expression. You must be familiar with the following code segments if (a = (b+c)) …. while (ch = getc()) ….

Slide: 13 CAMP 06: Maturing Minds Watch those semicolons! struct { int x; } f() {... } if (x[i] > big); big = x[i]; Dont trust implicit return types. Be careful of semicolons.

Slide: 14 CAMP 06: Maturing Minds The switch statement switch (color) { case 1: printf ("red"); case 2: printf("yellow"); break; case 3: printf("blue"); } Be careful of break. It is weakness as well as strength of C. case SUBTRACT: opnd2 = -opnd2; /* falls through*/ case ADD:... Use comments if a case falls through into another.

Slide: 15 CAMP 06: Maturing Minds The dangling else if (x == 0) if (y == 0) error(); else { z = x + y; f(&z); } if (x == 0) { if (y == 0) { error(); } else { z = x + y; f(&z); } Indentation is good as people understand it well. Braces are necessary as compilers understand it well.

Slide: 16 CAMP 06: Maturing Minds Functions and Parameter Passing Functions are also types to the compilers Type of a function comprises of the signature and the return type. Function names are identifiers. Parameters are passed in C using call by value. This value may be an integer, an array, a structure or a pointer.

Slide: 17 CAMP 06: Maturing Minds Call by Value There is no mechanism called call by address. The value passed may legitimately be an address and therefore the type of the formal parameter is pointer. The value of the actual parameter can never be changed from within a function invocation. What we change at the most is what has been pointed to by a formal parameter. Change in the formal parameter, in case the formal parameter is an address, can never be reflected in the value of the actual parameter.

Slide: 18 CAMP 06: Maturing Minds Array & Pointer These are two separate data types. Array name is an l- value that cannot be modified. Initialization of an array has a different syntax than initialization of a pointer. Distinguish between the two. char a[] = "string literal"; char *p = "string literal"; char *p = "hello, world!"; p[0] = 'H'; // This code may crash Pointer to an array int (*ap)[N]; /* N is a constant expression. Passing statically and dynamically allocated multi- dimensional array in functions.

Slide: 19 CAMP 06: Maturing Minds Passing 1-D Arrays to Functions Consider a 1-d array int a[10] has to be passed to a function. Which of the following functions have the correct declaration? void f (int *x) void f (int x[5]) void f (int x[10]) void f (int x[12])

Slide: 20 CAMP 06: Maturing Minds Passing 2-D Arrays to Functions Consider a 1-d array int a[10][20] has to be passed to a function. Which of the following functions have the correct declaration? void f (int * *x) void f (int *x[20]) void f (int (*x)[10]) void f (int (*x)[20]) void f (int x[10][20])

Slide: 21 CAMP 06: Maturing Minds Structure & Alignment Structure is a type. Only operators valid on a structure are: =, &,. Two structures cannot be compared. sizeof of a structure is most often more than the individual sizeof of its members struct example { char c; int n; } size of example is not 1+4. Due to word alignment Word alignment is important for efficient machine level data storage and retrieval.

Slide: 22 CAMP 06: Maturing Minds Structure & Alignment Padding for word alignment cannot be turned off in a universal manner Determining the byte offset of a field within a structure ANSI C defines a offsetof() macro, which should be used if available; see. If you don't have it, one possible implementation is #define offsetof(type, mem) ((size_t) ((char *)&((type *)0)->mem - (char *)(type *)0)) This implementation is not 100% portable; some compilers may legitimately refuse to accept it. Build a table of names and offsets, using the offsetof() macro. The offset of field b in struct a is offsetb = offsetof(struct a, b) If structp is a pointer to an instance of this structure, and field b is an int (with offset as computed above), b's value can be set indirectly with *(int *)((char *)structp + offsetb) = value;

Slide: 23 CAMP 06: Maturing Minds NULL pointers It must never be dereferenced. The NULL pointer is not a null string. if (p == (char *) 0)... //ok if (strcmp(p, (char *)0) == 0)... //invalid printf(p); //invalid if p is null pointer

Slide: 24 CAMP 06: Maturing Minds NULL pointer NULL pointer is a literal for every pointer type. However, the type of NULL is different in different contexts. NULL and NULL pointer are not the same. NULL is a macro that is defined to be 0. A constant 0 in a pointer context is converted into a null pointer at compile time. That is, in an initialization, assignment, or comparison when one side is a variable or an expression of pointer type, the compiler can tell that a constant 0 on the other side requests a null pointer, and generate the correctly-typed null pointer value. Therefore, the following fragments are perfectly legal char *p = 0; if(p != 0)

Slide: 25 CAMP 06: Maturing Minds NULL pointers in Functions An argument being passed to a function is not necessarily recognizable as a pointer context, and the compiler may not be able to tell that an unadorned 0 means a null pointer. To generate a null pointer in a function call context, an explicit cast may be required, to force the 0 to be recognized as a pointer. Suppose NULL is defined as follows: #define NULL ((char *)0) wouldn't that make function calls which pass an uncast NULL work? Not in general.

Slide: 26 CAMP 06: Maturing Minds getchar() returns an integer #include int main() { char c; while ((c = getchar()) != EOF) { putchar(c); } Catch the bug!!! char c cant hold EOF, use int c instead.

Slide: 27 CAMP 06: Maturing Minds The preprocessor Important for following major reasons We can change all instances of a particular quantity For defining macros that appear to be functions, but do not have the function calling overhead

Slide: 28 CAMP 06: Maturing Minds Macros are not functions In a macro, an operand that is used twice may be evaluated twice. Expressions like x[i++] lead to side effects. Avoid side effects! #define MAX(a,b) (((a) > (b)) ? (a) : (b)) biggest = x[0]; i = 1; while (i < n) biggest = MAX(biggest, x[i++]); // wrong??

Slide: 29 CAMP 06: Maturing Minds Understanding Declarations int *gi();() binds more tightly than *. *gi() is same as*(gi()) *(gi()) is of type int => gi() is a pointer to int, => gi is a function that returns pointer to int. int (*hi)();(*hi)() is int => (*hi) is a function that returns int => hi is a pointer to a function that returns int

Slide: 30 CAMP 06: Maturing Minds Understanding typecasts If you know how to declare a type, it is easy to write a cast for that type. TypeDeclarationCast Pointer to function that returns intint (*fp)()int (*)() Pointer to function that returns nothingvoid (*fp)()void (*)()

Slide: 31 CAMP 06: Maturing Minds Try it yourself There is a function void f(). Its address is stored at location 0x0 in memory. Write a C statement to execute this function by taking address from 0x0. What is meant by the following declaration? int signal( int (*)(int), int);

Slide: 32 CAMP 06: Maturing Minds Function pointers Variables which point to the address of a function We can implement call-backs Complicated syntax Use it only when you need it!

Slide: 33 CAMP 06: Maturing Minds A Generic Search Problem Suppose that we have to search one item from a list (dynamic array) of such items. We do not know the type of the item when we write the code. Pseudo code for (int i; i < n; i++) { if (equal(a[i], k) return i; } Issues: What is the type of the array a, variable k? If a is declared as void *, *(a+i) is not defined. How can we manage to write equal function? back

Slide: 34 CAMP 06: Maturing Minds Solution to Generic Search Problem Solution to the first two issues: Formal parameter a is of type void *. However actual parameter for a may be a pointer to a list of any thing (e.g., rectangle, employee etc.) The caller of generic_search has to supply the size of the type that the actual parameter of a points to. Formal parameter of k is of type void *. The caller ensures that the actual parameter for a and k are the same. for (i = 0; i < n; i++) { equal ( (void *)((char*)a + i *size), k) return i; } back

Slide: 35 CAMP 06: Maturing Minds Using Function pointers equal function can not be written by the implementer of generic_search. equal function can be written by the designer and developer of types such as rectangle. The prototype for equalmust be fixed and may be int XXX(void *, void *). The actual function XXX may be passed to the generic_search routine by the caller. int generic_search( void* a, int n, void *k, int size, int (*fp)(void *, void *)) { for (i = 0; i < n; i++) { if ((*fp)( (void *)((char*)a + i *size), k) == 0) return i; }

Slide: 36 CAMP 06: Maturing Minds Check external types yourself file1.c extern int n; file2.c char n; Compiler compiles single file, doesnt know contents of others. Linkers may not know about data types. The program may work just by coincidence!!

Slide: 37 CAMP 06: Maturing Minds Reentrancy For the code to be re-entrant, each invocation must use its own copy of any modifiable data. Static variables should not be used. Global variables should not be used [except constants] Automatic variables should be used. No calls to a non-reentrant code should be made.