Memory usage: #define vs. static const for uint8_t
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
add a comment |
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
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
add a comment |
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
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
c++ memory-usage
asked 6 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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered 6 hours ago
Majenko♦Majenko
68.1k43277
68.1k43277
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
answered 4 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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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