Question about the standard or maybe just good practice?



 DEVELOP > c-Plus-Plus > Question about the standard or maybe just good practice?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Sims"
Date: 08 Aug 2003 12:22:39 PM
Object: 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
.

User: "Jakob Bieling"

Title: Re: Question about the standard or maybe just good practice? 08 Aug 2003 03:35:41 PM
"Kevin Goodsell" <usenet1.spamfree.fusion@neverbox.com> wrote in message
news:3f3401f4@shknews01...

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)
.
User: "Victor Bazarov"

Title: Re: Question about the standard or maybe just good practice? 08 Aug 2003 03:42:39 PM
"Jakob Bieling" <netsurf@gmy.net> wrote...

"Kevin Goodsell" <usenet1.spamfree.fusion@neverbox.com> wrote in message
news:3f3401f4@shknews01...

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!
.
User: "Sims"

Title: Re: Question about the standard or maybe just good practice? 08 Aug 2003 04:14:11 PM

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



User: "Jakob Bieling"

Title: Re: Question about the standard or maybe just good practice? 08 Aug 2003 12:47:13 PM
"Sims" <siminfrance@hotmail.com> wrote in message
news:bh0mch$ssekn$1@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)
.
User: "Sims"

Title: Re: Question about the standard or maybe just good practice? 08 Aug 2003 03:59:22 PM


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
.


User: "Victor Bazarov"

Title: Re: Question about the standard or maybe just good practice? 08 Aug 2003 12:56:02 PM
"Sims" <siminfrance@hotmail.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_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
.
User: "Sims"

Title: Re: Question about the standard or maybe just good practice? 08 Aug 2003 04:22:09 PM


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_expression)

and

if (integral_expression != 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.
.
User: "Victor Bazarov"

Title: Re: Question about the standard or maybe just good practice? 08 Aug 2003 04:41:22 PM
"Sims" <siminfrance@hotmail.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_expression)

and

if (integral_expression != 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
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER