what is the disadvantage of #define

Discussion in 'C++' started by ankitsjain, Sep 14, 2012.

  1. ankitsjain

    ankitsjain New Member

    Joined:
    Sep 9, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    tell me what is the disadvantage of #define?
    and what do u mean by run time error and compile time error??
     
  2. state

    state New Member

    Joined:
    Sep 14, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Hi ankitsjain
    #define is not type safe
    #define's replaces any code that follows that in the main program where ever they are referred to.So more the code you place after #define more the compile time.
    Errors that arise during compilation due to syntax,semantics,type mismatch,etc.For example
    int main(void)
    {
    int a;
    //no closing braces
    Errors that arise at runtime.eg.stack overflow errors,errors due to insufficient memory,opening a file that does not exist,etc.
    Happy programming
     
  3. dipakk

    dipakk New Member

    Joined:
    Sep 2, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    Disadvantages of #define,
    1. Preprocessor not doing type checking
    2. May not store variable in symbol table
    3. Need to care for parenthesis
    4. Preprocessor hard code #define variable. Code size become bulky
    5. Any changes in macro , need to re-compile all source code.

    1. Compile Time error
    Error encounter while compiling program by compiler, like syntax error, type checking errors etc.

    For example, Declare preprocessor variable in header file and same use in another source
    file. At time of compilation, compiler never know about preprocessor variable as preprocessor
    replace it with it's macro value and Preprocessor variable may not store into symbol table for debugging purpose.
    It make sometime difficult to trace error root point if we had not written header file.


    2. Run time Error
    Error encounter at time of executing program by operating system, like referencing null pointer,
    insufficient memory for allocation etc.
    Example, If macro body has not taken care with parenthesis on mathematical operations.

    Macro Example :

    Code:
    #include <iostream>
    using namespace std;
    
    #define MAX_SIZE 1000000000
    #define FUNC(a,b) a*b
    
    int main()
    {
        //1. Compile Time Error
        int* pArr = new int[MAX_SIZE];
    
        //2. Run Time Error
        cout<<FUNC(2+1,5+1);
    
        cin.get();
       
    return 0;
    }
     
  4. lan keylogger

    lan keylogger New Member

    Joined:
    Jan 29, 2013
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.lan-monitoring.com/lan-keylogger.htm
    Because a-b=0, so following will happen run time error:
    int a=3;
    int b=3;
    int c=3/(a-b);
    When compile it, it can't discover a-b=0;

    The following will happen compile time error:
    int a=3;
    double b=3.1415;
    a=b;
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice