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

C++ 050 Memory Static Params

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

C++ 050 Memory Static Params

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Advanced Programming Techniques

Dynamic Memory Management


Class Attributs and Methods
State_C ontrol
Signal_A dapter C
State_C ontrol_A UDI o
on_ select_prev () n
init() on_select_next() t
raise_tick() on_tick() on_select_next() r
raise_select_prev () on_triptime_alarm() on_tick() o
raise_select_next() 1 on_mem_lev el_dow n() on_triptime_alarm() l
raise_triptime_alarm() on_mem_lev el_up() on_ select_prev ()
raise_ignition_on() on_mem_lev el_dow n() S
on_ignition_on() e
raise_ignition_off() on_ignition_off() on_mem_lev el_up()

Parameters
c
raise_reset_program() on_reset_program() on_ignition_off() t
raise_reset_all_programs() on_reset_all_programs() on_ignition_on() i
raise_eeprom_changed() on_eeprom_changed() reset_program() o
raise_memlev el_down() reset_all_programs() n
<<ctor>> init()
raise_memlev el_up() Create()

Impl_Tripdistance_A UDI C
Impl_Tripdistance
Program a
Data_Adapter l
c
calc_v alue() : v oid u
display () : v oid Imp_Triptime_Alarm l
ignition_on() : v oid Impl_Triptime_A larm_A UDI a
ignition_off() : void t
i
reset() : v oid Impl_Triptime o
<<?>> eeprom_changed() : ... n
need_recalc() : bool
Impl_Mean_C onsumption_A U DI S
Impl_Mean_C onsumption e

Prof. Dr.-Ing. Peter Fromm


c
t
i
Impl_C urrent_C onsumption Impl_C urrent_C onsumption_AUDIo
n

Impl_Average_Speed

Prof. Dr.-Ing. Michael Lipp Display _A dapter

clear()
Impl_Range

write_str()
<<draft>> w rite_sy mbol()
<<ctor>> init()
D
i
s
Display _Triptime_A larm_A U DI p
Display_Triptime_A larm l
Display_V alue a
O SEK-VGC y
(from SYS) show(v alue : dword) : v oid Display _Mean_C onsumtion_A U DI
Display _Mean_C onsumption S
e
c
t
i
O SEK-win32 Display_C urrent_C onsumption_A U DIo
Display _C urrent_C onsumption
(from SYS) n

Display_Tripdistance Display_Tripdistance_A UDI


console-win32
(from SYS)

"classes " of lefthand coloumns in the middel build up the trip computer core. The classes on the right side all
coloumn act as inerface to A ll code has to be independent of manufacturer. belong to the A UDI package
hareware and O S. They and are not part of the main class
are declared abstract in the The Impl_XXX and Display_XXX classes describe what flav ours of program hierarchy.
main class hierarchy and functionality are expected
implemented in several They are included to show where
SYS packages. the main class hierarchy has to be
expanded for manufactorer's
requirements
Wrap-Up

 Allocation of memory for objects


 This Pointer

 Wrong initialization of data is a common cause of programming errors


 C++ provides the following operations for initialization of data
 Constructor
 Destructor
 Copy Constructor
 Dynamic memory

Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 2
Part One

More about dynamic memory

Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 3
To be done: Copy constructor for matrix

 Matrix objects reference dynamically allocated data


 Impact on copy constructor?

Life demo
Naive copy constructor

Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 4
Shallow and deep copy

 Shallow copy
Original  Changes of the dynamic data
Pointer attribute
made by either the original or
copy Dyn. Data the copied object are visible
to both
Copy
Pointer attribute

 Deep copy
 Changes of the dynamic data
Original made by either the original or
Dyn. Data
Pointer attribute the copied object are effect
copy only the object that made the
change
Copy Dyn. Data
Pointer attribute

Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 5
General procedure

Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 6
Copy constructor: Rules of thumb

 Default copy constructor generated by compiler does bitwise copy


 When an object references dynamic data (in general: “a resource”) a
copy constructor must be provided
 The copy constructor must duplicate the dynamic data (provide a new
resource handle)
 Exception: shared resource (e. g. read-only lookup-table in dynamic
memory)
 Caveat: Destructor may only release resource when last object is
destroyed

 Still to be done: Assignment (see upcoming lecture on operator


overloading)

Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 7
Part Two

Static member variables and methods

Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 8
Member versus static variables

 So far, all attributes have been object scoped member variables


 A member variable has an own instance for every object.
 A static variable is a variable which is common for all objects of a
class.

m1.
object m1 memberVariable

class::
staticVariable

m2.
object m2 memberVariable

Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 9
Keyword static

#ifndef MEMBER_H
#define MEMBER_H

class member
{
public:
int memberVariable;
static int staticVariable;

};

#endif

Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 10
Declaration

A member variable... A static variable...

 is declared in the class header  is declared in the class header


file file
 is instantiated when the object is  is with the class, i.e. in the cpp
generated file
 has a different address for every  has a common address for
object every object
 is accessed  is accessed
 object.variable  object.variable
 object->variable (pointer)  object->variable (pointer)
 class::variable

Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 11
Usage

 Member variables should be used when every objects needs an own


copy of data
 Static variables can be used when objects want to share data or if
only singleton objects will be generated.
 An attribute that holds information about the class, e. g. a string
representation
 An attribute that holds summary information about the instances,
e. g. the number of objects created
 An attribute that holds a resource shared by all instances, e. g. a
network connection that all objects can write data to

Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 12
Don't mix up: Static Variables in Functions

When using the static members in functions, the variables gets a fixed
address on the heap instead on the stack. Consequences:

 The function has a memory. When entering the function again, it


remembers the value from the last call (upon leaving)
 The function is not reentrant. It can only be called once. Critical in a
multitasking environment!

Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 13
Static Operations

Operations may be declared static. This suppresses the this pointer to


be passed to the function. Consequences:

 The operation can only access static variables of the class


 It can not access any member variable
 As the this pointer is not passed, better optimized code can be
generated by the compiler (relevant for embedded systems)

Life Demo:
Counting instances

Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 14
Part Three

C++ extensions to parameters

Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 15
Passing Parameters

There are several ways to pass data to and from functions and
operations:
 Call by value  function(int parameter)
 Pointer  function(int* parameter)
 Call by reference  function(int& parameter)
 Return value  result = function()
C++ language
extension

Which one is the best? Design decision!

Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 16
Some pro’s and con’s

 Call by value generates an own copy of the data, the function


becomes reentrant (+)
 Call by value moves the data to the stack. Time consuming for large
data structures (-)
 Call by pointer and call by reference allow to return data via the
parameters (+)
 Call by pointer and call by reference work on the data of the caller,
the function is not reentrant, in a multitasking environment the data
may get corrupted (-)
 Call by pointer and call by reference only pass a pointer to the
function, very efficient (+)
 Pointer supports pointer arithmetic's, call by reference not
 Call by reference is easier to handle compared to pointer
 …

Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 17
Using const

Compromise:

int functionCallByReferenceConst(const int &para1)


{

int a;
a=2*para1;
//para1 = a; //Will be rejected by compiler
return a;
}

Efficient and protected!

(Note that constness can be cast off again...)

Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 18
Arrays

Arrays contain a certain number of identical objects.

Example: Array containing 6 integers

Conventions:
 Declaration: int a[6];
 First element a[0]
 Last element a[n-1]
 Pointer arithmetrics: &a[1] = &a[0] + 1 ( == sizeof(int))

a[0] a[1] a[2] a[3] a[4] a[5]

a  actually the array variable is a pointer

Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 19
Arrays - Access

Content
 a[position] //Index
 *(a+position) //Pointer

Address
 a //Address of first Element
 &a[0] //Address of first Element
 &a[1] //Address of second element
 a+1 //Address of second element

Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 20
Passing arrays as parameters

Arrays can only be passed by reference, i.e. as pointer

 func(int* array)
 func(int array[])
 func(int array[6])

 Note the difference: &array now points to the pointer located on the
stack

Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 21
Arrays of more dimensions

More dimensional arrays are possible in C++ but must be handled with
care

Conventions
 Declaration: int a[3][2]
 First element: a[0][0]
 Last element: a[2][1]

Caution: int a2d[3,2] compiles on some compilers, but is a normal one


dimensional array (comma operator)

a[0][0] a[1][0] a[2][0]


double dereferencing
a[0][1] a[1][1] a[2][1]

Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 22
Design recommendations

 Call by value for small data types


 Check the compiler conventions for register and stack usage

 Call by reference for larger structures and objects


 Use const for read-only access inside method

 Pointer for data structures which require pointer arithmetic's e.g. for
iterators. Example arrays.

 Return values for data which is directly used by another function.


 printf(sin(a))

Advanced Programming Techniques Prof. Dr.-Ing. P. Fromm, adapted by Prof. Dr.-Ing. M. Lipp 27/11/14 23
Wrap-Up

 Copy constructor and referencing resources

 Difference between static and member variables

 Call by reference versus call by value

Advanced Programming Techniques, Prof. Dr.-Ing. P. Fromm, Prof. Dr.-Ing. M. Lipp 27/11/14 24

You might also like