C++
  Home arrow C++ arrow Page 3 - 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
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    C++ Programmer Alerts - List


    (Page 3 of 4 )

    Wrong Subscripting

    C++ does not provide an automatic way to ensure that proper array subscripts are used. For example, the following code does not generate an error message:


    int C[10];

    int D[5];

    D[-5] = 2;


    Most compilers would assign array D immediately after array C in their activation record, so the likely effect of the preceding assignment is to modify the memory location of object C[5], instead of issuing an error message. Therefore D[-5] would be referring to the fifth element from the end of array C.

    The most common subscript error is misreading the last element of an array. Both experienced and beginning programmers sometimes forget that the last element of the array has a subscript one less than the size of the array.

    In part, because there is no automatic way to ensure that proper subscripts are used, programmers have turned to container classes provided in the Standard Template Library. These classes provide iterators and member functions that provide controlled access to the list elements. It is also possible to easily extend these classes so that subscripts are checked automatically.

    Use the Right Delimiter

    The following is the definition of a vector A of 10 int elements:


    Vector<int> A(10);


    However, someone can easily write:


    Vector<int> A[10];


    For the second case, object A is not a vector of 10 int elements, since the number 10 is inside square brackets and not curved parentheses. For this second case, object A is actually an array of size 10 whose elements are of type vector<int>. As you can see, this can cause great trouble. Always be careful when you are defining a vector.

    Multiple Angled Brackets

    One of the steps taken when translating a program is lexical analysis, which is where the individual elements of a program are determined. For example, when >> is encountered, many compilers will never treat these consecutive angled brackets as two distinct elements. They will group them as a single element (e.g. insertion operator). So the following statement may not compile as you expect:


    vector<vector<int>> B;


    That is, you may not have a two dimensional vector of int elements, since >> would be interpreted as a single element. A preferred way to define the two dimensional vector is:


    vector<vector<int> > B;


    There is a white space between the angled brackets, so the compiler will see the brackets as two distinct elements.

    More C++ Articles
    More By Chrysanthus Forcha


       · 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,...


    Iron Speed





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