The Function Pointer
|
|
|
- Lizbeth Franklin
- 9 years ago
- Views:
Transcription
1 The Function Pointer 1. Introduction to Function Pointers Function Pointers provide some extremely interesting, efficient and elegant programming techniques. You can use them to replace switch/if-statements, to realize your own late-binding or to implement callbacks. Unfortunately probably due to their complicated syntax they are treated quite stepmotherly in most computer books and documentations. If at all, they are addressed quite briefly and superficially. They are less error prone than normal pointers cause you will never allocate or de-allocate memory with them. All you ve got to do is to understand what they are and to learn their syntax. But keep in mind: Always ask yourself if you really need a function pointer. 1.1 What is a Function Pointer? Function Pointers are pointers, i.e. variables, which point to the address of a function. You must keep in mind, that a running program gets a certain space in the main-memory. Both, the executable compiled program code and the used variables, are put inside this memory. Thus a function in the program code is, like e.g. a character field, nothing than an address. It is only important how you, or better your compiler/processor, interpret the memory a pointer points to. 1.2 Introductory Example or How to Replace a Switch-Statement When you want to call a function DoIt() at a certain point called label in your program, you just put the call of the function DoIt() at the point label in your source code. Then you compile your code and every time your program comes up to the point label, your function is called. Everything is ok. But what can you do, if you don t know at build-time which function has got to be called? What do you do, when you want to decide it at runtime? Maybe you want to use a so called Callback- Function or you want to select one function out of a pool of possible functions. However you can also solve the latter problem using a switch-tatement, where you call the functions just like you want it, in the different branches. But there s still another way: Use a function pointer! In the following example we regard the task to perform one of the four basic arithmetic operations. The task is first solved using a switch-statement. Then it is shown, how the same can be done using a function pointer. // 1.2 Introductory Example or How to Replace a Switch-Statement // Task: Perform one of the four basic arithmetic operations specified by the // characters +, -, * or /. // The four arithmetic operations... one of these functions is selected // at runtime with a switch or a function pointer float Plus (float a, float b) return a+b; float Minus (float a, float b) return a-b; float Multiply(float a, float b) return a*b; float Divide (float a, float b) return a/b; // b!=0 is assumed 1
2 // Solution with a switch-statement -<opcode> specifies which operation to execute float Switch(float a, float b, char opcode) float result; // execute operation switch(opcode) case + : result = Plus (a, b); break; case - : result = Minus (a, b); break; case * : result = Multiply (a, b); break; case / : result = Divide (a, b); break; return result; //Solution with a function pointer:<pt2func> is a function pointer and points to // a function which takes two floats and returns a float. The function pointer // "specifies" which operation shall be executed. float Switch_With_Function_Pointer(float a, float b, float (*pt2func)(float, float)) float result = pt2func(a, b); // call using function pointer return result; // Execute example code void Replace_A_Switch() float result1, result2; printf("\nexecuting function Replace_A_Switch \n"); result1 = Switch(2, 5, /* + specifies function Plus to be executed */ + ); result2 = Switch_With_Function_Pointer(2, 5, /* pointer to function Minus */ &Minus); Important note: A function pointer always points to a function with a specific signature! Thus all functions, you want to use with the same function pointer, must have the same parameters and return-type! 2
3 2 The Syntax of C Function Pointers 2.1 Define a Function Pointer Since a function pointer is nothing than a variable, it must be defined as usual. In the following example we define a function pointers named pt2function. It points to a function, which take one float and two char and return an int. // 2.1 define a function pointer and initialize to NULL int (*pt2function)(float, char, char) = NULL; 2.2 Calling Convention Normally you don t have to think about a function s calling convention: The compiler assumes cdecl as default if you don t specify another convention. However if you want to know more, keep on reading... The calling convention tells the compiler things like how to pass the arguments or how to generate the name of a function. Examples for other calling conventions are stdcall, pascal, fastcall. The calling convention belongs to a functions signature: Thus functions and function pointers with different calling convention are incompatible with each other! For Borland and Microsoft compilers you specify a specific calling convention between the return type and the function s or function pointer s name. For the GNU GCC you use the attribute keyword: Write the function definition followed by the keyword attribute and then state the calling convention in double parentheses. // 2.2 define the calling convention void cdecl DoIt(float a, char b, char c); // Borland and Microsoft void DoIt(float a, char b, char c) attribute ((cdecl)); // GNU GCC 2.3 Assign an Address to a Function Pointer It s quite easy to assign the address of a function to a function pointer. You simply take the name of a suitable and known function or member function. Although it s optional for most compilers you should use the address operator & infront of the function s name in order to write portable code. // 2.3 assign an address to the function pointer // Note: Although you may ommit the address operator on most compilers // you should always use the correct way in order to write portable code. int DoIt (float a, char b, char c) printf("doit\n"); return a+b+c; int DoMore(float a, char b, char c) printf("domore\n"); return a-b+c; pt2function = DoIt; // short form pt2function = &DoMore; // correct assignment using address operator 3
4 2.4 Comparing Function Pointers You can use the comparison-operators (==,!=) the same way as usual. In the following example it is checked, whether pt2function actually contains the address of the function DoIt. A text is shown in case of equality. // 2.4 comparing function pointers if(pt2function!= NULL) // check if initialized if(pt2function == &DoIt) printf("pointer points to DoIt\n"); printf("pointer not initialized!!\n"); 2.5 Calling a Function using a Function Pointer In C you call a function using a function pointer by explicitly dereferencing it using the * operator. Alternatively you may also just use the function pointer s instead of the funtion s name. // 2.5 calling a function using a function pointer int result1 = pt2function (12, a, b ); // C short way int result2 = (*pt2function) (12, a, b ); // C 2.6 How to Pass a Function Pointer as an Argument? You can pass a function pointer as a function s calling argument. You need this for example if you want to pass a pointer to a callback function. The following code shows how to pass a pointer to a function which returns an int and takes a float and two char: // 2.6 How to Pass a Function Pointer // <pt2func> is a pointer to a function which returns an int and takes a float and two char void PassPtr(int (*pt2func)(float, char, char)) int result = (*pt2func)(12, a, b ); // call using function pointer printf("%d", result); // execute example code - DoIt is a suitable function like defined above in void Pass_A_Function_Pointer() printf("executing Pass_A_Function_Pointer \n"); PassPtr(&DoIt); 4
5 2.7 How to Return a Function Pointer? It s a little bit tricky but a function pointer can be a function s return value. In the following example there are two solutions of how to return a pointer to a function which is taking two float arguments and returns a float. // 2.7 How to Return a Function Pointer // Plus and Minus are defined above. They return a float and take two float // Direct solution: Function takes a char and returns a pointer to a // function which is taking two floats and returns a float. <opcode> // specifies which function to return float (*GetPtr1(char opcode))(float, float) if(opcode == + ) return &Plus; return &Minus; // default if invalid operator was passed // Solution using a typedef: Define a pointer to a function which is taking // two floats and returns a float typedef float(*pt2func)(float, float); // Function takes a char and returns a function pointer which is defined // with the typedef above. <opcode> specifies which function to return pt2func GetPtr2(char opcode) if(opcode == + ) return &Plus; return &Minus; // default if invalid operator was passed // Execute example code void Return_A_Function_Pointer() printf("executing Return_A_Function_Pointer \n"); // define a function pointer and initialize it to NULL float (*pt2function)(float, float) = NULL; pt2function=getptr1( + ); // get function pointer from function GetPtr1 printf("%d", (*pt2function)(2, 4)); // call function using the pointer pt2function=getptr2( - ); // get function pointer from function GetPtr2 printf("%d", (*pt2function)(2, 4)); // call function using the pointer 5
6 2.8 How to Use Arrays of Function Pointers? Operating with arrays of function pointer is very interesting. This offers the possibility to select a function using an index. The syntax appears difficult, which frequently leads to confusion. Below you find two ways of how to define and use an array of function pointers in C. The first way uses a typedef, the second way directly defines the array. It s up to you which way you prefer. // 2.8 How to Use Arrays of Function Pointers // // type-definition: pt2function now can be used as type typedef int (*pt2function)(float, char, char); // illustrate how to work with an array of function pointers void Array_Of_Function_Pointers() printf("\nexecuting Array_Of_Function_Pointers \n"); // define arrays and ini each element to NULL, <funcarr1> and <funcarr2> // are arrays with 10 pointers to functions which return an int and take a // float and two char // first way using the typedef pt2function funcarr1[10] = NULL; // 2nd way directly defining the array int (*funcarr2[10])(float, char, char) = NULL; // assign the function s address - DoIt and DoMore are suitable // functions like defined above in funcarr1[0] = funcarr2[1] = &DoIt; funcarr1[1] = funcarr2[0] = &DoMore; /* more assignments */ // calling a function using an index to address the function pointer printf("%d\n", funcarr1[1](12, a, b )); // short form printf("%d\n", (*funcarr1[0])(12, a, b )); // "correct" way of calling printf("%d\n", (*funcarr2[1])(56, a, b )); printf("%d\n", (*funcarr2[0])(34, a, b )); 6
7 3 How to Implement Callback Functions in C 3.1 Introduction to the Concept of Callback Functions Function Pointers provide the concept of callback functions. I ll try to introduce the concept of callback functions using the well known sort function qsort. This function sorts the items of a field according to a user-specific ranking. The field can contain items of any type; it is passed to the sort function using a void-pointer. Also the size of an item and the total number of items in the field has got to be passed. Now the question is: How can the sort-function sort the items of the field without any information about the type of an item? The answer is simple: The function receives the pointer to a comparison-function which takes void-pointers to two field-items, evaluates their ranking and returns the result coded as an int. So every time the sort algorithm needs a decision about the ranking of two items, it just calls the comparison-function via the function pointer. 3.2 How to Implement a Callback in C? To explain I just take the declaration of the function qsort which reads itself as follows: void qsort(void* Arr, int nelements, int sizeofanelement, int(*cmpfunc)(void *, void*)); Arr points to the first element of the field which is to be sorted, nelements is the number of items in the field, sizeofanelement the size of one item in bytes and cmpfunc is the pointer to the comparison function. This comparison function takes two void-pointers and returns an int. The syntax, how you use a function pointer as a parameter in a function-definition looks a little bit strange. Just review, how to define a function pointer and you ll see, it s exactly the same. A callback is done just like a normal function call would be done: You just use the name of the function pointer instead of a function name. This is shown below. Note: All calling arguments other than the function pointer were omitted to focus on the relevant things. void qsort(..., int(*cmpfunc)(void*, void*)) /* sort algorithm -note: item1 and item2 are void-pointers */ int bigger = cmpfunc(item1, item2); // make callback /* use the result */ 7
8 3.3 Example Code of the Usage of qsort // 3.3 How to make a callback in C by the means of the sort function qsort #include <stdlib.h> // due to: qsort #include <time.h> // randomize #include <stdio.h> // printf // comparison-function for the sort-algorithm // two items are taken by void-pointer, converted and compared int CmpFuncFloat(void* itema, void* itemb) // you ve got to explicitly cast to the correct type float* a = (float*) itema; float* b = (float*) itemb; // first item is bigger than the second one -> return 1 if(*a > *b) return 1; if(*a == *b) return 0; // equality -> return 0 return -1; // second item is bigger than the first one -> return -1 // example for the use of qsort() void QSortExample() float Arr[100]; int c; ::randomize(); // initialize random-number-generator for(int c=0;c<100;c++) // randomize all elements of the field Arr[c]=random(99); // sort using qsort() qsort((void*)arr, /*number of items*/ 100, /*size of an item*/ sizeof(arr[0]), /*comparison-function*/ CmpFuncFloat); // display first ten elements of the sorted field printf("the first ten elements of the sorted field are...\n"); for(int c=0;c<10;c++) printf("element #%d contains %.0f\n", c+1, Arr[c]); printf("\n"); 8
Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C
Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection
Sources: On the Web: Slides will be available on:
C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,
arrays C Programming Language - Arrays
arrays So far, we have been using only scalar variables scalar meaning a variable with a single value But many things require a set of related values coordinates or vectors require 3 (or 2, or 4, or more)
1 Abstract Data Types Information Hiding
1 1 Abstract Data Types Information Hiding 1.1 Data Types Data types are an integral part of every programming language. ANSI-C has int, double and char to name just a few. Programmers are rarely content
Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example
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
Lecture 22: C Programming 4 Embedded Systems
Lecture 22: C Programming 4 Embedded Systems Today s Goals Basic C programming process Variables and constants in C Pointers to access addresses Using a High Level Language High-level languages More human
Selection Statements
Chapter 5 Selection Statements 1 Statements So far, we ve used return statements and expression ess statements. e ts. Most of C s remaining statements fall into three categories: Selection statements:
Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.
Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to
The programming language C. sws1 1
The programming language C sws1 1 The programming language C invented by Dennis Ritchie in early 1970s who used it to write the first Hello World program C was used to write UNIX Standardised as K&C (Kernighan
The C Programming Language course syllabus associate level
TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming
Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)
Unit- I Introduction to c Language: C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating
Introduction to Data Structures
Introduction to Data Structures Albert Gural October 28, 2011 1 Introduction When trying to convert from an algorithm to the actual code, one important aspect to consider is how to store and manipulate
Tutorial on C Language Programming
Tutorial on C Language Programming Teodor Rus [email protected] The University of Iowa, Department of Computer Science Introduction to System Software p.1/64 Tutorial on C programming C program structure:
Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists
Lecture 11 Doubly Linked Lists & Array of Linked Lists In this lecture Doubly linked lists Array of Linked Lists Creating an Array of Linked Lists Representing a Sparse Matrix Defining a Node for a Sparse
X86-64 Architecture Guide
X86-64 Architecture Guide For the code-generation project, we shall expose you to a simplified version of the x86-64 platform. Example Consider the following Decaf program: class Program { int foo(int
Semantic Analysis: Types and Type Checking
Semantic Analysis Semantic Analysis: Types and Type Checking CS 471 October 10, 2007 Source code Lexical Analysis tokens Syntactic Analysis AST Semantic Analysis AST Intermediate Code Gen lexical errors
Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011
Applied Programming and Computer Science, DD2325/appcs5 PODF, Programmering och datalogi för fysiker, DA7 Autumn 25 Lecture 6, Huffman coding and hashing A. Maki, C. Edlund Tree When are trees used? Binary
C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu
C / C++ and Unix Programming Materials adapted from Dan Hood and Dianna Xu 1 C and Unix Programming Today s goals ú History of C ú Basic types ú printf ú Arithmetic operations, types and casting ú Intro
Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters
Interpreters and virtual machines Michel Schinz 2007 03 23 Interpreters Interpreters Why interpreters? An interpreter is a program that executes another program, represented as some kind of data-structure.
Stack Allocation. Run-Time Data Structures. Static Structures
Run-Time Data Structures Stack Allocation Static Structures For static structures, a fixed address is used throughout execution. This is the oldest and simplest memory organization. In current compilers,
MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00
MPLAB TM C30 Managed PSV Pointers Beta support included with MPLAB C30 V3.00 Contents 1 Overview 2 1.1 Why Beta?.............................. 2 1.2 Other Sources of Reference..................... 2 2
GDB Tutorial. A Walkthrough with Examples. CMSC 212 - Spring 2009. Last modified March 22, 2009. GDB Tutorial
A Walkthrough with Examples CMSC 212 - Spring 2009 Last modified March 22, 2009 What is gdb? GNU Debugger A debugger for several languages, including C and C++ It allows you to inspect what the program
Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void
1. Explain C tokens Tokens are basic building blocks of a C program. A token is the smallest element of a C program that is meaningful to the compiler. The C compiler recognizes the following kinds of
1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++
Answer the following 1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++ 2) Which data structure is needed to convert infix notations to postfix notations? Stack 3) The
Simple C Programs. Goals for this Lecture. Help you learn about:
Simple C Programs 1 Goals for this Lecture Help you learn about: Simple C programs Program structure Defining symbolic constants Detecting and reporting failure Functionality of the gcc command Preprocessor,
Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007
Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007 The Java Type System By now, you have seen a fair amount of Java. Time to study in more depth the foundations of the language,
Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions
Phys4051: C Lecture 2 & 3 Functions (Review) Comment Statements Variables & Operators Branching Instructions Comment Statements! Method 1: /* */! Method 2: // /* Single Line */ //Single Line /* This comment
5 Arrays and Pointers
5 Arrays and Pointers 5.1 One-dimensional arrays Arrays offer a convenient way to store and access blocks of data. Think of arrays as a sequential list that offers indexed access. For example, a list of
Conditions & Boolean Expressions
Conditions & Boolean Expressions 1 In C++, in order to ask a question, a program makes an assertion which is evaluated to either true (nonzero) or false (zero) by the computer at run time. Example: In
Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C
Basic Java Constructs and Data Types Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C 1 Contents Hello World Program Statements Explained Java Program Structure in
Example of a Java program
Example of a Java program class SomeNumbers static int square (int x) return x*x; public static void main (String[] args) int n=20; if (args.length > 0) // change default n = Integer.parseInt(args[0]);
Member Functions of the istream Class
Member Functions of the istream Class The extraction operator is of limited use because it always uses whitespace to delimit its reads of the input stream. It cannot be used to read those whitespace characters,
ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER
ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER Pierre A. von Kaenel Mathematics and Computer Science Department Skidmore College Saratoga Springs, NY 12866 (518) 580-5292 [email protected] ABSTRACT This paper
MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.
Exam Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) The JDK command to compile a class in the file Test.java is A) java Test.java B) java
Stacks. Linear data structures
Stacks Linear data structures Collection of components that can be arranged as a straight line Data structure grows or shrinks as we add or remove objects ADTs provide an abstract layer for various operations
13 Classes & Objects with Constructors/Destructors
13 Classes & Objects with Constructors/Destructors 13.1 Introduction In object oriented programming, the emphasis is on data rather than function. Class is a way that binds the data & function together.
www.sahajsolns.com Chapter 4 OOPS WITH C++ Sahaj Computer Solutions
Chapter 4 OOPS WITH C++ Sahaj Computer Solutions 1 Session Objectives Classes and Objects Class Declaration Class Members Data Constructors Destructors Member Functions Class Member Visibility Private,
Keil C51 Cross Compiler
Keil C51 Cross Compiler ANSI C Compiler Generates fast compact code for the 8051 and it s derivatives Advantages of C over Assembler Do not need to know the microcontroller instruction set Register allocation
Chapter One Introduction to Programming
Chapter One Introduction to Programming 1-1 Algorithm and Flowchart Algorithm is a step-by-step procedure for calculation. More precisely, algorithm is an effective method expressed as a finite list of
COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.
COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms 1 Activation Records activation declaration location Recall that an activation
Parameter Passing in Pascal
Parameter Passing in Pascal Mordechai Ben-Ari Department of Science Teaching Weizmann Institute of Science Rehovot 76100 Israel [email protected] Abstract This paper claims that reference parameters
PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?
1. Distinguish & and && operators. PART-A Questions 2. How does an enumerated statement differ from a typedef statement? 3. What are the various members of a class? 4. Who can access the protected members
Java Interview Questions and Answers
1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write and compile the java
C Programming Review & Productivity Tools
Review & Productivity Tools Giovanni Agosta Piattaforme Software per la Rete Modulo 2 Outline Preliminaries 1 Preliminaries 2 Function Pointers Variadic Functions 3 Build Automation Code Versioning 4 Preliminaries
Friendship and Encapsulation in C++
Friendship and Encapsulation in C++ Adrian P Robson Department of Computing University of Northumbria at Newcastle 23rd October 1995 Abstract There is much confusion and debate about friendship and encapsulation
Object Oriented Software Design II
Object Oriented Software Design II Introduction to C++ Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 20, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February
Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct
Dr. Martin O. Steinhauser University of Basel Graduate Lecture Spring Semester 2014 Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct Friday, 7 th March
Numbering Systems. InThisAppendix...
G InThisAppendix... Introduction Binary Numbering System Hexadecimal Numbering System Octal Numbering System Binary Coded Decimal (BCD) Numbering System Real (Floating Point) Numbering System BCD/Binary/Decimal/Hex/Octal
Arrays in Java. Working with Arrays
Arrays in Java So far we have talked about variables as a storage location for a single value of a particular data type. We can also define a variable in such a way that it can store multiple values. Such
An Introduction to Assembly Programming with the ARM 32-bit Processor Family
An Introduction to Assembly Programming with the ARM 32-bit Processor Family G. Agosta Politecnico di Milano December 3, 2011 Contents 1 Introduction 1 1.1 Prerequisites............................. 2
CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team
CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team Lecture Summary In this lecture, we learned about the ADT Priority Queue. A
Introduction to Java
Introduction to Java The HelloWorld program Primitive data types Assignment and arithmetic operations User input Conditional statements Looping Arrays CSA0011 Matthew Xuereb 2008 1 Java Overview A high
Section 4.1 Rules of Exponents
Section 4.1 Rules of Exponents THE MEANING OF THE EXPONENT The exponent is an abbreviation for repeated multiplication. The repeated number is called a factor. x n means n factors of x. The exponent tells
Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009
Software Engineering Concepts: Testing Simple Class Example continued Pointers & Dynamic Allocation CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009 Glenn G. Chappell Department
Getting Started with the Internet Communications Engine
Getting Started with the Internet Communications Engine David Vriezen April 7, 2014 Contents 1 Introduction 2 2 About Ice 2 2.1 Proxies................................. 2 3 Setting Up ICE 2 4 Slices 2
Symbol Tables. Introduction
Symbol Tables Introduction A compiler needs to collect and use information about the names appearing in the source program. This information is entered into a data structure called a symbol table. The
Boolean Data Outline
1. Boolean Data Outline 2. Data Types 3. C Boolean Data Type: char or int 4. C Built-In Boolean Data Type: bool 5. bool Data Type: Not Used in CS1313 6. Boolean Declaration 7. Boolean or Character? 8.
COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012
Binary numbers The reason humans represent numbers using decimal (the ten digits from 0,1,... 9) is that we have ten fingers. There is no other reason than that. There is nothing special otherwise about
Pemrograman Dasar. Basic Elements Of Java
Pemrograman Dasar Basic Elements Of Java Compiling and Running a Java Application 2 Portable Java Application 3 Java Platform Platform: hardware or software environment in which a program runs. Oracle
C++FA 5.1 PRACTICE MID-TERM EXAM
C++FA 5.1 PRACTICE MID-TERM EXAM This practicemid-term exam covers sections C++FA 1.1 through C++FA 1.4 of C++ with Financial Applications by Ben Van Vliet, available at www.benvanvliet.net. 1.) A pointer
Lecture 27 C and Assembly
Ananda Gunawardena Lecture 27 C and Assembly This is a quick introduction to working with x86 assembly. Some of the instructions and register names must be check for latest commands and register names.
KITES TECHNOLOGY COURSE MODULE (C, C++, DS)
KITES TECHNOLOGY 360 Degree Solution www.kitestechnology.com/academy.php [email protected] [email protected] Contact: - 8961334776 9433759247 9830639522.NET JAVA WEB DESIGN PHP SQL, PL/SQL
Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism
Polymorphism Problems with switch statement Programmer may forget to test all possible cases in a switch. Tracking this down can be time consuming and error prone Solution - use virtual functions (polymorphism)
Static Code Analysis Procedures in the Development Cycle
Static Code Analysis Procedures in the Development Cycle Tools, Technology, and Process in Engineering at Microsoft Mooly Beeri Microsoft Haifa R&D Center Agenda Static code analysis tools PREfix and PREfast
Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas
CS 110B - Rule Storage Classes Page 18-1 Attributes are distinctive features of a variable. Data type, int or double for example, is an attribute. Storage class is another attribute. There are four storage
Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C
Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C 1 An essential part of any embedded system design Programming 2 Programming in Assembly or HLL Processor and memory-sensitive
1 Description of The Simpletron
Simulating The Simpletron Computer 50 points 1 Description of The Simpletron In this assignment you will write a program to simulate a fictional computer that we will call the Simpletron. As its name implies
Section 1.5 Exponents, Square Roots, and the Order of Operations
Section 1.5 Exponents, Square Roots, and the Order of Operations Objectives In this section, you will learn to: To successfully complete this section, you need to understand: Identify perfect squares.
In this Chapter you ll learn:
Now go, write it before them in a table, and note it in a book. Isaiah 30:8 To go beyond is as wrong as to fall short. Confucius Begin at the beginning, and go on till you come to the end: then stop. Lewis
Passing 1D arrays to functions.
Passing 1D arrays to functions. In C++ arrays can only be reference parameters. It is not possible to pass an array by value. Therefore, the ampersand (&) is omitted. What is actually passed to the function,
5544 = 2 2772 = 2 2 1386 = 2 2 2 693. Now we have to find a divisor of 693. We can try 3, and 693 = 3 231,and we keep dividing by 3 to get: 1
MATH 13150: Freshman Seminar Unit 8 1. Prime numbers 1.1. Primes. A number bigger than 1 is called prime if its only divisors are 1 and itself. For example, 3 is prime because the only numbers dividing
10CS35: Data Structures Using C
CS35: Data Structures Using C QUESTION BANK REVIEW OF STRUCTURES AND POINTERS, INTRODUCTION TO SPECIAL FEATURES OF C OBJECTIVE: Learn : Usage of structures, unions - a conventional tool for handling a
ASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6)
ASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6) 1 COMPUTER LANGUAGES In order for a computer to be able to execute a program, the program must first be present
6 3 4 9 = 6 10 + 3 10 + 4 10 + 9 10
Lesson The Binary Number System. Why Binary? The number system that you are familiar with, that you use every day, is the decimal number system, also commonly referred to as the base- system. When you
Chapter 5 Names, Bindings, Type Checking, and Scopes
Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Scope Scope and Lifetime Referencing Environments Named
6.S096 Lecture 1 Introduction to C
6.S096 Lecture 1 Introduction to C Welcome to the Memory Jungle Andre Kessler Andre Kessler 6.S096 Lecture 1 Introduction to C 1 / 30 Outline 1 Motivation 2 Class Logistics 3 Memory Model 4 Compiling 5
Basic Object-Oriented Programming in Java
core programming Basic Object-Oriented Programming in Java 1 2001-2003 Marty Hall, Larry Brown http:// Agenda Similarities and differences between Java and C++ Object-oriented nomenclature and conventions
C++ Programming Language
C++ Programming Language Lecturer: Yuri Nefedov 7th and 8th semesters Lectures: 34 hours (7th semester); 32 hours (8th semester). Seminars: 34 hours (7th semester); 32 hours (8th semester). Course abstract
Regions in a circle. 7 points 57 regions
Regions in a circle 1 point 1 region points regions 3 points 4 regions 4 points 8 regions 5 points 16 regions The question is, what is the next picture? How many regions will 6 points give? There's an
Comp151. Definitions & Declarations
Comp151 Definitions & Declarations Example: Definition /* reverse_printcpp */ #include #include using namespace std; int global_var = 23; // global variable definition void reverse_print(const
Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)
Semantic Analysis Scoping (Readings 7.1,7.4,7.6) Static Dynamic Parameter passing methods (7.5) Building symbol tables (7.6) How to use them to find multiply-declared and undeclared variables Type checking
Lab Experience 17. Programming Language Translation
Lab Experience 17 Programming Language Translation Objectives Gain insight into the translation process for converting one virtual machine to another See the process by which an assembler translates assembly
Object Oriented Software Design II
Object Oriented Software Design II C++ intro Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 26, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February 26,
Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero THEN the Average is Sum divided by Count Conditions
C++FA 3.1 OPTIMIZING C++
C++FA 3.1 OPTIMIZING C++ Ben Van Vliet Measuring Performance Performance can be measured and judged in different ways execution time, memory usage, error count, ease of use and trade offs usually have
2) Write in detail the issues in the design of code generator.
COMPUTER SCIENCE AND ENGINEERING VI SEM CSE Principles of Compiler Design Unit-IV Question and answers UNIT IV CODE GENERATION 9 Issues in the design of code generator The target machine Runtime Storage
Chapter 7D The Java Virtual Machine
This sub chapter discusses another architecture, that of the JVM (Java Virtual Machine). In general, a VM (Virtual Machine) is a hypothetical machine (implemented in either hardware or software) that directly
Time Limit: X Flags: -std=gnu99 -w -O2 -fomitframe-pointer. Time Limit: X. Flags: -std=c++0x -w -O2 -fomit-frame-pointer - lm
Judge Environment Language Compilers Language Version Flags/Notes Max Memory Limit C gcc 4.8.1 Flags: -std=gnu99 -w -O2 -fomit-frame-pointer - lm C++ g++ 4.8.1 Flags: -std=c++0x -w -O2 -fomit-frame-pointer
Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved.
1 2 Introduction to Java Applications 2.2 First Program in Java: Printing a Line of Text 2 Application Executes when you use the java command to launch the Java Virtual Machine (JVM) Sample program Displays
C++ Overloading, Constructors, Assignment operator
C++ Overloading, Constructors, Assignment operator 1 Overloading Before looking at the initialization of objects in C++ with constructors, we need to understand what function overloading is In C, two functions
This explains why the mixed number equivalent to 7/3 is 2 + 1/3, also written 2
Chapter 28: Proper and Improper Fractions A fraction is called improper if the numerator is greater than the denominator For example, 7/ is improper because the numerator 7 is greater than the denominator
Parameter Passing. Standard mechanisms. Call by value-result Call by name, result
Parameter Passing Standard mechanisms Call by value Call by reference Other methods Call by value-result Call by name, result Terms Function definition where the details of the function are presented (type,
A PRACTICAL GUIDE TO db CALCULATIONS
A PRACTICAL GUIDE TO db CALCULATIONS This is a practical guide to doing db (decibel) calculations, covering most common audio situations. You see db numbers all the time in audio. You may understand that
PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE
PROGRAMMING IN C CONTENT AT A GLANCE 1 MODULE 1 Unit 1 : Basics of Programming Unit 2 : Fundamentals Unit 3 : C Operators MODULE 2 unit 1 : Input Output Statements unit 2 : Control Structures unit 3 :
UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming
UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming 1 2 Foreword First of all, this book isn t really for dummies. I wrote it for myself and other kids who are on the team. Everything
Lecture 12 Doubly Linked Lists (with Recursion)
Lecture 12 Doubly Linked Lists (with Recursion) In this lecture Introduction to Doubly linked lists What is recursion? Designing a node of a DLL Recursion and Linked Lists o Finding a node in a LL (recursively)
