x geta stdendl scott b baden cse 100 lec

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: lt; std::endl; } Scott B. Baden / CSE 100, Lec 2 / Spring 2013 7 Pictures of memory •  It is useful to abstract the essentials about the contents of memory by drawing simple pictures... •  Here is a picture of the contents of memory after those statements (either the Java or the C ++ version) execute: •  The pointer variable x points to the object, an instance of the class C, which contains an instance variable named a with value 5: x: a: •  5 Always be aware of the difference between a pointer and what it points to! Scott B. Baden / CSE 100, Lec 2 / Spring 2013 8 Differences between Java and C++ •  Class definitions In a Java, a visibility specifier (public, protected, or private) is attached to each member declaration; if missing, the member has “package” visibility In C++, a visibility specifier (public:, protected:, or private:) sets off a section of member declarations; if missing, the members have private visibility In C++, but not Java, the body of the class definition should end with a semicolon ; after the closing brace } Both C++ and Java will provide a public default constructor if you do not define any, but in C++ it is not guaranteed to initialize primitive member variables for you •  Pointers ▶  ▶  ▶  ▶  ▶  ▶  In Java, declaring a variable of class type creates a pointer that can point to an instance of that type (an object); in C++, you have to explicitly specify that the variable is a pointer by using * after the typename In Java, the dot operator . dereferences a pointer and accesses a member of the object pointed to; in C++, the -> operator does that; or, you can use a combination of the dereference operator * and the member access operator . That is, these are equivalent in C++: x->getA() and (*x).getA() Scott B. Baden / CSE 100, Lec 2 / Spring 2013 9 Memory management in Java and C++ •  In Java, all objects are created using new, and memory an object takes is automatically reclaimed by the garbage collector when it can no longer be accessed in your program C x = new C(); // create an object x = null; // object is inaccessible; no problem, gc will free •  In C++, objects can be created using new, but memory for such objects is not automatically reclaimed C* x = new C(); // create an object using new x = nullptr; // object is inaccessible; // problem!! it will not be freed •  That is a memory leak! In C++, an object created using new must be explicitly freed using delete for its memory to be reclaimed for later use in your program C* x = new C(); ... delete x; x = nullptr; // // // // create an object using new do things with the object free the object using delete make pointer null to avoid dangling pointer •  Note: some objects created in other ways in a C++ program are reclaimed automatically. Understanding the details of memory management is an important part of C++ programming Scott B. Baden / CSE 100, Lec 2 / Spring 2013 10 Automatic, static, and dynam...
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