ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 98
Rep:
Explain C++ function
Please explain the following piece of C++ to me
void cSkymax::SetMode(char newmode)
{
m.lock();
if (mode && newmode != mode)
ups_status_changed = true;
mode = newmode;
// printf("%s %d %s %s\n", __FILE__, __LINE__, mode, newmode);
m.unlock();
}
both mode and newmode are defined as char but I am failing to see how mode && newmode can be compared to a char. Surely && returns TRUE or FALSE not a char.
I am also assuming that m.lock and m.unlock are stopping memory swopping.
The reason for my question is that I require to do this function in C not C++ and I know very little about C++
char is an 8 bit integer. char can be used to handle characters or to handle 8 bit integer opearations. Note that char can be signed or not depending on the compiler.
bool is also an 8 bit integer, where 0 is false and ther values are considered true.
The result of the && operator has type bool. The comparison is then between two 8 bit integers.
both mode and newmode are defined as char but I am failing to see how mode && newmode can be compared to a char. Surely && returns TRUE or FALSE not a char.
I am also assuming that m.lock and m.unlock are stopping memory swopping.
The reason for my question is that I require to do this function in C not C++ and I know very little about C++
This is legal C code, too.
An if statement, without any specific comparison, decides if its fulfilled if the expression inside its parenthesis resolve to true/not zero.
So 'mode' on its own is simply a check like mode != 0.
This is then followed by && which, of course, is kind of an if test on its own, if the thing before it is true.
So, if mode is not zero, then the && goes forward to the next check, which tests if the new mode has a different value than mode.
Of course, if you make sure that every skymax object gets initialized with mode as zero, and if every new mode is nonzero, then you could simply test against the new mode being different than the old one.
Quote:
Originally Posted by stockton
By the way GCC 8.3.0 on Debian tells me
Code:
warning: comparison between pointer and integer
if (mode && newmode != mode)
^~
pointing to the !=
Then cSkymax::mode is probably a pointer, and not a simple character, making this ...not a good idea since mode would be replaced by 'newmode' if mode is nonzero and different to newmode.
Either change the mode field of cSkymax to a character, as well, or change the new mode to a pointer, a pointer that would point to a new/different mode, I suppose.
Anyway, this seems like a troublesome setup.
both mode and newmode are defined as char but I am failing to see how mode && newmode can be compared to a char. Surely && returns TRUE or FALSE not a char.
I think you're misunderstanding operator precedence and how && is evaluated.
First, operator precedence. It's:
Code:
if (mode && (newmode != mode))
Not:
Code:
if ((mode && newmode) != mode)
Second, short-circuit evaluation:
First, "mode" is evaluated. If it evaluates to true, then "newMode != mode" is evaluated. If that also evaluates to true, then the expression evaluates to true. Otherwise, it evaluates to false.
if (mode != 0) //if cSkymax::mode is not equal to zero
{
if (newmode != mode) //and (&&) if cSkymax::mode is not equal to newmode
{
do_something(); // then do something
}
//else do nothing, in other words, both checks must succeed to 'do_something();'
}
I think the best and most performant code is the code that the programmers can the most easily read, understand and work with.
Computers are quite beastly these days, and if you're writing for a platform that is like...from the past 20 years or so then you should be fine just writing legible stuff, even if you're using old versions of compilers and whatnot from back then.
No need to write brain wracking crypto obfuscated hackariffic guruesque code unless you're one of the elites that do it for important purposes or if you're that much of an enthusiast that you want to be elite like that.
...but I wager to say that the people who make it on that journey tend to have an easy time with the easy stuff, too.
So, yeah, don't make it harder on yourself than you have to, even if you're writing for some microcontroller for your own project.
You can always optimize later, don't set up your project like an entry to the IOCCC from the get go.
Edit: While IOCCC is a bit of an extreme example, I really mean all needlessly brain time eating code.
Name your variables descriptively, even if they get a bit longer (of course having a 80 character long variable name would be a tad silly but if it worked for you? Do that too.)
Break your code up into smaller pieces, don't go completely crazy to just avoid some code duplication, but the more you can encapsulate and reuse, the better.
Another great type of code is the code you can remove from the codebase, after all.
The less code, the less places to screw up :P
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.