473,563 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about the standard or maybe just good practice?

Hi,

I have some small questions that have never been any problems, (for my
compiler?), but always make me curious.
So here goes...

what does the standard sday about the 'if' statement?

for example
if i have,
/////////////////////////
const int aa = 1;
const int ab = 2;
const int ac = 4;

int someval = aa | ab | ac;

if( someval & ab )
{
// do this
}

but in the case above "someval & ab" is not true, the value is simply not
equal to 0.
Does the standard say that i am right or should i have written?

if( someval & ab != 0 )
{
// do this
}

Is my compiler wrong in accepting this?

My second question, is, if i have a structure
typedef struct
{
char a
int b
}MYSTRUCT *PMYSTRUCT

and i Read(..) a file using sizeof( MYSTRUCT ) will it make a direference if
i had declared my structure as
typedef struct
{
int b
char a
}MYSTRUCT *PMYSTRUCT

and what about a structure like
typedef struct
{
int b
char a
ANOTHERSTRUCT * B
}MYSTRUCT *PMYSTRUCT

how can i get 'sizeof' when ANOTHERSTRUCT is only a pointer? where do i get
'ANOTHERSTRUCT ' size?
where is it stored, in another segment?

Thanks for your inputs.

Sims


Jul 19 '05 #1
8 2080
"Sims" <si*********@ho tmail.com> wrote in message
news:bh******** ****@ID-162430.news.uni-berlin.de...
Hi,

I have some small questions that have never been any problems, (for my
compiler?), but always make me curious.
So here goes...

what does the standard sday about the 'if' statement?

for example
if i have,
/////////////////////////
const int aa = 1;
const int ab = 2;
const int ac = 4;

int someval = aa | ab | ac;

if( someval & ab )
{
// do this
}

but in the case above "someval & ab" is not true, the value is simply not
equal to 0.
Yes, it is not 0. Converted to bool, you get true.
Does the standard say that i am right or should i have written?

if( someval & ab != 0 )
{
// do this
}

Is my compiler wrong in accepting this?
No, but it does not do what you (most probably) intended. 'ab != 0' is
first evaluated, then the result is and'd with someval. The result of that
is converted to bool. Write it as:

if( (someval & ab) != 0 )

and you have the same as the first example.
My second question, is, if i have a structure
typedef struct
{
char a
int b
}MYSTRUCT *PMYSTRUCT
Other than you forgot all ";", I would recommend writing it like this:

struct MYSRUCT
{
char a;
int b;
};
and i Read(..) a file using sizeof( MYSTRUCT ) will it make a direference if i had declared my structure as
typedef struct
{
int b
char a
}MYSTRUCT *PMYSTRUCT
Yes. You switched the variables and will probably get garbage.
and what about a structure like
typedef struct
{
int b
char a
ANOTHERSTRUCT * B
}MYSTRUCT *PMYSTRUCT

how can i get 'sizeof' when ANOTHERSTRUCT is only a pointer?
sizeof is a compile time operator. It returns the size of what a
type/variable will occupy in memory, including all padding bits and bytes.
If you want to know the overall size of the structure (exluding padding
bytes), create a member function 'size' which returns the size:

return sizeof (a) + sizeof (b) + sizeof (*B);

Or, if you want the real amount of memory that the structure takes,
including the memory pointed by, return this:

return sizeof (*this) + sizeof (*B);
where do i get 'ANOTHERSTRUCT ' size?
sizeof (ANOTHERSTRUCT) would do. Or, if that struct contains pointers as
well, you should provide a 'size' function for that structure as well.
where is it stored, in another segment?


I do not think C++ has a concept of segments. Just know that when you
have a pointer it will point to some other part in memory. So in your
example, the MYSTRUCT could be somewhere at the 'beginning' of the memory,
while the ANOTHERSTRUCT pointed to by the member could be in the 'middle'.

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #2
"Sims" <si*********@ho tmail.com> wrote...
I have some small questions that have never been any problems, (for my
compiler?), but always make me curious.
So here goes...

what does the standard sday about the 'if' statement?

for example
if i have,
/////////////////////////
const int aa = 1;
const int ab = 2;
const int ac = 4;

int someval = aa | ab | ac;

if( someval & ab )
{
// do this
}

but in the case above "someval & ab" is not true, the value is simply not
equal to 0.
I am sorry, what? 'someval & ab' extracts only one bit. If that bit
is not zero, you get your 'if'. If the bit _is_ 0, it doesn't matter
what the rest of 'someval' looks like. Isn't that the whole purpose
of testing a bit?
Does the standard say that i am right or should i have written?

if( someval & ab != 0 )
{
// do this
}

Is my compiler wrong in accepting this?
No. The behaviours of

if (integral_expre ssion)

and

if (integral_expre ssion != 0)

are equivalent.
My second question, is, if i have a structure
typedef struct
{
char a
int b
}MYSTRUCT *PMYSTRUCT
Why is this dance around the bush? It would be much more C++ to
write

struct MYSTRUCT
{
char a;
int b;
};

typedef MYSTRUCT *PMYSTRUCT;
and i Read(..) a file using sizeof( MYSTRUCT ) will it make a direference if i had declared my structure as
typedef struct
{
int b
char a
}MYSTRUCT *PMYSTRUCT

and what about a structure like
typedef struct
{
int b
char a
ANOTHERSTRUCT * B
}MYSTRUCT *PMYSTRUCT
Yes, it will.
how can i get 'sizeof' when ANOTHERSTRUCT is only a pointer?
The sizeof(MYSTRUCT ) == sizeof(int) + sizeof(char) + sizeof(pointer) +
some padding.
where do i get
'ANOTHERSTRUCT ' size?
I am not sure I understand the question. sizeof(ANOTHERS TRUCT) is
the way to get its size.
where is it stored, in another segment?


Who knows? And who cares? You have a pointer to it, dereference it
and you'll get the 'ANOTHERSTRUCT' object.

Victor
Jul 19 '05 #3
"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:3f3401f4@s hknews01...
Sims wrote:
const int aa = 1;
const int ab = 2;
const int ac = 4;

int someval = aa | ab | ac;

if( someval & ab )
{
// do this
} if( someval & ab != 0 )
{
// do this
}

[...] So your two examples are equivalent.
Not quite. operator!= will be evaluated before operator& (binary).
However, I
question the wisdom of using the first form and consider the second to
be more clear.


But it only works, if you use parenthesis to change the operator
precedence:

if ( (someval & ab) != 0)
{
// do this
}

Since this is rather ugly, I like sticking to either the first example,
or I write a little inline function, which makes the code much easier to
read:

template <typename T>
inline bool is_bit_set (T lhs, T rhs)
{
return (lhs & rhs) != 0;
}

regards
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #4
"Jakob Bieling" <ne*****@gmy.ne t> wrote...
"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:3f3401f4@s hknews01...
Sims wrote:

const int aa = 1;
const int ab = 2;
const int ac = 4;

int someval = aa | ab | ac;

if( someval & ab )
{
// do this
} if( someval & ab != 0 )
{
// do this
}

[...] So your two examples are equivalent.


Not quite. operator!= will be evaluated before operator& (binary).


You're absolutely right! I made the same mistake. Shame on me!
Jul 19 '05 #5
>
Yes, it is not 0. Converted to bool, you get true.
So to anwer my question you are saying that "Anything but 0 is true".
Is that C++ standrd behaviour?
Does the standard say that i am right or should i have written?

if( someval & ab != 0 )
{
// do this
}

Is my compiler wrong in accepting this?
No, but it does not do what you (most probably) intended. 'ab != 0' is
first evaluated, then the result is and'd with someval. The result of that
is converted to bool. Write it as:

if( (someval & ab) != 0 )

and you have the same as the first example.
My second question, is, if i have a structure
typedef struct
{
char a
int b
}MYSTRUCT *PMYSTRUCT


Other than you forgot all ";", I would recommend writing it like this:

struct MYSRUCT
{
char a;
int b;
};


Ok, does it make any diference?(good practice or standard?)
and i Read(..) a file using sizeof( MYSTRUCT ) will it make a
direference if
i had declared my structure as
typedef struct
{
int b
char a
}MYSTRUCT *PMYSTRUCT
Yes. You switched the variables and will probably get garbage.


So you are saying that if i read a stucture it will be filled in in the
order i declared it?
and what about a structure like
typedef struct
{
int b
char a
ANOTHERSTRUCT * B
}MYSTRUCT *PMYSTRUCT

how can i get 'sizeof' when ANOTHERSTRUCT is only a pointer?
sizeof is a compile time operator. It returns the size of what a
type/variable will occupy in memory, including all padding bits and bytes.
If you want to know the overall size of the structure (exluding padding
bytes), create a member function 'size' which returns the size:

return sizeof (a) + sizeof (b) + sizeof (*B);

Or, if you want the real amount of memory that the structure takes,
including the memory pointed by, return this:

return sizeof (*this) + sizeof (*B);
where do i get 'ANOTHERSTRUCT ' size?


sizeof (ANOTHERSTRUCT) would do. Or, if that struct contains pointers

as well, you should provide a 'size' function for that structure as well.
where is it stored, in another segment?
I do not think C++ has a concept of segments. Just know that when you

...segment was the wrong word to use, i am sorry, i am not english.

hth

hth.

Sims
Jul 19 '05 #6
[...] So your two examples are equivalent.


Not quite. operator!= will be evaluated before operator& (binary).


You're absolutely right! I made the same mistake. Shame on me!


Same mistake as who?
I seem to be missing a post or two. The reply does not match my OP.
Or am I wrong here as well.

Regards.
Sims
Jul 19 '05 #7
>
I am sorry, what? 'someval & ab' extracts only one bit. If that bit
is not zero, you get your 'if'. If the bit _is_ 0, it doesn't matter
what the rest of 'someval' looks like. Isn't that the whole purpose
of testing a bit?
I am soryy...
i meant what does the standard say about 'if'
....
is
if(1)
{
do this1}

if(0)
{
do this2 }

if(33)
{
do this3 }

does the standard say that anything exect 0 is true.
Does the standard say that i am right or should i have written?

if( someval & ab != 0 )
{
// do this
}

Is my compiler wrong in accepting this?
No. The behaviours of

if (integral_expre ssion)

and

if (integral_expre ssion != 0)


I am sorry wh..
are you saying that "someval & ab" will return TRUE or FALSE, (1 or 0 )?
My second question, is, if i have a structure
typedef struct
{
char a
int b
}MYSTRUCT *PMYSTRUCT
Why is this dance around the bush? It would be much more C++ to
write

struct MYSTRUCT
{
char a;
int b;
};

typedef MYSTRUCT *PMYSTRUCT;


Ok.
and i Read(..) a file using sizeof( MYSTRUCT ) will it make a
direference if
i had declared my structure as
typedef struct
{
int b
char a
}MYSTRUCT *PMYSTRUCT

and what about a structure like
typedef struct
{
int b
char a
ANOTHERSTRUCT * B
}MYSTRUCT *PMYSTRUCT


Yes, it will.


Ok. I see. It explains it all now, thanks.

Jul 19 '05 #8
"Sims" <si*********@ho tmail.com> wrote...

I am sorry, what? 'someval & ab' extracts only one bit. If that bit
is not zero, you get your 'if'. If the bit _is_ 0, it doesn't matter
what the rest of 'someval' looks like. Isn't that the whole purpose
of testing a bit?
I am soryy...
i meant what does the standard say about 'if'
...
is
if(1)
{
do this1}

if(0)
{
do this2 }

if(33)
{
do this3 }

does the standard say that anything exect 0 is true.


Yes, it does. Sorry I misunderstood.
Does the standard say that i am right or should i have written?

if( someval & ab != 0 )
{
// do this
}

Is my compiler wrong in accepting this?


No. The behaviours of

if (integral_expre ssion)

and

if (integral_expre ssion != 0)


I am sorry wh..
are you saying that "someval & ab" will return TRUE or FALSE, (1 or 0 )?


I am saying that (someval & ab) will return an integral value either
equal 0 or not. If the result of that bitwise AND is 0, converted to
bool (inside the 'if' parentheses) you get 'false'. If the result of
that bitwise AND is non-zero, you get 'true'. And in C++ there are
no "TRUE" or "FALSE", those are Microsoft tricks.

Yes, converted back to int, 'true' gives 1 and 'false' gives 0. But
that's not the point, right?

Victor
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2227
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and the implementation of its member functions in foo.cpp (which obviously #inludes foo.h) and further assume that it depends on a class bar which is...
55
4623
by: Steve Jorgensen | last post by:
In a recent thread, RKC (correctly, I believe), took issue with my use of multiple parameters in a Property Let procedure to pass dimensional arguments on the basis that, although it works, it's not obvious how the code works if you don't know the intricacies of the Property Let/Get syntax. Likewise, I dislike (and code to minimize the use...
104
5445
by: Colin McGuire | last post by:
Hi, is there a way to show a form without a titlebar (and therefore no control box/minimize box/title etc) but still have it appear looking like 3D? The property FormBorderStyle to None - this gives no titlebar etc but the form borders don't look 3D. In case I haven't explained what I want well, I want a form that looks like a button...
29
3545
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this one data field - but i'm not sure) :-) Background info:
6
1657
by: tshad | last post by:
I am playing with Inheritance and want to make sure I understand it. I have the following Classes: ******************************************* Public Class AuthHeader:Inherits SoapHeader Public AuthHeaderName As String Public SessionKey As String = "Default" End Class Public Class ServiceTicket:Inherits AuthHeader
37
2191
by: mdh | last post by:
In one of the answers to a K&R exercise, the first couple of lines are: enum loop { NO, YES}; enum loop okloop=YES; I get the first line, but not the second. Sorry about the LOL question. Thanks in advance
25
1481
by: Brian | last post by:
Can some one please tell me what I'm doing wrong. I'm trying to create a class called Dog, but Visual Basic tells me that I can't enter Wolf.age....why is this? Public Class Form1 Public Class DOG Dim COLOUR As String Dim AGE As Integer Dim NAME As String
4
2015
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit conversions", the authors have advised the use of named functions that offer conversions instead of conversion operators. In page 87, example 2: Errors that work. class String { // ...
5
2874
by: Frank Millman | last post by:
Hi all This is not strictly a Python question, but as I am writing in Python, and as I know there are some XML gurus on this list, I hope it is appropriate here. XML-schemas are used to define the structure of an xml document, and to validate that a particular document conforms to the schema. They can also be used to transform the...
0
7659
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7580
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7882
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7945
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3634
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.