; Lecture 14 Pointers and Dynamic Memory Allocation
Learning Center
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

Lecture 14 Pointers and Dynamic Memory Allocation


Lecture 14 Pointers and Dynamic Memory Allocation

More Info
  • pg 1
									                    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

                    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.

                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++.

           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.


To top