3_pointers - Memory and C+ Pointers ! Java ! Java objects...

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

View Full Document Right Arrow Icon
Memory and C++ Pointers
Background image of page 1

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

View Full DocumentRight Arrow Icon
! Java objects and memory ! Java primitive types and memory ! C++ objects and memory ! C++ primitive types and memory ! Note: “primitive types” = int, long, float, double, char, … January 2010 Greg Mori 2
Background image of page 2
// in function, f … int arr[]; arr = getOrdArray(5); // … arr f n getOrdArray 5 stack (static) public int [] getOrdArray( int n){ int arr[] = new int [n]; for ( int i = 0; i < arr.length; ++i){ arr[i] = i * 2 + 1; } return arr; } heap (dynamic) 1 3 5 7 9 January 2010 3 Greg Mori
Background image of page 3

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

View Full DocumentRight Arrow Icon
January 2010 Greg Mori 4
Background image of page 4
! Java manages memory differently for primitives and objects: ! Objects go on the heap ! Primitive types go on the stack ! Primitive type variables are always values ! Object variables are always references January 2010 Greg Mori 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
// in function, f … int n = 5; Integer nObj = new Integer(5); // … n nObj f 5 stack heap 5 January 2010 6 Greg Mori
Background image of page 6
// in function, f … int n = 5; Integer nObj = new Integer(5); // … int m = n; Integer mObj = nObj; n nObj f 5 stack heap 5 mObj m January 2010 7 Greg Mori
Background image of page 7

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

View Full DocumentRight Arrow Icon
// in function, f … int n = 5; Integer nObj = new Integer(5); // … int m = n; Integer mObj = nObj; n = 6 n nObj f stack heap 5 mObj m 5 6 January 2010 8 Greg Mori
Background image of page 8
January 2010 Greg Mori 9
Background image of page 9

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

View Full DocumentRight Arrow Icon
! In C++: ! Both primitive types and objects can be allocated either on the stack or on the heap ! Both primitive type and object value and reference variables are allowed Hence, there needs to be C++ notation to distinguish between the two January 2010 Greg Mori 10
Background image of page 10
! There are two ways to refer to things in C++ ! The first is pointers ! The * character is used to denote a pointer January 2010 Greg Mori 11 // n is a Node object Node n; // n is a pointer to a Node object Node *n;
Background image of page 11

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

View Full DocumentRight Arrow Icon
! Variables in methods are allocated in stack memory ! C++ uses the keyword new to allocate space in the heap Greg Mori 12 // n is a Node object, in stack Node n; // np is a pointer to a Node variable, np is in stack Node *np; // new creates a Node object, in heap // np points to this object np = new Node(); January 2010
Background image of page 12
n np f stack heap Node object // n is a Node object, in stack Node n; // np is a pointer to a Node variable, np is in stack Node *np; // new creates a Node object, in heap // np points to this object np = new Node(); January 2010 13 Greg Mori
Background image of page 13

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

View Full DocumentRight Arrow Icon
!
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/17/2010 for the course CMPT 11151 taught by Professor Gregorymori during the Spring '10 term at Simon Fraser.

Page1 / 43

3_pointers - Memory and C+ Pointers ! Java ! Java objects...

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

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