Question about the standard or maybe just good practice?

S

Sims

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
 
J

Jakob Bieling

Sims said:
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
 
V

Victor Bazarov

Sims said:
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_expression)

and

if (integral_expression != 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(ANOTHERSTRUCT) 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
 
J

Jakob Bieling

Sims wrote:
[...] 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
 
S

Sims

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?
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.


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?)
direference

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?
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);


sizeof (ANOTHERSTRUCT) would do. Or, if that struct contains pointers as
well, you should provide a 'size' function for that structure as well.


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.

Sims
 
S

Sims

[...] 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
 
S

Sims

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.
No. The behaviours of

if (integral_expression)

and

if (integral_expression != 0)

I am sorry wh..
are you saying that "someval & ab" will return TRUE or FALSE, (1 or 0 )?
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.
direference

Yes, it will.

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

Victor Bazarov

Sims said:
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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top