EEEE3084 - Lab Worksheet 2 - Generic Programming 1
P Evans
October 6, 2025
1 Summary
These are some practice exercises that will allow you to do the coursework questions, they are not assessed but
they are similar to the coursework questions. I cannot help you do the coursework questions (I can’t tell you
how to answer assessed questions), but I can help you do these - so although these are not assessed, there is a
reason why you are asked to complete them in the labs!
2 Install the necessary software and test it
3 Design and implement a templated function that evaluates the
square of its argument
Design and implement a templated function that evaluates the square of its argument where the argument type
is to be a template parameter. Specifically, the square of the argument, x, is be evaluated as x*x. x can be any
type for which the * operator is defined.
4 Design and implement a templated function that evaluates the
distance between two 2D points
Design and implement a templated function that evaluates the distance between two 2D points a and b. Each
point is characterised by its x and y coordinates held in a 2-element array. (Be careful not to name the function
”distance” - this is the name of a built-in function)
1 f l o a t a [2]={ 1 , 0 };
2 f l o a t b [ 2 ] = { 3 , =7 }
3 f l o a t d i s t a n c e b e t w e e n p o i n t s a b=p o i n t d i s t a n c e ( a , b ) ;
4
5 d o u b l e c [ 2 ] = { 1 , =2 } ;
6 double d [2]={ 3 , 3 }
7 d o u b l e d i s t a n c e b e t w e e n p o i n t s c d=p o i n t d i s t a n c e ( c , d ) ;
8
5 Design and implement a templated function, get new array()
Design and implement a templated function, get new array(). This is to return a dynamically allocated array of
a certain size and type. Template on both the type and the integer size of the array. To be used like
1
1 f l o a t * n e w a r r a y=g e t n e w a r r a y <f l o a t ,7 >() ;
2
6 Design, implement and test a templated function to perform ”deref-
erencing”
Design, implement and test a templated function to perform ”dereferencing” - i.e. the argument is a pointer to
an object and the return value is a reference to the object. This proves useful with the STL later on. Maybe
use like (there are better reasons for having this function later in the module):
1 c l a s s m o t or b i k e ;
2 m o t o r b i k e * m = new mo t o r bi k e ;
3 m o t o r b i k e& M = d e r e f e r e n c e <motorbike >(m) ;
4
7 Design and implement a clone function class
Design and implement a clone function class whose ”function operator” accepts a reference to an existing object
and returns a reference to new object of the same type which is a copy of it. Basically the usage is of the form:
1 c l o n e r <motorbike> a c l o n e r ; // I f u s i n g V e r s i o n #1 s o l u t i o n
2 // o r
3 c l o n e r a c l o n e r ; // I f u s i n g V e r s i o n #2 s o l u t i o n
4 m o t o r b i k e m;
5 m o t o r b i k e &n = a c l o n e r (m) ;
6
which overall is just the same as writing:
1 m o t o r b i k e m;
2 m o t o r b i k e &n ( * ( new mo t o r bi k e (m) ) ) ;
3 // o r
4 m o t o r b i k e n (m) ;
5
Why bother? The cloner hides the way memory is allocated for the object. For now, the cloner might just use
new once for each requested clone, however you’d have the option to edit the cloner class so that, for example,
it preallcoated memory in blocks to minimse the number of calls to new. This could be done without ever
changing anything other than the cloner class: the rest of the program just uses the cloner without needing to
worry about how it does the allocation.
8 Further practice with the tools
Repeat the polymorphic/monomorphic example from the lectures last week (the code is on Moodle). Build the
programs with and without optimisation, and open each with Ghidra. The idea here is really just to go through
the process and to have a play with the tool, but there are some things to look for / think about below - you
are not expected to know the answers at the moment, but these are things we can discuss in the lab session if
you are interested and we’ll come back to some of them in the coming weeks.
Try to understand how the programs are actually working and what difference the optimser makes by
looking at the decompiled source code.
2
Try to understand how the decompiled code changes when you change the optimiser settings (i.e. -O1,
-O2, -O3). If you lookup the optimiser documentation it will tell you what each level introduces. Some of
these things (you might see a comment about vectorisation) will be mentioned in the coming lectures.
Something to think about: Look at the registers that the program is using (look in the disassembly
window, the one with the numbers and instructions). Registers are temporary storage locations on the
processor used for calculations, they are very fast compared to RAM as they are on-chip. The ”standard”
64-bit registers are named RAX, RBX, etc (in general 64-bit registers are named RXX) and there are
32-bit equivalents EAX, EBX, etc (in general 32-bit registers are named EXX). The 32 and 64 bit registers
are actually the same thing, EAX refers to the first 32 bits of RAX. These are the standard Intel x86
architecture registers.
You might also see references to registers XMM0, XMM1, etc - what are these registers? Can you find
any information saying what they are used for or how they are different to the standard ones? (Google /
ask ChatGPT!)
,