Lecture 14 Pointers and Dynamic Memory Allocation

Document Sample
Lecture 14 Pointers and Dynamic Memory Allocation Powered By Docstoc
					                    G6DICP                                                     Memory
                                                       ! Physical    memory (RAM) is arranged in bytes.
                Lecture 14
                                                       ! Each    byte has a physical address (number).

                                                       ! Data is stored in a physical byte (or series of
      Pointers and                                       bytes).
Dynamic Memory Allocation
                                                       ! Data can be accessed by using its physical
                                                         address.




                    Pointers                                               Pointer Syntax
! Pointersare a data type that contain memory          ! The    ^ character
  addresses.                                               ^byte - a pointer that is pointing to a byte
                                                                eg   var pt1 : ^byte;

!A   pointer can point to a variable of any type (eg       pt1^ - the data pointed to by pt1
                                                                eg   writeln(pt1^);
  a single byte, a string or an entire record).
                                                       ! The    @ character
! Because   a pointer contains the address of the          @myVar - the address of myVar
                                                                eg   var
  start of a data construct - pointers are the same                        myVar : byte;
  size regardless of what they are pointing to.                            pt1 : ^byte;
                                                                     ...
! Pointers can be untyped or typed (ie what data                     pt1:=@myVar;
  type they are pointing to may be specified).                       writeln(pt1^);




             Pointer Behaviour                             Dynamic Memory Allocation
! Remember!                                            ! Allocation    of memory is usually automatic.

! Pointers do not contain data - they contain a        ! The  Heap is an area of memory that can be
  reference to data.                                     allocated (and deallocated) while a program is
                                                         running (ie dynamically).
! Several pointers can point at one data object -
  thus changing the content of one will seemingly      ! This allows programs to re-use memory (eg for
  change the content of them all. Really only one        arrays of indeterminate size).
  copy of the data exists.




                                                                                                           1
                new and dispose                                     Advantages of Pointers
! new  allocates memory on the heap                        ! Data  can be accessed without physically moving
! dispose  deallocates memory                                it in memory.
! For example:                                             ! Dynamic allocation makes much more efficient
  var                                                        use of memory - allowing very large data
         myString : ^string;                                 constructs.
  begin                                                    ! Because pointers point to physical memory they
     new(myString);                                          can allow access to data anywhere.
     myString^:=’This is a String';
     writeln(name^);                                       ! Some languages make much more extensive use
     dispose(myString);                                      of pointers than does Pascal - eg C and C++.
  end.




           Problems with pointers
! Be    careful - bug prone.
     is easy to accidentally modify data - especially if
  ! It
    more than one pointer is pointing at the same data.

! Memory     leaks
     memory on the heap is not deallocated correctly, it
  ! If
    may retain data even when the program terminates -
    making this memory unavailable to other programs.




                                                                                                               2

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:6
posted:3/16/2010
language:
pages:2
Description: Lecture 14 Pointers and Dynamic Memory Allocation