{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

5_ada_acc_tyoes

5_ada_acc_tyoes - Today Access Types Introduction to...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computers and Programming Prof. I. K. Lundqvist Lecture 5 Mar 29 2004 Today: Access Types • Stack vs. Heap • Allocating memory • Deallocating memory • Difference from C model • Linked lists Dynamic Data Structures • Arrays have disadvantages: – size must be known at compile time – space wasted or insufficient (overflow) – inserting to/removing from middle requires shifting the elements Idea : Use dynamic, variable sized memory ( linked lists ) – size starts at zero, changes as necessary – space (de)allocated by programmer access variables needed Programs and Memory Stack : stores variables that are local to functions – All static memory is allocated from the stack – when a functions is called, its automatic variables are allocated on the top of the stack – when it ends its variables are de-allocated Code Data Heap Stack
Image of page 1

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

View Full Document Right Arrow Icon
Programs and Memory Heap : place for variables that are created with ‘ new and disposed by unchecked _ deallocation Dynamic memory is allocated from the heap Data : initalized variables including global and static variables Code (text): program instructions to be executed Code Data Heap Stack Programs and Memory Heap : place for variables that are created with ‘ new and disposed by unchecked _ deallocation Dynamic memory is allocated from the heap Data : initalized variables including global and static variables Code (text): program instructions to be executed Code Data Heap Stack Stack vs. Heap • Stack – Grows “down” – Operations always take place at the top • Push and pop are well organized – Support for nested functions and recursion • Heap – Grows “up” – The order in which objects are created or destroyed is completely under the control of the programmer – You can have ‘holes’ – Dynamic memory management – Memory fragmentation - memory fragments into small blocks over lifetime of program Node consists of item and pointer to next node Linked Lists Linked list : a list of nodes (records), each pointing to the next node • List data type: – insert/delete anywhere – sequence of nodes – implement using linked memory • Singly linked list: head NULL signals end of list 42 98 12 6
Image of page 2
List Operations Go to a position in the list. Insert : an item at a position in the list. Delete : an item from a position in the list. Retrieve : an item from a position. Replace : an item at a position. Traverse : a list. Search : for an item in the list. Storage allocation • Allocator new T • T is arbitrary data type • memory is allocated for an object of type T • pointer to that memory is returned new T'(value) • memory allocated as above • initial value stored in that memory • pointer to the memory is returned – examples: ?
Image of page 3

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

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

{[ snackBarMessage ]}