topic02_MemoryAndPointers

topic02_MemoryAndPointers - Basic Memory Management and...

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

View Full Document Right Arrow Icon
Basic Memory Management and Pointers in C++ CS 1037a – Topic 2
Background image of page 1

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

View Full DocumentRight Arrow Icon
Overview: part I • Pointers - a basic variable type • values of pointer variables (or pointers ) • computer memory as a huge 1D array • basic operations with pointer variables • Why pointers? • pointers to (static) objects/variables • pointers as function arguments • pointers and arrays • pointers to dynamic objects/variables 2-2
Background image of page 2
Overview: part II • Dynamic Memory Operations • dynamic vs. static objects • Why dynamic objects are needed? • operators new and delete • dynamic vs. static arrays • operators new[ ] and delete[ ] “stack” and “ heap” memory 2-3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Overview: part III • Examples of Dynamic Arrays • dynamic array of objects versus array of pointers to dynamic objects • dynamic arrays of pointers • double pointers • dynamic 2D arrays • objects containing dynamic arrays 2-4
Background image of page 4
Related materials • Sec. 4.1: pointers and dynamic memory • Sec. 4.2: pointers and arrays as parameters from Main and Savitch “Data Structures & other objects using C++” from CS1037 labs • Lab 2, lab 3, and Lab 4 2-5 from ES1036 lecture notes • Topic 6 and Topic 9 (parts 1&2)
Background image of page 5

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

View Full DocumentRight Arrow Icon
POINTERS Part I 2-6
Background image of page 6
Background info • all data (variables/objects/arrays) processed by computer program sit in computer memory • computer memory can be seen as a HUGE one-dimensional array 2-7 For example, can assume that each cell stores 1 byte of data • each cell in this memory array has a unique “index”, normally referred to as address 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
Background image of page 7

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

View Full DocumentRight Arrow Icon
Background info • When a function (e.g. main ) starts to run, operating system (OS) allocates/assigns memory space to all declared variables, objects or arrays declared in this function 2-8 char a; int j; Point p; char arr[6]; Assume, for simplicity, that each cell stores 1 byte (8 bits) of data 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
Background image of page 8
Background info • Values of variables/objects in the memory can be accessed (for example) using indexes ( addresses ) of the corresponding cells in the memory, if known 2-9 char a; int j; Point p; char arr[6]; Assume, for simplicity, that each cell stores 1 byte (8 bits) of data byte #12 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 bytes #20-23 bytes #30-35 bytes #37-44
Background image of page 9

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

View Full DocumentRight Arrow Icon
Background info • Since variables/objects/arrays occupy consecutive cells in the memory, it is enough to know address of the first cell as long as we know the size of object/array 2-10 char a; int j; Point p; char arr[6]; Assume, for simplicity, that each cell stores 1 byte (8 bits) of data 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 byte #12 4 bytes after #20 6 bytes after #30 8 bytes after #37
Background image of page 10
Address operator „&‟ • Operator “&” returns address (of the first cell) where variable/object is stored 2-11 char a; int j; Point p; char arr[6]; 0 1 2 3 4 5 6 7 8
Background image of page 11

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

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

This note was uploaded on 10/26/2009 for the course CS cs1037 taught by Professor Yuri during the Fall '07 term at UWO.

Page1 / 136

topic02_MemoryAndPointers - Basic Memory Management and...

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

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