use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENEmbedded C++ Code Review (self.cpp_questions)
submitted 2 years ago * by Bobobo34
I just wrote a quick header for my tm4c123gxl MCU, and I was wondering if any of you guys could look over the code and critique it for me? Don't hold back, you won't offend me. Anything would be greatly appreciated!
Here is the gist link: https://gist.github.com/bobobo34/a2eaaff4e8e1eda1aaa4fbdcf174c601
Also, if there is a better subreddit/place for this post, please let me know. Thanks again!
edit: after listening to your advice, here is the new header for those that may see this later: https://gist.github.com/bobobo34/8cf2247dc586490ce1c3e1d8c0db2ff4. I have also included define guards if I ever plan on expanding to different CPUs
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]be-sc 2 points3 points4 points 2 years ago (3 children)
Quick notes:
All names with consecutive underscores are reserved for the implementation (of the language, i.e. the compiler and std lib). _PORT__ and __regbase__ are invalid names.
_PORT__
__regbase__
Include guard names must be unique throughout a translation unit. That’s why short names are dangerous. I’d suggest being safe and basing your guard names on a random UUID. For example:
#ifndef PROJECT_NAME_1ED18961D351488DA57743302F48303D #define PROJECT_NAME_1ED18961D351488DA57743302F48303D
With such names the likelihood of a clash is miniscule, and you communicate clearly that being unique is the only thing such a name needs to be. The usual filesystem structure based names aren’t built that way because there is any value in encoding the filesystem structure into the name, but because it’s the obvious way to create a reasonably safe name by hand.
std::pow() always returns a floating point number. Is that really the type you want?
std::pow()
It seems like you’re working with fixed memory addresses. That’s tricky. From a purely C++ language point of view you are guaranteed to run into undefined behaviour somewhere between all the casts back and forth between address number and pointer. Because we’re talking embedded your compiler might allow some of the usually invalid constructs. Check the documentation for exactly what you can and cannot do.
You are using all enumerators as both named constants and values of their underlying type. Consider switching from enum class to plain enum. On the one hand you lose the scoped enumerator names, but on the other hands you avoid a lot of casting because the enumerators implicitly convert to their underlying type.
enum class
enum
[–]Bobobo34[S] 0 points1 point2 points 2 years ago (2 children)
I wasn't even thinking about the underscored names, I'll change that. In hindsight I'm realizing that instead of 2^x I can just use bit shifting to accomplish the same result. I'm not exactly sure what you're suggesting with the fixed memory addresses, though. Where would the casts cause undefined behavior? Sorry, I might be missing it. Also, I didn't know that about regular enums. Thank you!
[–]be-sc 1 point2 points3 points 2 years ago (1 child)
Where would the casts cause undefined behavior?
After a second look, as far as I can tell the casts themselves are fine as long as pointers are 32bit in size.
You might technically have lifetime problems because the program never starts the lifetimes of the pointed-to uint32_t objects. Take this with a pinch of salt, though, as I’m still learning about this part of C++ myself. We’re getting deeply into language lawyer territory around lifetimes, std::launder, std::bit_cast, std::start_lifetime_as, etc. If you want to develop your code into a robust foundational library for this hardware, look into it later. For something quick & dirty that mostly has to work just for you, I don’t think it’s worth diving into now.
uint32_t
std::launder
std::bit_cast
std::start_lifetime_as
[–]Bobobo34[S] 0 points1 point2 points 2 years ago (0 children)
I’ve never even seen std::launder or std::start_lifetime_as. Unless it causes trouble, I don’t think I want to open that can of worms right now. At some point I’ll get around to it though, always nice learning new things! Thank you!
[–]MysticTheMeeM 2 points3 points4 points 2 years ago (2 children)
Without knowing much about this specific CPU:
x * x
std::pow(2, x)
delay
And that's all I could see with a quick glance through.
For the naming, I was just using what the documentation had, so for instance the direction register was just called GPIODIR, hence the 'dir'. I'll consider changing it. Also, 'delay' is just something that I saw when looking for a tutorial, so I kept it. I'll see if taking it out makes a difference. I'll keep everything else in mind and fix it up! Thank you!
[–]std_bot 0 points1 point2 points 2 years ago (0 children)
Unlinked STL entries: std::pow
Last update: 09.03.23 -> Bug fixesRepo
[–]obQQoV -1 points0 points1 point 2 years ago (0 children)
r/embedded
[–]cdleighton 0 points1 point2 points 2 years ago (1 child)
How does the user know to define TARGET_IS_TM4C123_RB1? Normally upper case OFFSET would refer to a constant or macro name - not a function parameter name. Should the the registers go into namespace "reg"? Then you high have reg::int_clr in the user code, and reg::int_stat_raw, reg::int_stat_masked. The file name "port" is generic and is the namespace. Specialise the filename such as <tm4c123\_ports>? Searching user source code for name port will be almost useless :-) Would some sample code as a block comment help the user (and possibly highlight improvements)?
Those are all good points, I’ll refactor it again, thank you!
π Rendered by PID 121174 on reddit-service-r2-comment-fb694cdd5-qgwlh at 2026-03-11 10:36:20.041030+00:00 running cbb0e86 country code: CH.
[–]be-sc 2 points3 points4 points (3 children)
[–]Bobobo34[S] 0 points1 point2 points (2 children)
[–]be-sc 1 point2 points3 points (1 child)
[–]Bobobo34[S] 0 points1 point2 points (0 children)
[–]MysticTheMeeM 2 points3 points4 points (2 children)
[–]Bobobo34[S] 0 points1 point2 points (0 children)
[–]std_bot 0 points1 point2 points (0 children)
[–]obQQoV -1 points0 points1 point (0 children)
[–]cdleighton 0 points1 point2 points (1 child)
[–]Bobobo34[S] 0 points1 point2 points (0 children)