Don't role macros for MIN as well as MAX

Don't role macros for MIN as well as MAX - Hallo gues welcome to my blog, you can read this article with title Don't role macros for MIN as well as MAX, Happy reading

IMPORTANT, MUST BE READ... : Don't role macros for MIN as well as MAX
Title : Don't role macros for MIN as well as MAX

Read More


Don't role macros for MIN as well as MAX

IMPORTANT, MUST BE READ...

It is mutual to run across small-scale helper functions implemented every bit macros, peculiarly inward older C code. Everyone seems to produce it.  But you lot should avoid macros, too instead utilization inline functions.

The motivation for using macros was originally that you lot needed to utilization a small-scale business office inward many places but were worried well-nigh the overhead of doing a subroutine call. So instead, you lot used a macro, which expands into source code inward the preprocessor phase.  That was a reasonable tradeoff forty years ago. Not such a neat stance now, because macros drive problems for no skillful reason.

For example, you lot mightiness seem on the Web too abide by these mutual macros
    #define MAX(a,b) ((a) > (b) ? a : b)
    #define MIN(a,b) ((a) < (b) ? a : b)

And you lot mightiness abide by that it seems to piece of work for a while.  You mightiness larn bitten past times the missing "()" guards approximately the 2d re-create of a too b inward the inward a higher house -- which version you lot larn depends on which cutting & glue code site you lot visit. 

But thence you'll abide by that at that spot are yet weird situations where you lot larn unexpected behavior. For example, what does this do?
    c = MAX(a++, b);
If a is greater than b executing the code volition increment a twice, but if a is less than or equal to b it volition solely increment a once.  And if you lot outset mixing types or putting complicated expressions into the macro things tin larn weird too buggy inward a hurry.

Another related employment is that the macro volition expand, increasing the cyclomatic complexity of your code. That's because a macro is equivalent to you lot having seat the conditional branch into the source code. (Remember, macro expansion is done past times the preprocessor, the thence compiler itself acts every bit if you'd typed the conditional assignment aspect every house you lot utilization the macro.) This complexity rating is justified, because at that spot is no actual physical care for that tin travel unit of measurement tested independently.

As it turns out, macros are evil. See the C++ FAQ: https://isocpp.org/wiki/faq/misc-technical-issues#macros-with-if  which lists iv dissimilar types of evil behavior.  There are fancy hacks to endeavour to larn whatever item macros such every bit MIN too MAX to travel meliorate behaved, but no affair how difficult you lot endeavour you're actually exactly making a bargain alongside the devil. 

What's the fix?

The create is: don't utilization macros. Instead utilization inline physical care for calls.

You should already bring access to built-in functions for floating betoken such every bit fmin() too fmax().  If it's there, utilization the materials from your compiler vendor instead of writing it yourself!

If your compiler doesn't bring integer instant too max, or you lot are worried well-nigh breaking existing macro code, convert the macros into inline functions alongside minimal changes to your code base:

inline int32_t MAX(int32_t a, int32_t b) { return((a) > (b) ? a : b); }
inline int32_t MIN(int32_t a, int32_t b) { return((a) < (b) ? a : b); }

If you lot bring other types to bargain alongside you lot mightiness require dissimilar variants depending on the types, but oft a slice of code uses predominantly i information type for its calculations, thence inward exercise this is normally non a big deal. And don't forget, if your create environs has a built inward instant or max you lot tin exactly laid upwards the macro to telephone phone that directly.

What well-nigh performance?

The motivation for using macros dorsum inward the bad former days was efficiency. H5N1 subroutine telephone phone involved a lot of overhead. But the inline keyword tells the compiler to expand the code in-place piece retaining all the advantages of a subroutine call.  Compilers are pretty skillful at optimization these days. So at that spot is no overhead at run-time.  I've besides seen advice to seat the inline business office inward a header file thence it volition travel visible to whatever physical care for needing it, too the macro was already at that spot anyway.

Strictly speaking, "inline" is a proposition to the compiler. However, if you lot bring a decent compiler it volition follow the proposition unless the inline business office is thence big the telephone phone overhead exactly doesn't matter. Some compilers bring a alarm flag that volition permit you lot know when the inline didn't happen.  For example, utilization -Winline for gcc.  If your compiler ignores "inline" for something every bit straightforward every bit MIN or MAX, larn a dissimilar compiler.

What well-nigh multiple types?

H5N1 perceived payoff of the macro approach is that you lot tin play fast too unloose alongside types.  But playing fast too unloose alongside types is a BAD IDEA because you'll larn bugs.  

If you lot actually loathe having to gibe the business office cite to the information types thence what you lot require is to switch to a linguistic communication that tin grip this past times automatically picking the correct business office based on the operator types. In other words, switch from a to a linguistic communication that is 45 years former (C) to i that is solely well-nigh 35 years former (C++).  There's a newspaper from 1995 that explains this inward the context of instant too max implemented alongside templates:  http://www.aristeia.com/Papers/C++ReportColumns/jan95.pdf
As it turns out the rabbit hole goes a lot deeper than you lot mightiness intend for a generic solution.

But you lot don't bring to larn downwards the rabbit hole.  For most code the best response is but to utilization inline functions too alternative the business office cite that matches your information types. You shouldn't lose whatever functioning at all, too you'll travel probable to salve a lot of fourth dimension chasing obscure bugs.



IMPORTANT, MUST BE READ...

Thank for your attention Don't role macros for MIN as well as MAX

my blog Don't role macros for MIN as well as MAX, Have a nice day.

Now you read article Don't role macros for MIN as well as MAX this permalink article is https://fairemirima.blogspot.com/2017/11/dont-role-macros-for-min-as-well-as-max.html Thank you and Best regards. You Can read nice Tips below. It was always better to choose topics that interest you or in wich you at least have some knowledge about . When creating targeted internet copywriting , you have to stick with your strong points , or everyone will know it . Make a list of all of the things and or topics that you are interested in . . . How much do you know ? Can you tell it as a story ? That is The essence of writing for the web . You Have to know your subject well , or nobody will believe you it is always better to impress someone then upset them . When Writing Targeted Internet Copywriting , you have to choose your appropriate target group of customers . without a target group of customers , you could ramble on incessantly about random subjects for days on end with no essence of a final goal . You always have to keep in mind who your customers are and what they are looking for . . . . . . . . . IMPORTANT, MUST BE READ...

0 Response to "Don't role macros for MIN as well as MAX"

Post a Comment