L07 - pointers, references, and functions

L07 - pointers, references, and functions - Remember from...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Remember from last week - • There are two categories of types in C++ – value types • variables of value type contain data of that type • value types are accessed directly • they normally contain primitive type data – bool , Char (or wchar_t), Byte (or char), SByte (or signed char), Int16 (or short), UInt16 (or unsigned short), Int32 (or int or long), UInt32 (or unsigned int/long), Int64 or (int64), UInt64 (or unsigned int64), Decimal, Single (or float ), Double (or double ) – reference types • contain the address of the location in memory where the data of that type is stored • usually refer to objects • examples include String ^ and Object ^
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Pointers and References (i) Pointers and references are variables that contain memory addresses as their values – a pointer normally contains the address of another variable that contains a specific value a “normal” variable directly references a value and a pointer indirectly references a value Pointers in standard C++ have to be declared just like any other variable: int *nums; double *xval, *yval; – here nums is a pointer to something that has an int value and both xval and yval are pointers to things that have double value – each pointer declared must have the * prepended: double *xval, yval declares one pointer to things of type double and one ordinary double variable
Background image of page 2
Pointers and References (ii) References give us a functionality that is similar to that of pointers, but they aren’t quite the same A reference variable is an alias for another variable All reference variables must be initialized when they are declared – from then on, any changes to the reference variable will be made to the aliased variable. The alias is just another name for the original variable. Reference variables cannot be reassigned as aliases to other variables Recap: Pointers and references – when you see *px you should read “what px points to”
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Memory and Memory Management in C++ • There are two basic “kinds” of memory that the system handles in standard C++: – the runtime stack • when a function gets called or a new block is entered (and variables are declared), the new “local environment” is pushed onto the runtime stack. When the function is exited, its “local stuff” is popped off the runtime stack – the heap • when new data is allocated dynamically under the control of the programmer (with the aid of the new construct), space is allocated for that new data on the native C++ heap • it is the programmer’s job to explicitly deallocate that space when it is no longer needed • failure to do so is the source of the memory leaks we all know and love.
Background image of page 4
C++/CLI Issues The CLR maintains its own memory that is independent of the standard native C++ heap the CLR cleans up after you – as we will see, you have to clean up your own stuff on the standard native C++ heap this is supposed to eliminate memory leaks and fragmentation that is an issue
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

L07 - pointers, references, and functions - Remember from...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online