Memory usage: #define vs. static const for uint8_tLockup while evaluating Wire.requestFrom function...

How big is a framed opening for a door relative to the finished door opening width?

Can a player sacrifice a creature after declaring that creature as blocker while taking lethal damage?

Renting a 2CV in France

Closed set in topological space generated by sets of the form [a, b).

How to completely remove a package in Ubuntu (like it never existed)

Why avoid shared user accounts?

Do authors have to be politically correct in article-writing?

Memory usage: #define vs. static const for uint8_t

Critique vs nitpicking

Count repetitions of an array

Crack the bank account's password!

What is the wife of a henpecked husband called?

What species should be used for storage of human minds?

Why do neural networks need so many examples to perform?

I have trouble understanding this fallacy: "If A, then B. Therefore if not-B, then not-A."

Article. The word "Respect"

What is a good reason for every spaceship to carry a weapon on board?

How to not let the Identify spell spoil everything?

Does it take energy to move something in a circle?

Boss asked me to sign a resignation paper without a date on it along with my new contract

Stuck on a Geometry Puzzle

Is `Object` a function in javascript?

Plausible reason to leave the Solar System?

Taking headphones when quitting job



Memory usage: #define vs. static const for uint8_t


Lockup while evaluating Wire.requestFrom function (Arduino)Malloc with Objects in Arduino librariesHID-Project problems with keycode enums'Non-Deterministic' memory usage on ArduinoQuestion of proper usage for F MacroDetailed analyse of memory usageWhat are safe memory usage limits?Is it possible to configure FRAM memory for use as stack and heap?Reducing Memory Usage lots of Floatsmemory usage “dos and don'ts”













1















I'm writing an Arduino library to communicate with an I2C device, and I'm wondering what the best way is to declare all the register addresses so as to save memory.



Using #defines:



#define REGISTER_MOTOR_1_MODE 0x44
#define REGISTER_MOTOR_2_MODE 0x47


Or using static const:



static const uint8_t REGISTER_MOTOR_1_MODE = 0x44;
static const uint8_t REGISTER_MOTOR_2_MODE = 0x47;


(Obviously I have more than just two registers I need to declare, but I thought two would illustrate the point just fine)










share|improve this question























  • define's are just "find and replace"s, and can bit you, if you do anything other than numbers. For example #define LED_MASK 0x01<<2. A safe(r) way to write that would be #define LED_MASK (0x01<<2). See also stackoverflow.com/questions/6542270/…

    – Gerben
    4 hours ago
















1















I'm writing an Arduino library to communicate with an I2C device, and I'm wondering what the best way is to declare all the register addresses so as to save memory.



Using #defines:



#define REGISTER_MOTOR_1_MODE 0x44
#define REGISTER_MOTOR_2_MODE 0x47


Or using static const:



static const uint8_t REGISTER_MOTOR_1_MODE = 0x44;
static const uint8_t REGISTER_MOTOR_2_MODE = 0x47;


(Obviously I have more than just two registers I need to declare, but I thought two would illustrate the point just fine)










share|improve this question























  • define's are just "find and replace"s, and can bit you, if you do anything other than numbers. For example #define LED_MASK 0x01<<2. A safe(r) way to write that would be #define LED_MASK (0x01<<2). See also stackoverflow.com/questions/6542270/…

    – Gerben
    4 hours ago














1












1








1








I'm writing an Arduino library to communicate with an I2C device, and I'm wondering what the best way is to declare all the register addresses so as to save memory.



Using #defines:



#define REGISTER_MOTOR_1_MODE 0x44
#define REGISTER_MOTOR_2_MODE 0x47


Or using static const:



static const uint8_t REGISTER_MOTOR_1_MODE = 0x44;
static const uint8_t REGISTER_MOTOR_2_MODE = 0x47;


(Obviously I have more than just two registers I need to declare, but I thought two would illustrate the point just fine)










share|improve this question














I'm writing an Arduino library to communicate with an I2C device, and I'm wondering what the best way is to declare all the register addresses so as to save memory.



Using #defines:



#define REGISTER_MOTOR_1_MODE 0x44
#define REGISTER_MOTOR_2_MODE 0x47


Or using static const:



static const uint8_t REGISTER_MOTOR_1_MODE = 0x44;
static const uint8_t REGISTER_MOTOR_2_MODE = 0x47;


(Obviously I have more than just two registers I need to declare, but I thought two would illustrate the point just fine)







c++ memory-usage






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 5 hours ago









Android DevAndroid Dev

1084




1084













  • define's are just "find and replace"s, and can bit you, if you do anything other than numbers. For example #define LED_MASK 0x01<<2. A safe(r) way to write that would be #define LED_MASK (0x01<<2). See also stackoverflow.com/questions/6542270/…

    – Gerben
    4 hours ago



















  • define's are just "find and replace"s, and can bit you, if you do anything other than numbers. For example #define LED_MASK 0x01<<2. A safe(r) way to write that would be #define LED_MASK (0x01<<2). See also stackoverflow.com/questions/6542270/…

    – Gerben
    4 hours ago

















define's are just "find and replace"s, and can bit you, if you do anything other than numbers. For example #define LED_MASK 0x01<<2. A safe(r) way to write that would be #define LED_MASK (0x01<<2). See also stackoverflow.com/questions/6542270/…

– Gerben
4 hours ago





define's are just "find and replace"s, and can bit you, if you do anything other than numbers. For example #define LED_MASK 0x01<<2. A safe(r) way to write that would be #define LED_MASK (0x01<<2). See also stackoverflow.com/questions/6542270/…

– Gerben
4 hours ago










2 Answers
2






active

oldest

votes


















6














You'll find no noticeable difference memory-wise between the two.



The only real difference is that the const method also imposes a type to the value, which can be useful for function overloading or mathematical operations.






share|improve this answer































    3














    A #define is a preprocessor macro. As Gerben says in his comment, it's just an automated find-and-replace.



    If you use it to hold things like C string constants, e.g. #define ERROR_STRING "You messed up, bud!" it could actually cause your program to take more memory, since that same string literal will be duplicated every time you reference it. In that case a static const would be both type-safe AND reduce memory usage.






    share|improve this answer
























    • Would it be duplicated? I mean, isn't it the job of the compiler to ensure "You messed up, bud!" occurs exactly once in the string constant table?

      – juhist
      1 hour ago






    • 1





      @juhist: C++ explicitly states that the compiler can choose to duplicate identical strings, or not. Most compilers have a flag to either do this, or not. I think, by default, most compilers do not merge strings, in case the app relies on them being unique.

      – Mooing Duck
      1 hour ago











    Your Answer






    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("schematics", function () {
    StackExchange.schematics.init();
    });
    }, "cicuitlab");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "540"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f61970%2fmemory-usage-define-vs-static-const-for-uint8-t%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    You'll find no noticeable difference memory-wise between the two.



    The only real difference is that the const method also imposes a type to the value, which can be useful for function overloading or mathematical operations.






    share|improve this answer




























      6














      You'll find no noticeable difference memory-wise between the two.



      The only real difference is that the const method also imposes a type to the value, which can be useful for function overloading or mathematical operations.






      share|improve this answer


























        6












        6








        6







        You'll find no noticeable difference memory-wise between the two.



        The only real difference is that the const method also imposes a type to the value, which can be useful for function overloading or mathematical operations.






        share|improve this answer













        You'll find no noticeable difference memory-wise between the two.



        The only real difference is that the const method also imposes a type to the value, which can be useful for function overloading or mathematical operations.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 5 hours ago









        MajenkoMajenko

        68.1k43277




        68.1k43277























            3














            A #define is a preprocessor macro. As Gerben says in his comment, it's just an automated find-and-replace.



            If you use it to hold things like C string constants, e.g. #define ERROR_STRING "You messed up, bud!" it could actually cause your program to take more memory, since that same string literal will be duplicated every time you reference it. In that case a static const would be both type-safe AND reduce memory usage.






            share|improve this answer
























            • Would it be duplicated? I mean, isn't it the job of the compiler to ensure "You messed up, bud!" occurs exactly once in the string constant table?

              – juhist
              1 hour ago






            • 1





              @juhist: C++ explicitly states that the compiler can choose to duplicate identical strings, or not. Most compilers have a flag to either do this, or not. I think, by default, most compilers do not merge strings, in case the app relies on them being unique.

              – Mooing Duck
              1 hour ago
















            3














            A #define is a preprocessor macro. As Gerben says in his comment, it's just an automated find-and-replace.



            If you use it to hold things like C string constants, e.g. #define ERROR_STRING "You messed up, bud!" it could actually cause your program to take more memory, since that same string literal will be duplicated every time you reference it. In that case a static const would be both type-safe AND reduce memory usage.






            share|improve this answer
























            • Would it be duplicated? I mean, isn't it the job of the compiler to ensure "You messed up, bud!" occurs exactly once in the string constant table?

              – juhist
              1 hour ago






            • 1





              @juhist: C++ explicitly states that the compiler can choose to duplicate identical strings, or not. Most compilers have a flag to either do this, or not. I think, by default, most compilers do not merge strings, in case the app relies on them being unique.

              – Mooing Duck
              1 hour ago














            3












            3








            3







            A #define is a preprocessor macro. As Gerben says in his comment, it's just an automated find-and-replace.



            If you use it to hold things like C string constants, e.g. #define ERROR_STRING "You messed up, bud!" it could actually cause your program to take more memory, since that same string literal will be duplicated every time you reference it. In that case a static const would be both type-safe AND reduce memory usage.






            share|improve this answer













            A #define is a preprocessor macro. As Gerben says in his comment, it's just an automated find-and-replace.



            If you use it to hold things like C string constants, e.g. #define ERROR_STRING "You messed up, bud!" it could actually cause your program to take more memory, since that same string literal will be duplicated every time you reference it. In that case a static const would be both type-safe AND reduce memory usage.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 3 hours ago









            Duncan CDuncan C

            1,7091617




            1,7091617













            • Would it be duplicated? I mean, isn't it the job of the compiler to ensure "You messed up, bud!" occurs exactly once in the string constant table?

              – juhist
              1 hour ago






            • 1





              @juhist: C++ explicitly states that the compiler can choose to duplicate identical strings, or not. Most compilers have a flag to either do this, or not. I think, by default, most compilers do not merge strings, in case the app relies on them being unique.

              – Mooing Duck
              1 hour ago



















            • Would it be duplicated? I mean, isn't it the job of the compiler to ensure "You messed up, bud!" occurs exactly once in the string constant table?

              – juhist
              1 hour ago






            • 1





              @juhist: C++ explicitly states that the compiler can choose to duplicate identical strings, or not. Most compilers have a flag to either do this, or not. I think, by default, most compilers do not merge strings, in case the app relies on them being unique.

              – Mooing Duck
              1 hour ago

















            Would it be duplicated? I mean, isn't it the job of the compiler to ensure "You messed up, bud!" occurs exactly once in the string constant table?

            – juhist
            1 hour ago





            Would it be duplicated? I mean, isn't it the job of the compiler to ensure "You messed up, bud!" occurs exactly once in the string constant table?

            – juhist
            1 hour ago




            1




            1





            @juhist: C++ explicitly states that the compiler can choose to duplicate identical strings, or not. Most compilers have a flag to either do this, or not. I think, by default, most compilers do not merge strings, in case the app relies on them being unique.

            – Mooing Duck
            1 hour ago





            @juhist: C++ explicitly states that the compiler can choose to duplicate identical strings, or not. Most compilers have a flag to either do this, or not. I think, by default, most compilers do not merge strings, in case the app relies on them being unique.

            – Mooing Duck
            1 hour ago


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Arduino Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f61970%2fmemory-usage-define-vs-static-const-for-uint8-t%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can't compile dgruyter and caption packagesLaTeX templates/packages for writing a patent specificationLatex...

            Schneeberg (Smreczany) Bibliografia | Menu...

            Hans Bellmer Spis treści Życiorys | Upamiętnienie | Przypisy | Bibliografia | Linki zewnętrzne |...