2013 10 automatic static and dynamic data in java

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: ic data in Java •  A running program typically has 3 kinds of data in it: •  Automatic (stack) •  Dynamic (Heap) •  Static (data) •  These kinds of data differ in: •  •  •  what region of memory the data is stored in when and how memory for the data is allocated (“created”) when and how memory is deallocated (“destroyed”) •  Both Java and C++ have basically these same 3 kinds of data, with some differences Silberschatz, fig 3.1 Scott B. Baden / CSE 100, Lec 2 / Spring 2013 11 Automatic data •  Automatic data is called “automatic” because it is automatically created and destroyed when your program runs •  Examples of automatic data in both Java and C++ •  •  formal parameters to functions local variables in functions (if not declared static) •  Memory for automatic data is allocated on the runtime stack •  Memory for automatic data is allocated when execution reaches the scope of the variable’s declaration •  Memory for automatic data is deallocated when execution •  Deallocation is always from the end of the stack •  The variables (i, j, x) are automatic int foo(int i){
 int j = i + 1;
 C* x; x = new C(); x->setA(j); j = x->getA(); delete x; return j; } Scott B. Baden / CSE 100, Lec 2 / Spring 2013 12 Static data •  Static data is called static because it essentially exists during the entire execution of your program •  Examples of static data in Java and C++ •  Variables declared static •  C++ variables declared outside any function and class body •  Memory for static data is allocated in the static data segment •  Memory for static data is allocated when your program starts, or when execution reaches the static data declaration for the first time •  Memory for static data is deallocated when your program exits Scott B. Baden / CSE 100, Lec 2 / Spring 2013 13 Dynamic data •  Dynamic data is called “dynamic” because it is allocated (in C/C++, destroyed) under explicit programmer control •  •  Example of dynamic data in Java: all objects (instances of a class), including arrays Dynamic data is created using the new operator Example of dynamic data in C++: any variables created using the new operator. In C++, any type variable can be created using new •  Memory for dynamic data is allocated from the “heap” •  In Java, dynamic data exists from the time object is reclaimed by the garbage collector new is called until the •  In C++, dynamic data exists from the time new is called until it is deallocated with the delete operator •  Managing heap memory requires programmer attention in C++, in Java it’s automatic •  The object created with new must be deleted, but note that the container C is automatic! int foo(int i) {
 int j = i + 1;
 C* x; x = new C( ); x->setA(j); j = x->getA( ); delete x; return j; } Scott B. Baden / CSE 100, Lec 2 / Spring 2013 14 Example of an automatic object...
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