Pointers - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Pointers

Description:

... not compile anything before the #include 'stdafx.h' in the ... This type of file can also have alternate names, including 'StdInclude.h'. Back to Pointers ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 21
Provided by: berrinya
Category:
Tags: compile | pointers

less

Transcript and Presenter's Notes

Title: Pointers


1
Pointers
  • Part I (these slides)
  • - Announcements
  • Additions to lect1 slides
  • Prelude to Pointers
  • Representation of Data in Memory
  • Memory
  • Pointers (syntax)
  • Part II (next slides)
  • Pointers and arrays
  • Dynamic memory allocation

2
Announcements
  • Recitations
  • How were they?
  • Classrooms will change (see the announcements
    before going to next recitation)
  • About hw0
  • Library making shown in recitations
  • Precompiled header files
  • See next slides for what they are
  • DLL (shared library) is more difficult than
    static ones ignore for now
  • About slides
  • From now on, I will add an advanced section on
    some topics, for those who want to read more
    others may skip these info.
  • Advanced info slides will be marked in red (slide
    header) or will be written in smaller fonts in
    regular slides (as fine-print, so to speak)

3
Header Files Reference
  • Very good detailed info about advantages and
    problems you may face when splitting your code
    into several files
  • http//www.gamedev.net/reference/programming/featu
    res/orgfiles/

4
Precompiled Header Files
  • Precompiled header file compiler reads the
    header file once and then writes some
    intermediate code to a file, the so called
    precompiled header. Most often it is just a dump
    of the parse tree the compiler has read so far.
  • The main goal of precompiled header is to reduce
    compilation time by providing often used header
    files under a form easier and faster for the
    compiler to read and use. Once your project is
    built the first time, subsequent builds will be
    faster because of the presence of the precompiled
    header files.

5
Precompiled Header Files stdafx.h
  • stdafx.h is commonly used in MS Windows projects
    as the name of a file which describes include
    files (both standard system include files and
    project specific include files that you can add)
    that are used frequently but are changed
    infrequently.
  • Stdafx.cpp Contains the preprocessor directive
    include "stdafx.h" and adds include files for
    precompiled types.
  • These files are used to build a precompiled
    header file Projname.pch

6
Precompiled Header Files advanced
  • Visual C will also not compile anything before
    the include "stdafx.h" in the source file
    (unless you uncheck the compile option
    /Yu'stdafx.h) it assumes all code in your
    source up to and including that line is already
    compiled.
  • The AFX in stdafx.h stands for Application
    Framework eXtensions. AFX was the original
    abbreviation for the Microsoft Foundation Classes
    (MFC). This type of file can also have alternate
    names, including "StdInclude.h".

7
Back to Pointers
8
Representation of Data
  • All data in a the computers memory is
    represented as a sequence of bits
  • Bit unit of storage, represents the level of an
    electrical charge. Can be either 0 or 1.
  • _ _
  • 0 1
  • Byte 8 bits make up a byte, and a character is
    one byte (smallest data type)
  • A byte can represent 256 (0255) different
    symbols
  • _ _ _ _ _ _ _ _
  • 0 0 0 0 0 0 0 0 ? 0 //binary representation
    and corresponding integer values
  • 0 0 0 0 0 0 0 1 ? 1
  • 0 0 0 0 0 0 1 0 ? 2
  • 0 0 0 0 0 0 1 1 ? 3
  • ...
  • 1 1 1 1 1 1 1 1 ? 255
  • Word typical data size used in computer
  • 32 bits on most current computers
  • 64 bits on some current computers

9
Hex Numbers
  • Hexadecimal numbers
  • - Similar to binary coding.
  • - Each digit can take values
    0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
  • corresponding to values in 0 to 15 (0-9, A
    10, B11, ... F15)
  • e.g. 0x2435af00 ?
  • _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  • 0 0 1 0 _ _ _ _ _ _ _ _ _ _ _
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  • 0 0 1 0 0 1 0 0 _ _ _ _ _ _ _
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  • ...

10
Hex Numbers ctd.
  • Representation and arithmetic is then similar to
    the binary or digit representation, with this
    range
  • 0x2435AF00 1
  • 0x2435AF01
  • 0x2435AEFF 1
  • 0x2435AF00
  • ...

11
Memory
  • What happens when we define variables
  • char c A 
  • int n 5   
  •     
  • 0x2435af00
  • 0x2435af01

. . .
Symbol table
. . .
Memory
12
Pointers
  • A pointer is a variable to store an address in
    computer memory
  • Henceence, it points to a variable
  • Purpose
  • Basis for implementing linked data structures
    linked lists, trees, graphs...
  • Provide dynamic memory allocation variable size
    arrays
  • Sharing objects among instances of a class (we
    will see how later, now to the basics...)

13
Pointers definition
  • A pointer is defined as lttypegt
    ptr_variable
  • - ptr_variable does not store a lttypegt value,
    but

  • stores the address of a memory location that
    stores a lttypegt value
  • char c A 
  • char p    

An unititialized pointer is said to point to
Junk/Nowhere/Null. Null is defined in standard
header files to mean nowhere/nothing.
We dont just say pointer p We will see why.
14
Pointers , or the address of operator
  • You can store the address of another variable
    (c), in a pointer variable (p)
  • char c A 
  • char p
  • p c    
  • This is not the main use of pointers (assigning
    an existing variables address), but is useful in
    explaining the concept of address storage.
    Pointers can be used in this way for accessing
    static array locations (instead of the slower
    array addressing). We will see these later.

15
Pointers dereferencing
  • ptr derefence (or indirection) operator gives
    the content of memory location pointed by the
    pointer variable (ptr)
  • char c A 
  • char p
  • p c
  • cout ltlt p
  • //prints the value of c, that is A
  • //just like cout ltlt c prints the value of
    the char variable
  • //note that p and c can be used
    equivalently anywhere in the program
  •     

16
Pointer assignment
  • A pointer can be assigned to another pointer of
    the same type.
  • Assume we have done as before
  • double n
  • double p
  • p n
  • p 17.5 // memory location pointed by p
    contains 17.5
  • Now you can assign p to another pointer variable
  • double q
  • q p // q and p points to the same location
    in memory

n
p
q
17.5
n
p
17
Pointers definition(what happens behind the
scenes)
  • char c A    
  • char p    
  • 0x2435af00
  • 0x2435af01

. . .
A null
c p
Symbol table
. . .
//a pointer uses 4 bytes of memory //hence it
takes more space then 1 byte
Memory
18
Pointer address of a variable (what happens
behind the scenes)
  • char c A    
  • char p    
  • p c // p now points to c.  

. . .
A 0x2435af00
Symbol table
0x2435af00 0x2435af01
. . .
Alternative and more meaningful view
c A p
Memory
19
Pointer dereferencing (what happens behind the
scenes)
  • char c A    
  • char p     //p points to null
  • p c // p now points to c.  
  • p B

//unchanged
. . .
B 0x2435af00
0x2435af00 0x2435af01
. . .
Symbol table
Memory
20
Misc.
  • What happens if you try to assign a
    string/int/double expression to a pointer
    variable?
  • e.g. double q
  • q 123.45
  • syntax error
  • What happens if you try to access a memory
    location pointed by a pointer that is not
    initialized?
  • e.g. double q
  • cout ltlt q ltlt endl
  • a run-time (application) error occurs
  • What happens if you display the value of a
    pointer?
  • e.g. cout ltlt q ltlt endl
  • nothing, but it displays the value of q, which
    is the address where it points to.
Write a Comment
User Comments (0)
About PowerShow.com