#define vs. const

#define vs. const - Hallo gues welcome to my blog, you can read this article with title #define vs. const, Happy reading

IMPORTANT, MUST BE READ... : #define vs. const
Title : #define vs. const

Read More


#define vs. const

IMPORTANT, MUST BE READ... Is your code amount of "#define" statements?  If so, yous should see switching to the const keyword.

Old schoolhouse C:
    #define MYVAL 7

Better approach:
   const uint32_t myVal = 7;

Here are approximately reasons yous should role const instead of #define:
  • #define has global scope, therefore you're creating (read-only) global values every fourth dimension yous role #define. Global ambit is evil, therefore don't create that.  (Read-only global ambit for constant values is a chip less evil than global variables per se, particularly if yous can't role the namespace features of C++. But costless global ambit is ever a bad idea.) Influenza A virus subtype H5N1 const choice tin obey scoping rules, including beingness purely local if defined within a procedure, or to a greater extent than commonly file static alongside the "static" keyword.
  • Const lets yous create to a greater extent than aggressive type checking (depending upon your compiler too static analysis tools, particularly if yous role a typedef to a greater extent than specific than built-in C information types). While C is a chip weak equally a linguistic communication inward this surface area compared to other languages, a classical instance is a const lets yous position a publish equally beingness inward feet or meters, spell the #define approach is simply equally if you'd typed the publish vii inward alongside no units. The #define approach tin seize alongside teeth yous if yous role the incorrect value inward the incorrect place. Type checking is an effective trend to uncovering bugs, too using #define gives upward an chance to allow static analysis tools help yous alongside that.
  • Const lets yous role the value equally if it were a variable when yous demand to (e.g., passing an address to the variable) without having to alter how the variable is defined.
  • #define inward full general is therefore bug-prone that yous should minimize its role simply to avoid having to pass fourth dimension hollo for "is this i OK?" inward a peer review. Most #define uses tend to live on const variables inward old-school code, therefore getting rid of them tin dramatically cut the peer review burden of sifting through hundreds of #define statements to await for problems.
Here are approximately mutual myths nearly this tradeoff. (Note that on approximately systems these statements mightiness live on true, particularly if yous accept too former too lame compiler.  But they don't necessarily accept to live on truthful too they oft are false, particularly on newer chips alongside newer compilers.)
  • "Const wastes memory."  False if yous accept a compiler that is smart plenty to create the correct thing. Sure, if yous desire to decease a pointer to the const it volition genuinely accept to alive inward retentiveness somewhere, but yous can't fifty-fifty decease a pointer to a #define at all. One of the points of "const" is to give the compiler a hint that lets it optimize retentiveness footprint.
  • "Const won't function for X." Generally faux if yous accept a newer compiler, too particularly if yous are using a mostly-C subset of the capability of a C++ compiler, equally is increasingly common. And honestly, most of the fourth dimension #define is simply beingness used equally a plainly former integer const to become rid of magic numbers. const volition function fine.  (If yous accept magic numbers instead of #define, too then yous accept bigger problems than this even.) Use const for the no-brainer cases. Something is in all probability incorrect if everything nearly your code is therefore special yous demand #define everywhere.
  • "Const hassles me nearly type conversions."  That's a characteristic to forbid yous from beingness sloppy!  So strictly speaking the compiler doing this is non a myth. The myth is that this is a bad thing.
There are plenty of discussions on this topic.  You'll also run into that approximately folks advocate using enums for approximately situations, which we'll become to approximately other time. For now, if yous alter equally many #defines equally yous tin to consts too then that is probable to ameliorate your code quality, too maybe even out a few bugs yous didn't realize yous had.

Be careful when reading give-and-take grouping postings on this topic.  There is a lot of dis-information out at that spot nearly functioning too other potential tradeoff factors, normally based on statements nearly twenty twelvemonth former versions of the C linguistic communication or experiences alongside compilers that accept wretched optimization capability.  In general, yous should ever role const past times default unless your item compiler/system/usage presents a compelling instance non to.

See also the Barr Group C coding measure dominion 1.8.b which says to role const, too has a publish of other really useful rules.




IMPORTANT, MUST BE READ...

Thank for your attention #define vs. const

my blog #define vs. const, Have a nice day.

Now you read article #define vs. const this permalink article is http://fairemirima.blogspot.com/2017/11/define-vs-const.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 "#define vs. const"

Post a Comment