LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-16-2020, 03:50 AM   #1
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 98

Rep: Reputation: 1
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++
 
Old 01-16-2020, 04:04 AM   #2
wainamoinen
Member
 
Registered: Sep 2009
Location: Belgium
Distribution: Slackware
Posts: 30

Rep: Reputation: 22
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.
 
Old 01-16-2020, 05:50 AM   #3
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 98

Original Poster
Rep: Reputation: 1
Thank you but surely there is a better way of doing this? Preferably more understandable.
 
Old 01-16-2020, 05:59 AM   #4
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 98

Original Poster
Rep: Reputation: 1
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 !=

Last edited by stockton; 01-16-2020 at 06:01 AM.
 
Old 01-16-2020, 06:11 AM   #5
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 442

Rep: Reputation: 196Reputation: 196
Quote:
Originally Posted by stockton View Post
Please explain the following piece of C++ to me
Code:
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++
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.

Rewritten (in a C like manner) it would be:
Code:
void cSkymax_SetMode(cSkymax * skymax, char newmode)
{
  skymax->m.lock();
  if (skymax->mode != 0 && skymax->mode != newmode){
    skymax->ups_status_changed = 1;
  }
  skymax->mode = newmode;
  skymax->m.unlock();
}
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 View Post
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.

Last edited by Geist; 01-16-2020 at 06:17 AM.
 
1 members found this post helpful.
Old 01-16-2020, 08:20 AM   #6
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by stockton View Post
Please explain the following piece of C++ to me

I am also assuming that m.lock and m.unlock are stopping memory swopping.
It's likely that m is a std::mutex. You can look to pthreads for this in C (man pthread_mutex_lock).
 
2 members found this post helpful.
Old 01-17-2020, 03:27 AM   #7
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 98

Original Poster
Rep: Reputation: 1
Geist, thank you for your explanation however neither mode nor newmode are pointers. Both are defined as char.
 
Old 01-17-2020, 07:52 AM   #8
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 442

Rep: Reputation: 196Reputation: 196
Quote:
Originally Posted by stockton View Post
neither mode nor newmode are pointers. Both are defined as char.
Well, then...uh...then I guess the compiler is a liar, or something equally strange.
 
Old 01-17-2020, 08:49 AM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,134

Rep: Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283
Quote:
Originally Posted by stockton View Post
Code:
if (mode && newmode != mode)
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.

Last edited by dugan; 01-17-2020 at 11:28 AM.
 
1 members found this post helpful.
Old 01-19-2020, 02:26 AM   #10
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 98

Original Poster
Rep: Reputation: 1
Dugan please tell me how
Code:
if (newmode != mode)
is different to
Code:
if (mode && newmode != mode)
 
Old 01-19-2020, 02:44 AM   #11
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 442

Rep: Reputation: 196Reputation: 196
Code:
if (newmode != mode)
If newmode is not equal to mode.


Code:
if (mode && newmode != mode)
Can be expanded to:
Code:
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();'
}

Last edited by Geist; 01-19-2020 at 02:54 AM.
 
Old 01-19-2020, 01:46 PM   #12
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,134

Rep: Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283
Not really sure what else I can say, but I'll try...

Code:
if (mode && newmode != mode)
is equivalent to:

Code:
if ((mode != 0) && (newmode != mode))
Geist's explanation above mine is also correct.

Last edited by dugan; 01-19-2020 at 02:32 PM.
 
Old 01-22-2020, 05:50 PM   #13
vmelkon
Member
 
Registered: Feb 2007
Location: Canada
Distribution: Kubuntu 22.04
Posts: 535

Rep: Reputation: 83
I think that
if (mode && newmode != mode)
is better than
if ((mode != 0) && (newmode != mode))

on the x86 platform. There is a special instruction that is used when you do if(mode).
Anyway, perhaps performance is not important in your case.
 
Old 01-22-2020, 05:51 PM   #14
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,134

Rep: Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283Reputation: 5283
What makes you think the compiler won't optimize both to the exact same object code?
 
Old 01-22-2020, 08:33 PM   #15
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 442

Rep: Reputation: 196Reputation: 196
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

Last edited by Geist; 01-22-2020 at 08:36 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 06:00 PM
Can anyone please explain about "Function call interrupts" entry in /proc/interrupts? cyclops.xmen Linux - Software 2 12-09-2009 01:13 PM
can someone explain the 'for' function in python to me deathalele Programming 8 10-16-2008 12:10 PM
Help me ... explain parameters of cacheflush() function minhstone Red Hat 2 01-22-2008 10:41 PM
explain function Filipe Programming 1 03-01-2007 07:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration