0% found this document useful (0 votes)
37 views

05 Applicative Programming

The document discusses computer architecture concepts like registers and caches. It introduces header files and how they allow functions to be declared in separate files so that code can be split across multiple files yet still compile correctly. It discusses how header files contain function declarations while code files can contain function definitions and calls, resolving the symbol definitions at link time. Recurring issues like the von Neumann bottleneck and cache hierarchies are also covered.

Uploaded by

anidcohen9058
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

05 Applicative Programming

The document discusses computer architecture concepts like registers and caches. It introduces header files and how they allow functions to be declared in separate files so that code can be split across multiple files yet still compile correctly. It discusses how header files contain function declarations while code files can contain function definitions and calls, resolving the symbol definitions at link time. Recurring issues like the von Neumann bottleneck and cache hierarchies are also covered.

Uploaded by

anidcohen9058
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

5. Applicative Programming 1. Juli 2011

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 1 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Outline

Recapitulation Computer architecture extended:

Registers and caches


Header les Global variables and constants Namespaces Loops revisited Enums

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 2 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Recapitulation
i n t foo ( i n t a , i n t b ) { i f ( a==0) { return b; } b = b%a ; }

What is missing in this code?

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Recapitulation
i n t foo ( i n t a , i n t b ) { i f ( a==0) { return b; } b = b%a ; }

What is missing in this code? What does the return b do?

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Recapitulation
i n t foo ( i n t a , i n t b ) { i f ( a==0) { return b; } b = b%a ; }

What is missing in this code? What does the return b do? What happens if we replae a==0 with a=0?

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Recapitulation
i n t foo ( i n t a , i n t b ) { i f ( a==0) { int a; a=b ; return b; } b = b%a ; return b; }

What value does a have in each single code line?

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 4 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Recapitulation
i n t foo ( i n t a , i n t b ) { i f ( a==0) { int a; a=b ; return b; } b = b%a ; return b; }

What value does a have in each single code line? What happens with the following snippet? i n t a =10; i n t b =20; i n t r =foo ( a , b )

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 4 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Recapitulation
i n t foo ( i n t a , i n t b ) { i f ( a==0) { int a; a=b ; return b; } b = b%a ; return b; }

What value does a have in each single code line? What happens with the following snippet? i n t a =10; i n t b =20; i n t r =foo ( a , b )

Can foo() modify the value of the variable a in our main application?

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 4 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Recapitulation
i n t foo ( i n t a , i n t b ) { i f ( a==0) { int a; a=b ; return b; } b = b%a ; return b; }

What value does a have in each single code line? What happens with the following snippet? i n t a =10; i n t b =20; i n t r =foo ( a , b )

Can foo() modify the value of the variable a in our main application?

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 4 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

5.1. Computer Architecture Excursus: Registers and Caches

John von Neumann


19031957 Manhattan Project (Los Alamos) June 30, 1945 (but Turing et. al. published similar ideas)

Computer Consists of Four

Components
There is a Von-Neumann bottleneck

John von Neumann

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 5 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

The Neumann Bottleneck

One or two documents in the ofce (two registers in the ALU) aint sufcient. Introduce more registers (Itanium e.g. has 128 of them). However, number of registers still is limited.

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 6 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

The Neumann Bottleneck

Running into the basement is time consuming, and The bigger the basement (memory), the slower the search becomes. The faster the processor, the more annoying the slow search in the memory is. Can we study this effect?

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 7 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

The Memory Gap

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 8 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Idea of a Cache

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 9 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Cache Levels

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 10 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Caches
Computers have a hierarchy of caches and lots of registers. The time to nish one operation depends signicantly on where the data is located

right now.
It is important for many algorithms to exhibit spatial locality and temporal locality. Brain teaser 1: How would you implement a matrix-vector product? Brain teaser 2: What is the fundamental challenge if we transpose a matrix? Brain teaser 3: What is the best way to run through a Cartesian grid?

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 11 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

5.2. Header Files


C/C++ distinguishes declaration and denition. Motivation for this is still missing and requires a more general denition than

before.
Declaration: Tell the compiler what names are available. Denition: Dene where the name is stored.

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 12 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Functions and Their Declaration


void foo ( i n t a ) { / / do something i n t e l l i g e n t } ... foo ( 4 4 ) ;

This denition is both a declaration and a denition. A function call is replaced by a reset of the program counter (and some additional

things).
Sometimes, wed like to split up les into several les.

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 13 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Functions and Their Declaration


/ / File A void foo ( i n t a ) { / / do something i n t e l l i g e n t } / / File B v o i d bar ( i n t a ) { foo ( a +2); }

We can invent some clever C/C++-concatenate operation (C/C++ actually has such a thing) and make le B compile.

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 14 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

. . . However
/ / File A void foo ( i n t a ) { / / do something i n t e l l i g e n t } / / File B v o i d bar ( i n t a ) { foo ( a +2); } / / File C void t a r ( i n t a ) { foo ( a +4); }

We can invent some clever C/C++-concatenate operation (C/C++ actually has such a thing) and make le B and le C compile. However, the linking process fails as the symbol foo() is dened twice.

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 15 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Function Denitions
/ / File A void foo ( i n t a ) { / / do something i n t e l l i g e n t } / / File B void foo ( i n t a ) ; v o i d bar ( i n t a ) { foo ( a +2); } / / File C void foo ( i n t a ) ; void t a r ( i n t a ) { foo ( a +4); }

First, this code compiles ne. Second, this code links ne, if we pass the linker all three les. Third, we now know why! The compiler does not resolve the program counter

modications, but the linker does.


5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 16 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Function Denitions
Writing the functions declaration over and over again is cumbersome and error prone. Lets put it into a le of its own. Files containing solely declarations, are called header les and typically have the extension .h or .hpp.
/ / F i l e my . h void foo ( i n t a ) ; / / F i l e my . cpp # i n c l u d e my . h void foo ( i n t a ) { / / do something i n t e l l i g e n t } / / File B # i n c l u d e my . h v o i d bar ( i n t a ) { foo ( a +2); } / / File C # i n c l u d e my . h void t a r ( i n t a ) { foo ( a +4); }
5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 17 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Header Files
/ / F i l e my . h void foo ( i n t a ) ; / / F i l e my . cpp # i n c l u d e my . h void foo ( i n t a ) { / / do something i n t e l l i g e n t }

A good header le never contains denitions. A good header les has lots of comments as other editors of les see it frequently. A header le is typically accompanied by one implementation le (best practice).

Every C/C++ and every UNIX installation comes along with tons of header les.

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 18 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Linker Usage
C comes along with lots of useful standard libraries. These libraries provide tons of useful operations.

See math.h for example or iostream which provides the terminal output function << (this is also just a function with the name <<).

We add denitions due to include statements. We add the compiled les either by

passing the object les to the linker or passing the library les (a,so) due to the -l argument with -L giving the compiler the library search path.

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 19 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

5.3. The Precompiler and Global Variables Circulant Declarations


/ / File a.h void foo ( i n t a ) ; void foo ( i n t a ) ; / / F i l e b . cpp # i n c l u d e my . h v o i d bar ( i n t a ) ; / / F i l e b . cpp # i n c l u d e my . h # i n c l u d e my . h / / m u l t i p l e d e f i n i t i o n s . Has t o be removed v o i d bar ( i n t a ) ;

/ / m u l t i p l e d e f i n i t i o n s . Has t o be removed

Sometimes, it is pain to remove multiple denitions. In particular, if a headers include other headers (transitivity of includes).

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 20 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Include Guards
/ / File a.h # i f n d e f INCLUDE A H # d e f i n e INCLUDE A H void foo ( i n t a ) ; #endif / / File b.h # i n c l u d e my . h v o i d bar ( i n t a ) ; / / F i l e b . cpp #include b . h v o i d bar ( i n t a ) {\ l d o t s } / / File c .h # i n c l u d e my . h #include b . h void t a r ( ) ; / / F i l e c . cpp #include c . h v o i d t a r ( ) {\ l d o t s }

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 21 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

The C/C++ Precompiler


/ / T h i s i s my header # i f n d e f IDENTIFIER FOR MY HEADER H # d e f i n e IDENTIFIER FOR MY HEADER H / / A l l t h e d e c l a r a t i o n s here #endif / / T h i s i s my i m p l e m e n t a t i o n # i n c l u d e myheader . h / / a l l t h e d e f i n i t i o n s here

The C/C++ is a powerful precompiler (does something similar like cut-n-paste). Its symbol mechanism allows us to avoid multiple declarations. Embed every header le into ifndef-define-endif statements. Precompiler can do a lot of additional things we wont discuss here.

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 22 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Global Variables and Constants


/ / T h i s i s my header # i f n d e f IDENTIFIER FOR PI HEADER H # d e f i n e IDENTIFIER FOR PI HEADER H / / A l l t h e d e c l a r a t i o n s here c o n s t double PI ; / / T e l l s c o m p i l e r t h a t t h i s v a r i a b l e may n o t be / / changed . #endif / / T h i s i s my i m p l e m e n t a t i o n #include pi . h c o n s t double PI = 2 0 ; / / Another f i l e #include pi . h \ldots double myValue ; \ldots myValue = p i ; Remember difference of declaration and denition. Other les only need the denition of the variable (which is a constant here). Linker then is responsible to bring together denition (address) and references to
5. Applicative Programming

the symbol.
page 23 of 41

Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Multiple Declarations with Different Context


/ / T h i s i s header A # i f n d e f IDENTIFIER FOR HEADER A H # d e f i n e IDENTIFIER FOR HEADER A H / C a l c u l a t e s f o r c e between two p a r t i c l e s ( LennardJones ) / f u n c t i o n double getForce ( c o n s t double& d i s t a n c e ) ; #endif / / T h i s i s header B # i f n d e f IDENTIFIER FOR HEADER B H # d e f i n e IDENTIFIER FOR HEADER B H / C a l c u l a t e s f o r c e between two p a r t i c l e s (WCA model ) / f u n c t i o n double getForce ( c o n s t double& d i s t a n c e ) ; #endif Sometimes, functions and variables have the same name as they were developed

by different people and written into different les.


It is very difcult to distinguish between these names, i.e. we have to analyse the

(indirect) include paths.


Sometimes, we might want to use both functions within one implementation le.
5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 24 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Namespaces
/ / T h i s i s header A # i f n d e f IDENTIFIER FOR HEADER A H # d e f i n e IDENTIFIER FOR HEADER A H namespace lennardJones { / C a l c u l a t e s f o r c e between two p a r t i c l e s ( LennardJones ) / double getForce ( c o n s t double& d i s t a n c e ) ; } #endif / / T h i s i s header B # i f n d e f IDENTIFIER FOR HEADER B H # d e f i n e IDENTIFIER FOR HEADER B H namespace wca { / C a l c u l a t e s f o r c e between two p a r t i c l e s (WCA model ) / double getForce ( c o n s t double& d i s t a n c e ) ; } #endif

With namespaces, we can group variables, constants, and functions into logic subsets.
5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 25 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Using Namespaces
/ / T h i s i s t h e header # i f n d e f IDENTIFIER FOR HEADER A H # d e f i n e IDENTIFIER FOR HEADER A H namespace lennardJones { / C a l c u l a t e s f o r c e between two p a r t i c l e s ( LennardJones ) / double getForce ( c o n s t double& d i s t a n c e ) ; } #endif / / This i s the implementation # i n c l u d e lennardJones . h double lennardJones : : getForce ( c o n s t double& d i s t a n c e ) { \ldots } Namespaces are predecessors of the identier. Namespaces can be embedded into each other. Alternatively, you can use u s i n g namespace lennardJones ;
5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 26 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

The Standard Library


# i n c l u d e <iostream > i n t main ( ) { i n t a = 1; std : : cout < < my d a r l i n g i s my no . < < a< < std : : endl ; return 0; }

All the functions, constants, . . . of the standard library are embedded into the

namespace std.
std::endl is just a constant whose denition can be found in iostream or a

header includes by iostream.


Hint: Never use using namespace within a header le.

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 27 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

5.4. Loops
Create three les: main.cpp,

fibonacci.cpp, and fibonacci.h.


Dene a function void

printFibonacci(int max) in fibonacci.cpp. It shall be embedded into the namespace fib.


Make printFibonacci(int max) print all

Fibonacci numbers Fn = Fn1 + Fn2 from Fn {0, max } .


At least F0 and F1 shall be printed! Realise the computation with a while

F0 = 0 F1 = 1

loop, or do it recursively.

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 28 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Do-While Loop

do { / / something i n t e l l i g e n t } while ( expression ) ;

Rewrite your code using the do-while loop.

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 29 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

The For-Loop
i n t a =0; do { / / something i n t e l l i g e n t a ++; } w h i l e ( a < 20);

Often, we want to run through a sequence with xed size. While loops are error-prone as we might forget the increment. Alternatively: Use the for loop. f o r ( i n t a =0; a <20; a++) { / / something i n t e l l i g e n t }

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 30 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Semantics of the For Loop


f o r ( i n t a =0; a <20; a++) { / / something i n t e l l i g e n t }

The for statements opens a new scope. Statement int a creates a new variable (loop counter) within this scope. Statement a<20 is the termination criterion. It is evaluated before one iterate. Statement a++ is an increment.

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 31 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Scoping within a For Loop


i n t a =20; f o r ( i n t a =0; a <20; a++) { / / a i s hidden w i t h i n l o o p body / / something i n t e l l i g e n t }

f o r ( i n t a =0; a <20; ) { / / something i n t e l l i g e n t } a+=20; / / a a i n t known o u t s i d e t h e scope

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 32 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

A For Loop is a While Loop


f o r ( i n t a =0; a <20; a++) { / / a i s hidden w i t h i n l o o p body / / something i n t e l l i g e n t }

{ i n t a =0; w h i l e ( a <20) { / / something i n t e l l i g e n t a ++; } }

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 33 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

What do These Snippets Do?


f o r ( i n t a =0; a <20; a++) { / / something i n t e l l i g e n t a =2; }

f o r ( i n t a =0; a <20; a++) { / / something i n t e l l i g e n t a =2; }

f o r ( i n t a =0; a <20; a =2) { / / something i n t e l l i g e n t }

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 34 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

What do These Snippets Do?


f o r ( i n t a =0; a < 20;) { / / something i n t e l l i g e n t a +=2; }

i n t b =2; f o r ( i n t a=b 2; a < 20;) { / / something i n t e l l i g e n t b ++; }

f o r ( i n t a =0; a <20; a + + ) ; { / / something i n t e l l i g e n t }

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 35 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

For Best Practices


f o r ( i n t a =0; a < 20;) { / / something i n t e l l i g e n t a +=2; }

Use for for xed ranges (compiler can optimise and parallelise). Do not invoke a function in the guard or increment section. Always open brackets after a for loop. Do not manipulate the counter within a for loop (error-prone; optimisation).

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 36 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

5.5. Enums
In the next session, we will create more sophisticated data structures such as sequences of variables and complex data types that represent whole records (like tuples of time and measurement in an experiment). Before, we however have to talk about the last (primitive) datatypeenumerations.
enum Colour { Red , Green , Blue };

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 37 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Enums in Action
enum Colour { Red , Green , Blue }; \ldots Colour myColour = Red ; \ldots i f ( myColour==Green ) { ... }

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 38 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Enums
enum Colour { Red , Green , Blue };

Mind the semicolon terminating the enum denition. Enums are basically integers (no type safety). Enum variants belong the enclosing namespace.

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 39 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Enums and Integers


enum Colour { Red , Green , Blue }; / Maps c o l o u r t o a grey v a l u e / i n t getGreyValue ( c o n s t Colour& c o l o u r ) { ... } ... i n t a = getGreyValue ( 1 7 ) ;

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 40 of 41

Computer Architecture Excursus: Registers and Caches

Header Files

The Precompiler and Global Variables

Loops

Enums

Enums and Namespaces


namespace mySpace { enum Colour { Red , / / i s mapped t o 0 Green , / / i s mapped t o 1 Blue / / i s mapped t o 2 }; enum TUMColours { Blue , / / i s mapped t o 0 Orange , / / i s mapped t o 1 White , / / i s mapped t o 2 Black / / i s mapped t o 3 } } mySpace : : Colour c o l o u r = mySpace : : Blue ; / / what happens?

5. Applicative Programming Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 41 of 41

You might also like