Code Examples
  Home arrow Code Examples arrow Page 4 - SatView: Pointer Perfect, Part 1
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

SatView: Pointer Perfect, Part 1
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2004-11-29

    Table of Contents:
  • SatView: Pointer Perfect, Part 1
  • Operations on pointers
  • The pointer-to-member operator
  • Pointer casting
  • Dynamic_cast

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    SatView: Pointer Perfect, Part 1 - Pointer casting


    (Page 4 of 5 )

    Pointers can take on different identities, depending on how you wish to use them. You might find older C APIs using void* as function parameters to allow you to implement your own types. An API could, for example, allow you to provide function pointers that construct, destruct, read and print strings. Since the API is only concerned with void* it won’t care whether you are using char, wchar_t, std::string, BSTR or whatever. This way the integration of string into your code base remains up to you, although lack of types makes it unsafe. This is an interesting point we are definitely going to explore later on.

    In C++, the API could use pointers (preferably abstract) to base classes instead of void*. It does all its operations on the base class and leaves the implementation up to you in a derived class. Through virtual functions, the compiler will know which functions have to be called at runtime (the world of polymorphic classes). This way you can integrate your own code in a type safe way.

    In order to use the different forms of a pointer, you sometimes need to cast them into their right shape. A nice rule to remember is that a cast doesn’t show respect for identity; so always verify how much respect your code is showing. Don’t be surprised to find bug-riddled code to be showing no respect at all.

    C-style cast

    The standard C-style cast is one that simply forces one type onto another.

    T t = (T)expression;

    e.g. float *pPtr2 = (float*)pMyVar;

    C-style casts are totally unsafe (meaning they don’t perform neither compile time nor run time checks), so C++ provides operators that enhance these casts. Although the C++ cast operators require more work as far as typing is concerned, they will help you catch bugs early in the development process.

    Static_cast

    The static_cast works like the C-style cast and relies only on compile time information and performs no run time checks.

    T t = static_cast<T>(expression);

    e.g. float *pPtr2 = static_cast<float*>(myVar);

    When you are dealing with a chunk of memory pointed at by char* (sizeof(char)==1... thus the pointer allows you to iterate byte for byte through the memory), there is an easy way to extract data types from this chunk. All you need is the reference operator and the static_cast:

     int framerate = *(static_cast<int*>(&pChunk[n]));

    What happens is that the address of the nth byte in pChunk is retrieved and casted from a char* to an int*, on which we use the reference operator. This forces the retrieval of the next 4 bytes (sizeof(int)==4), the contents of which are copied as an int into framerate. Always make sure that the memory you are reading really is available and that you are aware of any possible endianity issues. Endianity is something you deal with when you are writing cross-platform code and will be explained it in another article.

    More Code Examples Articles
    More By J. Nakamura


     

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway