C++
  Home arrow C++ arrow Page 4 - C++ Programmer Alerts
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
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? 
C++

C++ Programmer Alerts
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-04-02

    Table of Contents:
  • C++ Programmer Alerts
  • Function Basics and Libraries
  • List
  • Pointers and Dynamic Memory

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    C++ Programmer Alerts - Pointers and Dynamic Memory


    (Page 4 of 4 )

    Always Define one Object per Statement

    Some programmers juxtapose the dereferencing operator with the type name rather than the object name when defining pointer objects, as in the following:


    char* ptr;


    The above definition will work, but what about the following:


    char* p, q; //really char *p; char t


    Here only p is a pointer; q is not a pointer. The statement first creates an object, p, that is a pointer to a char. It then creates an object, q, that is a char. The different object types are a result of the right associativity of the dereferencing operator – even though the dereferencing operator is juxtaposed with the type char, it is still associated with object p. To be prudent, always define one object per statement; also put the dereferencing operator next to the object name and not the type name.

    Inadvertent Representation Errors

    Consider the following:


    int B[5];

    float y;

    int *ptr = &B[4]; // ptr points to last element of B

    ++ptr; //undefined: ptr not pointing to an int location


    In both increment and decrement operations, the system does not check whether the object stored at the resulting address is the same type as the base type of the pointer object during execution. If a mismatch occurs, then the effect on the program is undefined.

    In the above code segment, the initialization of ptr makes it point to the last element of int array B. In most C++ implementations, float object y will occur immediately after array B in memory. Therefore, the subsequent increment of ptr makes it point to the representation of y. So, the further use of ptr produces undefined results.

    There should be enough memory

    If you are using a char pointer with an extraction, the pointer must point to a char array of sufficient length to store the extracted characters. The pointer is automatically assumed to be pointing to valid and sufficient memory. When this is not the case, the effect of the extraction on your program is undefined.

    TEMPLATES AND POLYMORPHISM

    Friends should not be Trusted

    Use a friend only as a last resort for the following reason: although the friend mechanism provides some control over what function, operator, or class manipulates the underlying data representation, it still creates a major security hole with respect to information hiding.

    All these alerts are what I have gathered based on my experience and the experience of expert programmers.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · If you have been having mysterious problems when coding in C++, then this article...
     

    C++ ARTICLES

    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection
    - Temporary Variables: Chasing Temporaries Away
    - Temporary Variables: Temporaries Are Not Nec...
    - Temporary Variables: Keep Your Values Close,...







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