Lec02

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: rogrammer, you should know how and when this happens, but you do not need to take steps to make it happen •  Because the standard C++ runtime environment does not have a garbage collector, both allocation and deallocation of dynamic data must be done under programmer control •  This leads to common bugs that can be very hard to track down. •  We’ll look at these types of bugs: •  •  •  Inaccessible objects Memory leaks Dangling pointers Scott B. Baden / CSE 100, Lec 2 / Spring 2013 30 Inaccessible objects •  The inaccessible object bug: changing the value of the only pointer to an object, so you can’t access the object anymore thing* p = new thing(); thing* q = new thing(); p = q; // the first thing is now inaccessible Scott B. Baden / CSE 100, Lec 2 / Spring 2013 31 Memory leaks •  Memory leaks: forgetting to delete dynamic data (often related to inacessible objects) •  (these are hard to get in Java, because of its garbage collector) void foo() { double* q = new double(3.0); ... /* stuff, but no delete q */ } ... for(i=0;i<1000000;i++) foo(); // massive memory leak! Scott B. Baden / CSE 100, Lec 2 / Spring 2013 32 Dangling pointers •  A "dangling pointer" is a pointer variable that contains a non-null address that is no longer valid... the pointer isn’t null, but it isn’t pointing to a valid object either •  Hard to get in Java, with no pointers to automatic variables, and good garbage collection of dynamic variables int* bar() { int i; return &i; // automatic variable, allocated on stack // return pointer to automatic variable... // bad news! Stack frame is popped upon return } ... int* p; int* q; // q and p are now dangling pointers p = new int(99); q = p; delete p; p = nullptr; // q is still dangling! Scott B. Baden / CSE 100, Lec 2 / Spring 2013 33 References in C++ •  Besides having pointers, C++ also permits creating references to existing variables •  References are especially useful for function parameters: they allow passing arguments to functions without copying the arguments •  (The same effect can be had with pointers, but the syntax of references is simpler) •  It is important to understand the difference between pointers and references... •  To declare and create and initialize a reference variable in C++, use a declaration statement of the form <typename> & <identifier> = <existing_variable> •  This creates a reference to <existing_variable>, which must be of type <typename>, and makes <identifier> the name of this reference •  A reference is an alias: it provides another name for an existing variable int a = 5; int &b = a; // b and a now both refer to the same variable b= 6; std::cout << a << std::endl; // prints 6 Scott B. Baden / CSE 100, Lec 2 / Spring 2013 34 An abstract memory picture of references •  A reference is an alias: it sets up another name for an existing vari...
View Full Document

This note was uploaded on 09/11/2013 for the course CSE 100 taught by Professor Staff during the Spring '08 term at UCSD.

Ask a homework question - tutors are online