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.
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:
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 *ptr = &B; // 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.