Wednesday, February 24, 2010

Embedded Engineer: integer promotion rule - Programming in C and C++

One thing Embedded engineer should know is integer promotion rule. Declaring the variable in correct makes life lot more easy in debugging and it should a good practice of programming as well.
Below example is for integer promotion rule.

unsigned int uIntVar = 2;
  int intVar = -10;
    (uIntVar+intVar > 2) ? puts("> 2") : puts("<= 2");

Answer is >2. Expression having the signed and unsigned integers, signed integer becomes a large number and evaluates to positive number in above example.
However it becomes a negative number if
int result = intVar  + uIntVar;  Or type cast the result to int

Now let me post questions.
1) What is the return value if invoke malloc with size zero?
2) How do you write a 1's compliment for given number?

Answer for the first question is strange where second one tests whether you are a embedded engineer.

Interrupt Service Routine - ISR

Thumb rule for ISR is : It should be short and sweet.
1) Will not have any parameters or return values.
2) Should avoid performance hit operations like floating point operations.
3) Should not contain non-entrant functions.
If somebody gives you an example code to identify the problems in the ISR routine, these points should be considered to identify the faults.

Thursday, February 18, 2010

C and C++ Programming - Best practice for Singleton pattern

Best practice for the singleton class to avoid the mis-usage.

1) Make the constructor as private.
2) Hide the copy constructor
3) Hide the = operator
4) Return the reference of the instance instead of pointer in the instance() method

Below is the example:

class Singleton
{

public:

    /*!
     * @brief Method to retrieve the object (singleton pattern)
     */
    static Singleton & instance();

    /*!
     * @brief Destructor
     */
    virtual ~Singleton();

protected:

private:
    /*!
     * @brief constructor, which can only be accessed by instance() method
     */
    Singleton();

    /**
     * @brief Hide the copy constructor.
     */
    Singleton(const Singleton & p_singleton);

    /**
     * @brief Hide the operator '='.
     */
    Singleton & operator =(const Singleton & p_rhs);

    /*!
     * @brief singleton, can only be accessed by instance()
     */
    static Singleton* c_pInstance;
};

C and C++ Programming - How to do you decide whether to have a variable as class member or static class member

Simple but people often do mistakes!
I have seen people writing code, where some of variables does not fit to be class member still used as class members. If a variable not required during the life of  class instance then it is not required to have it as class member. Remember static class members are per class not per instance.

Tuesday, February 9, 2010

C and C++ Programming - Constant usage in C/C++

Constant:
Constant means read-only. Answer is not complete but still good. Constant can be declared in various way for various needs.

const int a; //Constant (non-modifiable) integer
int const a; //Constant (non-modifiable) integer
const int *a; //pointer to a Constant (non-modifiable) integer
int * const a; //Constant (non-modifiable) pointer to a integer
int const * a const; //Constant (non-modifiable) pointer to constant integer

You can write a program without using a constant keyword which would work without any problem. But writing a program using constant is good practice and dictates user on how to use them based on its behavior.


Post GSM/GPRS(BTS) Question and Get Answers

You can post question to me on GSM/GPRS/LTE, even C and C++ questions, I will try my best answer

Monday, February 8, 2010

C and C++ Programming - When and where to use volatile keyword

Main usage of volatile seen in embedded space. Practical use is seen in
1) Memory mapped peripheral registers 2) Global variables modified by ISR routine 3) Global variables modified by multi-threaded environment. In these scenarios values of these variable can change unexpectedly.

Watch out for the below behaviours and first thing to look for is assessing  for the need for volatile variables.
 Code stops working when
1) Multithreaded environment but works in single process/threads
2) Enable optimization
3) Enable Interrupts

Volatile qualifier will be used when the variable is declared, which tells the compiler that value of the variable can change without any action taken by code.

volatile int myVolatile;
int volatile myVolatile;
What is the difference between two?
If you do not know the answer let me know.

int * volatile myVolatile;
This means volatile pointer to non volatile variable.
int volatile * volatile myVolatile;
Volatile pointer to Volatile variable.