Tutorials - C++/PHP etc

http://www.java2s.com/

Posted under General, Programming, c++

This post was written by Chirag on April 15, 2009

Attributes of Objects

Attributes of Objects - Chirag Patel January 01, 2009

type
- Determines size of object and its memory address alignment
- the values the object can have
- the operations that can be performed on the object
- a function type specifies function parameter lists and return type

scope
- portion of translation unit (source code produced by preprocessor from source and header files) in which the name is visible
- file scope, block scope, function prototype scope, function scope
- C++ has namespace scope and class scope in addition

storage duration
- lifetime of the storage of object
- static, automatic or dynamic
- enumeration constants, functions, labels, types - do not have storage duration

linkage
- no linkage => entity can’t be referenced via name from anywhere else
- internal linkage => entity that can be referenced via name declared in the same scope or in other scopes of the same translation unit
- external linkage => in addition to capabilities similar to internal linkage, this can be referenced in other translation units

keywords: auto, extern, register, static - defines storage duration and/or linkage

Posted under c++

This post was written by Chirag on January 2, 2009

Code analysis

Check www.valgrind.org. This is Linux based tool and can detect code bugs early on.

Beware. It is not bug free either ;-) It is meant for dynamic analysis, and sometimes falsely alarms.

Posted under Infrastructure, Linux, c++

This post was written by Chirag on September 8, 2008

C++ coding tips

Some C++ coding tips:

- Do not turn off warning level(s) from compilation. This will save you trouble later. Use as much stricter settings as possible.

- Do not ignore warning(s) in your build process.

- Stick to ANSI C++ coding as much as possible.
All header file code must be contained within:

#ifndef __file_h
#define __file_h 1

… (code)

#endif
Use “#pragma once” as first line of header file if using Visual C++ compiler 2003 and later.

 - Do not use “goto” if possible.

- Always perform clean build before code submission and/or merge.

- Use “const” modifier as much as possible.

- Define “enum” with assigned values. Do not let it go for assumption. This improves readability. Say,

enum MyEnum {

SUNDAY = 0,

MONDAY = 1,

}; // enum for days

- Use brackets in pre-processor director. Say,

#define BIT(n)  (1 << n) // not preferred

#define BIT(n) (1 << (n)) // preferred

- Prefer library functions that take size or count argument over to functions without such argument for string manipulation. Say,

strncpy is preferred over strcpy.

- One include file should not contain more than one class definitions.

- Using “const” or “enum” is preferred over using “define” for contants. Say,

#define LOOP_COUNT  100 // non preferred

const int LOOP_COUNT = 100; // preferred

enum { LOOP_COUNT = 100, }; // or preferred

- A member function that does not modify class state must be declared as “const”.

- A class must have a default constructor with all data members initialized in it.

- A class must have copy constructor and assignment operator defined to avoid surprises.

- Assignment operator function must take care of destructive operation e.g. assigning to self (a = a).

- Avoid functions having more than five arguments.

- Do not mix malloc/free and new/delete.

- If array is allocated using “new”, use “delete []” for deallocating it.

- Avoid type conversions if possible.

- Re-initialize pointer when deallocated. Say,

delete p;

p = NULL;

- It is good to have a carriage return in the end of a code file.

- Use inclusive lower limit and exclusive upper limit in “for” loop. Say,

for ( int idx = 0; idx < MAX; ++idx ) // exclusive upper limit

for ( int idx = MAX; idx >= 1; –idx ) // inclusive lower limit

- Do not use “static” variable in “inline” function in header file.

I hope that this article is helpful and will avoid some silly bugs creeping over your code :-)

Posted under c++

This post was written by Chirag on August 8, 2008