Home arrow C++ arrow Page 3 - C++ Programmer Alerts
C++

C++ Programmer Alerts


In this article I show you how you can avoid common programming errors in C++. There will be examples (including code) that you can use to practice on your own. Are you interested? Then click on the link and start reading.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
April 02, 2008
TABLE OF CONTENTS:
  1. · C++ Programmer Alerts
  2. · Function Basics and Libraries
  3. · List
  4. · Pointers and Dynamic Memory

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- 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++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials