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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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 / 5
    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


    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

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT