code in dot H files
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: code in dot H files

  1. #1
    Join Date
    Nov 2011
    Posts
    69

    code in dot H files

    An engineer just left our project and now I am working on his project. In his dot H files there are several routines where he writes the executable code in the declaration rather than the dot C or dot CPP file. For a method with maybe one line of code I don't see a real problem with that. However he has multiple methods written like this, one of which is over 150 lines of code. (Not a comment in sight but that is a different topic.)

    Question:
    Is there a possible advantage in writing the code in the dot H file rather than the dot CPP file?

    Seems to me its better to put all the code in one place.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    Re: code in dot H files

    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Nov 2011
    Posts
    69

    Re: code in dot H files

    No, it is definately not template code.

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,359

    Re: code in dot H files

    Quote Originally Posted by bkelly View Post
    Question:
    Is there a possible advantage in writing the code in the dot H file rather than the dot CPP file?

    Seems to me its better to put all the code in one place.
    Quote Originally Posted by bkelly View Post
    No, it is definately not template code.
    Then I don't see any advantage in putting the implementation code in the dot H file.
    Instead, there is a disadvantage: if you change something in the implementation being in .h file then all the cpp files that #include this header will be recompiled.
    Victor Nijegorodov

  5. #5
    Join Date
    Nov 2011
    Posts
    69

    Re: code in dot H files

    I agree and those are my thoughts. The previous guy did so much like this that I figured there might be something I am totaly unaware of.
    Thanks for taking the time to reply.

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    Re: code in dot H files

    If the function is small, then there is the slight advantage that the compile can 'inline' the function. However, I highly doubt the compiler can inline a function 150 lines long.

    Viggy

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,845

    Re: code in dot H files

    Inlining may not be an issue, All compilers take inlining as a hint only, any function that results in too much generated code bytes will simply not get inlined at all, even if it's in a .h file and even if you tried to force it with an inline or __inline or what your compiler offers in that area.

    There is more to it than merely inlining however. By having the code in a .h the compiler is aware of what the fucntion does and may be able to simplify the code which may eventually lead to the compiler inserting only minor fragments of the 150line code. This can happen for highly branched code where the branches are taken ONLY based on parameter values (assuming they are POD types) of the function. Suppose something like;

    Code:
    int foo(int bar, double val)
    {
       if (bar==2)
           return val*2;
       else if (bar==3)
           return val*val;
       else if (bar==4)
           return val/5;
       else if (bar>4 && bar<20)
           return val;
    //...lots more tests...
       else
          return val+1;
    }
    With this code in a .cpp, all the compiler can do is generate calls to this function from any source other than the source where this function code resides.


    With the above in a .h things get interesting.

    With a call like foo(barvar, valvar); where barvar is a variable that the compiler cannot guarantee the value for, it will need to generate a function call, and have the entire function body in the executable as well.
    However, a call like int i = foo(123,3); might very well be optimized to int i = 4;
    and a call like int i = foo(4, valvar); might be optimised to int i = valvar/5;


    Changing the function to a .cpp, could have devastating consequences for performance if such a function is 'called' inside performance critical code.

    That programmer could be a C++ genious, he could be an idiot and even in this case, he could be an idiot that just happened to have done the right thing for the wrong reasons.


    If you have no comments to go by stating that this needs to be in a .h for a particular reason, then functional analysis of if this would even do much being inlined/optimized or very detailed performance testing can reveal what's really going on.

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,845

    Re: code in dot H files

    Quote Originally Posted by MrViggy View Post
    If the function is small, then there is the slight advantage that the compile can 'inline' the function. However, I highly doubt the compiler can inline a function 150 lines long.
    it won't inline 150lines of code. but...
    it may optimize away most of those 150lines of code and only inline the bit it needs.

    I have template functions (the fact they're templates really doesn't matter in this decision) that are much larger than 150lines, but where the compiler can extract only the short bit of code it needs when the function is 'called' with certain literal values.

  9. #9
    Join Date
    Jul 2002
    Posts
    2,512

    Re: code in dot H files

    Code in h-files is not subject of local redininitions that are made in .cpp files, for example, DEBUG_NEW redifinition. It is very difficult to find memory leaks if their source is in h-files.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center