Generic Programming in C

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

1 Lecture13: Other C Topics 12/17/2012. Topics Variable-length argument lists Pointers to functions Command-line arguments Suffixes for integer and floating-point.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Recursive Sorting Why is recursive sorting faster ? Merge Sort Quick Sort Description Quick Sort Pseudocode Choice of Quick Sort pivot record The Quick.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Built into qsort is a function that can swap two given array elements.
21-2 Understand various methods of sorting. Use the qsort function to sort. Binary search Related Chapter: ABC 8.5.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Pointers to Functions In C programming language. Introduction  While many programming languages support the concept of pointers to data, only a few enable.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Arrays Chapter 7.
Motivation for Generic Programming in C++
CSC215 Lecture Advanced Pointers.
CSC215 Lecture Advanced Pointers.
Introduction to Linked Lists
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Chapter 10: Pointers Starting Out with C++ Early Objects
Chapter 7 Part 1 Edited by JJ Shepherd
CSE 374 Programming Concepts & Tools
Pointers and Pointer-Based Strings
Student Book An Introduction
Pointers Revisited What is variable address, name, value?
Lecture 6 C++ Programming
Pointers and References
Introduction to Linked Lists
Some examples.
Pointers and References
Dynamic Memory Allocation
Chapter 9: Pointers.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Alternate Version of STARTING OUT WITH C++ 4th Edition
Pointers, Dynamic Data, and Reference Types
Data Structures and Algorithms
More About Data Types & Functions
Built-In (a.k.a. Native) Types in C++
Miscellaneous functions
Pointers and Dynamic Variables
Classes and Objects.
Outline Defining and using Pointers Operations on pointers
Qsort.
CS111 Computer Programming
TUTORIAL 11 CS 137 F18 November 27th.
C++ Pointers and Strings
Pointers Pointers point to memory locations
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
Submitted By : Veenu Saini Lecturer (IT)
Type compatibility and pointer operation
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Chapter 18 Recursion.
登金陵鳳凰臺 ~李白 鳳凰臺上鳳凰遊, 鳳去臺空江自流。 吳宮花草埋幽徑, 晉代衣冠成古丘。 三山半落青山外, 二水中分白鷺洲。
C++ Pointers and Strings
pointer-to-pointer (double pointer)
Introduction to Pointers
Presentation transcript:

Generic Programming in C

The Goal To write code once that works on a variety of types. The tools: pointers to functions void* polymorphism (C++, not called Generic programming) templates (C++, “pure” Generic programming)

void* A way to pass data of an arbitrary type.

Rules regarding void * void* is a generic pointer capable of representing any pointer type. int i= 5; double f= 3.14; void* p; p= &i; // ok p= &f; // ok

Rules regarding void * void* pointer cannot be dereferenced. int i= 5; double f= 3.14; void* p; p= &i; // ok p= &f; // ok printf("%f\n",*p)); // ERROR

void* can be explicitly cast to another pointer type (for example here it's double*) int i= 5; double f= 3.14; void* p; p= &i; // ok p= &f; // ok printf("%f\n",*((double*)p)); // ok Rules regarding void *

void* example: swap_e.c

Pointers to Functions Assuming the function f is defined, &f and f are pointers to the function. i.e: The address where the function's definition begins in memory.

Example int avg(int num1, int num2) { return ( num1 + num2 ) / 2; } int (* func)(int, int); // a ptr variable func = &avg; // assignment func = avg; // same int result = (*func)(20, 30); // invoke it result = func(20, 30); // same

Suggestion Use typedef ! typedef int (* TwoIntsFunc) (int, int); TwoIntsFunc f1; f1 = &avg; ... f1 = ∑

What is it good for? Generic programming - pass a function name to another function as a parameter. Once upon a time it was a way to make a c struct kind of a poor class.

qsort #include <stdlib.h> void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); Array to be sorted # elements in array sizeof each element in array Pointer to the comparator function. Return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

qsort_e.c

qsort problems Not efficient – casting, calls to functions. Not user freindly. Type safety problems. void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));

C++ solves all this problems efficient – no casting, usually no calls to functions. User friendly - sort(arr,arr+ARR_SIZE) Type safety. Current implementation is a different algorithm – introspective sort. We will learn more about it in C++ course