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

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
  1. · C++ Programmer Alerts
  2. · Function Basics and Libraries
  3. · List
  4. · Pointers and Dynamic Memory

print this article

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.


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.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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