C++I n s t i t u t e o f A s t r o n o m yU n i v e r s i t y o f C a m b r i d g e 2 0 1 5c h e d g e s @ a s t . c a m . a c . u k
WorkshopThree
What's on the agenda?
Last WeekFlow control and looping andsome debugging.
This WeekArrays, dynamic memory andthe vector library.
What is an array?Arrays are ways of storing a set of variables at the same time when they are of the same type.They are particularly useful for big datasets such as tables where a column would be stored in anarray.
What should we do with one?Arrays are just like other data types and just like a regular variable they need to be declared andusually initialised. This is more than just declaring the type to the compiler but now also declareshow much memory it will need to set aside for the variable.
What should we do with one?Arrays are just like other data types and just like a regular variable they need to be declared andusually initialised. This is more than just declaring the type to the compiler but now also declareshow much memory it will need to set aside for the variable.
Gotchas:Use [Constant Int]
Use the right numberof elements
You /can/ go over theedge of the array.
...and then get looping.Whether it's filling your array or doing operations on it you are going to want to loop over your arrayvariables most likely. C++ is not like python, you will not be able to perform operations on arrayseasily without making neat functions for them.
More DimensionsBut arrays don't stop there! You can have as many dimensions as you fancy, but remember thatthey will drain your memory quickly. A 50 element array is quite small but a 50x50x50x50 elementarray quickly becomes a drain on your memory.
Arrays with Unknown LengthSometimes you will want to create a variable, usually to store something, that you the developerwill not know the size of. It may be that you want the code to determine the size itself. This meansyou don't know how to initialise it. You could just make an extra large array but often this iswasteful. Instead we use dynamic memory!
I am Dynamic
Arrays with Unknown LengthSometimes you will want to create a variable, usually to store something, that you the developerwill not know the size of. It may be that you want the code to determine the size itself. This meansyou don't know how to initialise it. You could just make an extra large array but often this iswasteful. Instead we use dynamic memory!
Array
Dynamically allocatedArray
The dynamic allocation is finding ablock of memory large enough to
store "x" integers.
PointersPointers are important and something I've always found tricky. They are very useful and are prettynecessary for dealing with large arrays.
Array
Dynamically allocatedArray
A wild pointer appeared!
Pointers When we make a variable it gets stored in the memory.
This has an addresswithin the memory
system, but we rarelycare about it. When we
call the variable thecompiler/OS knowwhere to find the
memory block it stored itin.
Pointers If we want to find the address of a variable we use &In this case &name is a pointer to the memory address of our
variable name.
Pointers But more usefully we can then use this pointer to get back to theoriginal value that we assigned the variable that is stored at that
address This is done with the dereferencing symbol *
Pointing + Array = Dynamic MemoryTo sum up, dynamic memory works by first establishing a pointer which will take us to a newmemory address and using the new command to allot a block of memory the size we need for ourarray type and array size. This size now no longer needs to be constant and set by the developerbut can be a variable from within the code.
Handling MemoryMemory is not an infinite commodity. You should be careful how you use it and when you'refinished with it you should clean up after yourself. The pair to the new command is the deletecommand.
New
Reserve me a block
Delete
Free up that block again, Idon't need it.
Handling MemoryMemory is not an infinite commodity. You should be careful how you use it and when you'refinished with it you should clean up after yourself. The pair to the new command is the deletecommand.
Don't get this confused with the method from C which is to use where we use malloc and free asour paired commands to perform the same trick. Don't worry, you can still use them in C++ if youhave reason to by including the c standard library but you should stick to the C++ versions of newand delete.
Todays Tip: ValgrindHaving memory problems? Memory leaking? Don't know where? Try out valgrind which should beinstalled on the IoA machines. Be warned that this is another debugging interface with a lot ofscope to help you but can be a bit of a learning curve. If you do have memory problems andsegfaults that gdb isn't getting rid of for you, give it a go!
The Heap and the StackThis is really just incase you are interested or need to get deep into dynamic memory allocationand get a bit lost in the jargon. When we store information when coding there are two (main)places where the information is stored.
Heap
Stack
Big, slow to write,keeps allocation
Small, fast and willdeallocate when
"popped"
Are arrays frustrating you? Dynamic memory allocation a pain? Then look no further than the...
VECTOR LIBRARYsupply co.
T R D M R K
Class Example
For a few minutes I'llwork through a shortcode using the topics
we've introduced.Feel free to write
along with me if youwould like.
Example Class
Please see the link for theexamples I've put
together. Staying isoptional but I'll be here
for the rest of the class ifyou have any questions
about the materialcovered or have any
problems writing andcompiling the examples
just let me know.