Safely writing directly to a memory addressWhat's the shortest code to write directly to a memory address in C/C++?Typedef function pointer?Initializing a pointer with an absolute memory addressWrite-Only pointer typeHow does pointer type casting work in cWriting to hardware register using void *C standard compliant way to access null pointer address?Safe usage of longjmp/setjmp with volatileBest way to avoid cast integer to pointer when dealing with memory access (MISRA 2008)Memory mapping peripheral registers using pointer array
How do I lift the insulation blower into the attic?
Is there a distance limit for minecart tracks?
1 John in Luther’s Bibel
Would this string work as string?
Do people actually use the word "kaputt" in conversation?
Mortal danger in mid-grade literature
What is the tangent at a sharp point on a curve?
What is this high flying aircraft over Pennsylvania?
Calculate Pi using Monte Carlo
Are hand made posters acceptable in Academia?
Trouble reading roman numeral notation with flats
Why is participating in the European Parliamentary elections used as a threat?
How would a solely written language work mechanically
Why does the frost depth increase when the surface temperature warms up?
Has the laser at Magurele, Romania reached the tenth of the Sun power?
Air travel with refrigerated insulin
Can a Knock spell open the door to Mordenkainen's Magnificent Mansion?
Is there any common country to visit for persons holding UK and Schengen visas?
Offset in split text content
Recursively move files within sub directories
python displays `n` instead of breaking a line
When is the exact date for EOL of Ubuntu 14.04 LTS?
Hashing password to increase entropy
Travelling in US for more than 90 days
Safely writing directly to a memory address
What's the shortest code to write directly to a memory address in C/C++?Typedef function pointer?Initializing a pointer with an absolute memory addressWrite-Only pointer typeHow does pointer type casting work in cWriting to hardware register using void *C standard compliant way to access null pointer address?Safe usage of longjmp/setjmp with volatileBest way to avoid cast integer to pointer when dealing with memory access (MISRA 2008)Memory mapping peripheral registers using pointer array
I'm writing to a register with a known address.
volatile uint32_t *my_address =
(volatile uint32_t *)MY_ADDRESS;
*my_address = 0x1234;
What is the safest way of writing to memory? I've read that it should be put behind a macro and/or I should check that the pointer is not null.
Thanks
c embedded
|
show 6 more comments
I'm writing to a register with a known address.
volatile uint32_t *my_address =
(volatile uint32_t *)MY_ADDRESS;
*my_address = 0x1234;
What is the safest way of writing to memory? I've read that it should be put behind a macro and/or I should check that the pointer is not null.
Thanks
c embedded
2
Looks safe enough to me as long as there is no MMU in the way...
– Eugene Sh.
Mar 7 at 20:11
1
What type of register?
– Jay Elston
Mar 7 at 20:11
1
What you have right there is "non-safely derived pointer". It is implementation-defined if using non-safely derived pointers is allowed in the program. Since my gut feeling is you are writing for some sort of the embedded platform, you should rest assured that all compilers for those platforms allow non-safely derived pointers.
– SergeyA
Mar 7 at 20:13
@SergeyA Never heard this term even though I can guess its meaning.. Any formal reference/definition?
– Eugene Sh.
Mar 7 at 20:15
@EugeneSh. I am aware of C++ definition of it: eel.is/c++draft/basic.stc.dynamic.safety I am not actually sure that it is available in C (I didn't really notice the C tag)
– SergeyA
Mar 7 at 20:20
|
show 6 more comments
I'm writing to a register with a known address.
volatile uint32_t *my_address =
(volatile uint32_t *)MY_ADDRESS;
*my_address = 0x1234;
What is the safest way of writing to memory? I've read that it should be put behind a macro and/or I should check that the pointer is not null.
Thanks
c embedded
I'm writing to a register with a known address.
volatile uint32_t *my_address =
(volatile uint32_t *)MY_ADDRESS;
*my_address = 0x1234;
What is the safest way of writing to memory? I've read that it should be put behind a macro and/or I should check that the pointer is not null.
Thanks
c embedded
c embedded
edited Mar 7 at 20:11
John Smith
asked Mar 7 at 20:08
John SmithJohn Smith
659
659
2
Looks safe enough to me as long as there is no MMU in the way...
– Eugene Sh.
Mar 7 at 20:11
1
What type of register?
– Jay Elston
Mar 7 at 20:11
1
What you have right there is "non-safely derived pointer". It is implementation-defined if using non-safely derived pointers is allowed in the program. Since my gut feeling is you are writing for some sort of the embedded platform, you should rest assured that all compilers for those platforms allow non-safely derived pointers.
– SergeyA
Mar 7 at 20:13
@SergeyA Never heard this term even though I can guess its meaning.. Any formal reference/definition?
– Eugene Sh.
Mar 7 at 20:15
@EugeneSh. I am aware of C++ definition of it: eel.is/c++draft/basic.stc.dynamic.safety I am not actually sure that it is available in C (I didn't really notice the C tag)
– SergeyA
Mar 7 at 20:20
|
show 6 more comments
2
Looks safe enough to me as long as there is no MMU in the way...
– Eugene Sh.
Mar 7 at 20:11
1
What type of register?
– Jay Elston
Mar 7 at 20:11
1
What you have right there is "non-safely derived pointer". It is implementation-defined if using non-safely derived pointers is allowed in the program. Since my gut feeling is you are writing for some sort of the embedded platform, you should rest assured that all compilers for those platforms allow non-safely derived pointers.
– SergeyA
Mar 7 at 20:13
@SergeyA Never heard this term even though I can guess its meaning.. Any formal reference/definition?
– Eugene Sh.
Mar 7 at 20:15
@EugeneSh. I am aware of C++ definition of it: eel.is/c++draft/basic.stc.dynamic.safety I am not actually sure that it is available in C (I didn't really notice the C tag)
– SergeyA
Mar 7 at 20:20
2
2
Looks safe enough to me as long as there is no MMU in the way...
– Eugene Sh.
Mar 7 at 20:11
Looks safe enough to me as long as there is no MMU in the way...
– Eugene Sh.
Mar 7 at 20:11
1
1
What type of register?
– Jay Elston
Mar 7 at 20:11
What type of register?
– Jay Elston
Mar 7 at 20:11
1
1
What you have right there is "non-safely derived pointer". It is implementation-defined if using non-safely derived pointers is allowed in the program. Since my gut feeling is you are writing for some sort of the embedded platform, you should rest assured that all compilers for those platforms allow non-safely derived pointers.
– SergeyA
Mar 7 at 20:13
What you have right there is "non-safely derived pointer". It is implementation-defined if using non-safely derived pointers is allowed in the program. Since my gut feeling is you are writing for some sort of the embedded platform, you should rest assured that all compilers for those platforms allow non-safely derived pointers.
– SergeyA
Mar 7 at 20:13
@SergeyA Never heard this term even though I can guess its meaning.. Any formal reference/definition?
– Eugene Sh.
Mar 7 at 20:15
@SergeyA Never heard this term even though I can guess its meaning.. Any formal reference/definition?
– Eugene Sh.
Mar 7 at 20:15
@EugeneSh. I am aware of C++ definition of it: eel.is/c++draft/basic.stc.dynamic.safety I am not actually sure that it is available in C (I didn't really notice the C tag)
– SergeyA
Mar 7 at 20:20
@EugeneSh. I am aware of C++ definition of it: eel.is/c++draft/basic.stc.dynamic.safety I am not actually sure that it is available in C (I didn't really notice the C tag)
– SergeyA
Mar 7 at 20:20
|
show 6 more comments
1 Answer
1
active
oldest
votes
If you want to safely write/read memory-mapped, you could use an already existing piece of code designed for this very purpose, for example ARM's mmio.h header file.
The resulting code equivalent to the one in your example would then be:
#include<mmio.h>
// ...
mmio_write_32(MY_ADDRESS, 0x1234);
Because the functions included in the header are static inline, there will be no performance penalty caused by a call to a subroutine - the code will just be inserted where it is being called in your program.
Since this header file is being widely used, you can reasonably stop worrying on your register read/write operations by using it.
And because it is licensed under the BSD 3-Clause, you are free to use it in both your commercial and pet projects .
But that is not necessarily safe - we don't know if the hardware permits word access there, if the address is aligned or the hardware permits it not to be, and we don't know if anything else like an ISR might see a non-atomic write half-done. It's also not meaningfully different from the code in the question - so what you are really doing is making a deference to authority over something your authority doesn't even claim to provide.
– Chris Stratton
Mar 8 at 3:59
First of all, I am not making a deference to any authority of any kind. I used the word 'safe' here in the sense that using mmio.h should protect against a misuse of the volatile keyword, I should have elaborated a bit: since the example code looked more like a beginner's code than the one you would probably have written yourself, I (wrongly?) assumed the question was more a basic one than the one you apparently thought it was - there was no information whatsoever on the actual target. John Smith could probably tell more precisely what he was looking for.
– Frant
Mar 8 at 14:24
I 100% do agree with your comment's content, but for the somewhat rude part.
– Frant
Mar 8 at 14:27
...and the ' I've read that it should be put behind a macro' is what guided my answer.
– Frant
Mar 8 at 14:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f55052026%2fsafely-writing-directly-to-a-memory-address%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to safely write/read memory-mapped, you could use an already existing piece of code designed for this very purpose, for example ARM's mmio.h header file.
The resulting code equivalent to the one in your example would then be:
#include<mmio.h>
// ...
mmio_write_32(MY_ADDRESS, 0x1234);
Because the functions included in the header are static inline, there will be no performance penalty caused by a call to a subroutine - the code will just be inserted where it is being called in your program.
Since this header file is being widely used, you can reasonably stop worrying on your register read/write operations by using it.
And because it is licensed under the BSD 3-Clause, you are free to use it in both your commercial and pet projects .
But that is not necessarily safe - we don't know if the hardware permits word access there, if the address is aligned or the hardware permits it not to be, and we don't know if anything else like an ISR might see a non-atomic write half-done. It's also not meaningfully different from the code in the question - so what you are really doing is making a deference to authority over something your authority doesn't even claim to provide.
– Chris Stratton
Mar 8 at 3:59
First of all, I am not making a deference to any authority of any kind. I used the word 'safe' here in the sense that using mmio.h should protect against a misuse of the volatile keyword, I should have elaborated a bit: since the example code looked more like a beginner's code than the one you would probably have written yourself, I (wrongly?) assumed the question was more a basic one than the one you apparently thought it was - there was no information whatsoever on the actual target. John Smith could probably tell more precisely what he was looking for.
– Frant
Mar 8 at 14:24
I 100% do agree with your comment's content, but for the somewhat rude part.
– Frant
Mar 8 at 14:27
...and the ' I've read that it should be put behind a macro' is what guided my answer.
– Frant
Mar 8 at 14:29
add a comment |
If you want to safely write/read memory-mapped, you could use an already existing piece of code designed for this very purpose, for example ARM's mmio.h header file.
The resulting code equivalent to the one in your example would then be:
#include<mmio.h>
// ...
mmio_write_32(MY_ADDRESS, 0x1234);
Because the functions included in the header are static inline, there will be no performance penalty caused by a call to a subroutine - the code will just be inserted where it is being called in your program.
Since this header file is being widely used, you can reasonably stop worrying on your register read/write operations by using it.
And because it is licensed under the BSD 3-Clause, you are free to use it in both your commercial and pet projects .
But that is not necessarily safe - we don't know if the hardware permits word access there, if the address is aligned or the hardware permits it not to be, and we don't know if anything else like an ISR might see a non-atomic write half-done. It's also not meaningfully different from the code in the question - so what you are really doing is making a deference to authority over something your authority doesn't even claim to provide.
– Chris Stratton
Mar 8 at 3:59
First of all, I am not making a deference to any authority of any kind. I used the word 'safe' here in the sense that using mmio.h should protect against a misuse of the volatile keyword, I should have elaborated a bit: since the example code looked more like a beginner's code than the one you would probably have written yourself, I (wrongly?) assumed the question was more a basic one than the one you apparently thought it was - there was no information whatsoever on the actual target. John Smith could probably tell more precisely what he was looking for.
– Frant
Mar 8 at 14:24
I 100% do agree with your comment's content, but for the somewhat rude part.
– Frant
Mar 8 at 14:27
...and the ' I've read that it should be put behind a macro' is what guided my answer.
– Frant
Mar 8 at 14:29
add a comment |
If you want to safely write/read memory-mapped, you could use an already existing piece of code designed for this very purpose, for example ARM's mmio.h header file.
The resulting code equivalent to the one in your example would then be:
#include<mmio.h>
// ...
mmio_write_32(MY_ADDRESS, 0x1234);
Because the functions included in the header are static inline, there will be no performance penalty caused by a call to a subroutine - the code will just be inserted where it is being called in your program.
Since this header file is being widely used, you can reasonably stop worrying on your register read/write operations by using it.
And because it is licensed under the BSD 3-Clause, you are free to use it in both your commercial and pet projects .
If you want to safely write/read memory-mapped, you could use an already existing piece of code designed for this very purpose, for example ARM's mmio.h header file.
The resulting code equivalent to the one in your example would then be:
#include<mmio.h>
// ...
mmio_write_32(MY_ADDRESS, 0x1234);
Because the functions included in the header are static inline, there will be no performance penalty caused by a call to a subroutine - the code will just be inserted where it is being called in your program.
Since this header file is being widely used, you can reasonably stop worrying on your register read/write operations by using it.
And because it is licensed under the BSD 3-Clause, you are free to use it in both your commercial and pet projects .
edited Mar 8 at 2:11
answered Mar 8 at 0:45
FrantFrant
84259
84259
But that is not necessarily safe - we don't know if the hardware permits word access there, if the address is aligned or the hardware permits it not to be, and we don't know if anything else like an ISR might see a non-atomic write half-done. It's also not meaningfully different from the code in the question - so what you are really doing is making a deference to authority over something your authority doesn't even claim to provide.
– Chris Stratton
Mar 8 at 3:59
First of all, I am not making a deference to any authority of any kind. I used the word 'safe' here in the sense that using mmio.h should protect against a misuse of the volatile keyword, I should have elaborated a bit: since the example code looked more like a beginner's code than the one you would probably have written yourself, I (wrongly?) assumed the question was more a basic one than the one you apparently thought it was - there was no information whatsoever on the actual target. John Smith could probably tell more precisely what he was looking for.
– Frant
Mar 8 at 14:24
I 100% do agree with your comment's content, but for the somewhat rude part.
– Frant
Mar 8 at 14:27
...and the ' I've read that it should be put behind a macro' is what guided my answer.
– Frant
Mar 8 at 14:29
add a comment |
But that is not necessarily safe - we don't know if the hardware permits word access there, if the address is aligned or the hardware permits it not to be, and we don't know if anything else like an ISR might see a non-atomic write half-done. It's also not meaningfully different from the code in the question - so what you are really doing is making a deference to authority over something your authority doesn't even claim to provide.
– Chris Stratton
Mar 8 at 3:59
First of all, I am not making a deference to any authority of any kind. I used the word 'safe' here in the sense that using mmio.h should protect against a misuse of the volatile keyword, I should have elaborated a bit: since the example code looked more like a beginner's code than the one you would probably have written yourself, I (wrongly?) assumed the question was more a basic one than the one you apparently thought it was - there was no information whatsoever on the actual target. John Smith could probably tell more precisely what he was looking for.
– Frant
Mar 8 at 14:24
I 100% do agree with your comment's content, but for the somewhat rude part.
– Frant
Mar 8 at 14:27
...and the ' I've read that it should be put behind a macro' is what guided my answer.
– Frant
Mar 8 at 14:29
But that is not necessarily safe - we don't know if the hardware permits word access there, if the address is aligned or the hardware permits it not to be, and we don't know if anything else like an ISR might see a non-atomic write half-done. It's also not meaningfully different from the code in the question - so what you are really doing is making a deference to authority over something your authority doesn't even claim to provide.
– Chris Stratton
Mar 8 at 3:59
But that is not necessarily safe - we don't know if the hardware permits word access there, if the address is aligned or the hardware permits it not to be, and we don't know if anything else like an ISR might see a non-atomic write half-done. It's also not meaningfully different from the code in the question - so what you are really doing is making a deference to authority over something your authority doesn't even claim to provide.
– Chris Stratton
Mar 8 at 3:59
First of all, I am not making a deference to any authority of any kind. I used the word 'safe' here in the sense that using mmio.h should protect against a misuse of the volatile keyword, I should have elaborated a bit: since the example code looked more like a beginner's code than the one you would probably have written yourself, I (wrongly?) assumed the question was more a basic one than the one you apparently thought it was - there was no information whatsoever on the actual target. John Smith could probably tell more precisely what he was looking for.
– Frant
Mar 8 at 14:24
First of all, I am not making a deference to any authority of any kind. I used the word 'safe' here in the sense that using mmio.h should protect against a misuse of the volatile keyword, I should have elaborated a bit: since the example code looked more like a beginner's code than the one you would probably have written yourself, I (wrongly?) assumed the question was more a basic one than the one you apparently thought it was - there was no information whatsoever on the actual target. John Smith could probably tell more precisely what he was looking for.
– Frant
Mar 8 at 14:24
I 100% do agree with your comment's content, but for the somewhat rude part.
– Frant
Mar 8 at 14:27
I 100% do agree with your comment's content, but for the somewhat rude part.
– Frant
Mar 8 at 14:27
...and the ' I've read that it should be put behind a macro' is what guided my answer.
– Frant
Mar 8 at 14:29
...and the ' I've read that it should be put behind a macro' is what guided my answer.
– Frant
Mar 8 at 14:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f55052026%2fsafely-writing-directly-to-a-memory-address%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
2
Looks safe enough to me as long as there is no MMU in the way...
– Eugene Sh.
Mar 7 at 20:11
1
What type of register?
– Jay Elston
Mar 7 at 20:11
1
What you have right there is "non-safely derived pointer". It is implementation-defined if using non-safely derived pointers is allowed in the program. Since my gut feeling is you are writing for some sort of the embedded platform, you should rest assured that all compilers for those platforms allow non-safely derived pointers.
– SergeyA
Mar 7 at 20:13
@SergeyA Never heard this term even though I can guess its meaning.. Any formal reference/definition?
– Eugene Sh.
Mar 7 at 20:15
@EugeneSh. I am aware of C++ definition of it: eel.is/c++draft/basic.stc.dynamic.safety I am not actually sure that it is available in C (I didn't really notice the C tag)
– SergeyA
Mar 7 at 20:20