Pointer comparisions of same type

T

T Ryi

Hi,
I've a code like this
if(cp < buffer + sizeof(int) -1 ) ....

Now buffer is unsigned int * , and cp is signed int *. Gcc gives a
warning .

I change it to
if(cp < (int *)(buffer + (sizeof(int) -1 ) )) ...

Is there anything wrong with this cast ? I'm assuming that casting
between 2 pointers of same type but in different signedness doesn't
change anything . Is my assumption correct ?


thanks
try
 
N

Nick Keighley

 I've a code like this
 if(cp < buffer + sizeof(int) -1 ) ....

Now buffer is unsigned int * , and cp is signed int *. Gcc gives a
warning .

I change it to
 if(cp < (int *)(buffer + (sizeof(int) -1 ) )) ...

Is there anything wrong with this cast ?   I'm assuming that casting
between 2 pointers of same type but in different signedness

in other words *not* of the same type

doesn't change anything . Is my assumption correct ?

it'll most likely work on most implementations but I think it is
technically Undefined Behaviour (UB). And if cp is not in the same
array ("object" is the technical term) as buffer then you'll /also/
get UB.

UB means the implementor is allowed by the C standard to anything he
damn well pleases (including working the way you expect it to).
 
B

Barry Schwarz

Hi,
I've a code like this
if(cp < buffer + sizeof(int) -1 ) ....

Now buffer is unsigned int * , and cp is signed int *. Gcc gives a
warning .

What is the intent of the above code? Adding sizeof(int) to any kind
of int pointer is a bit unusual. If sizeof(int) is 4, the right
expression evaluates to the address of the fourth unsigned int in
buffer (&buffer[3]). If sizeof(int) is 8, it would be &buffer[7]. Is
that what you really want?

Are you sure buffer is not a pointer to unsigned char (and cp a
pointer to char or signed char)? If so, the expression would evaluate
to the last byte of the first int in buffer. Depending on endianness,
that could be the low order or high order byte of the int.

But then the question would be - why process all but the last byte of
an int?

So the real question becomes - what are you really trying to do?
I change it to
if(cp < (int *)(buffer + (sizeof(int) -1 ) )) ...

Why did you add parentheses around sizeof(int)-1.
Is there anything wrong with this cast ? I'm assuming that casting
between 2 pointers of same type but in different signedness doesn't
change anything . Is my assumption correct ?

They are not the same type but corresponding types.

A signed type and the corresponding unsigned type must have the same
alignment and size. Therefore, if the value of the operand expression
is valid, then the cast operator will evaluate to a legal value which
can be assigned to cp.

Incidentally, all of your extra parentheses are superfluous. The
statement will evaluate the same if you code it
if(cp < (int *)buffer + sizeof(int) -1) ...
 
B

Ben Bacarisse

Nick Keighley said:
in other words *not* of the same type



it'll most likely work on most implementations but I think it is
technically Undefined Behaviour (UB).

I don't think it is undefined. You can convert between pointer to
object types and the only possibility of UB comes from the pointer
being misaligned (6.3.2.3 p7), but corresponding signed and unsigned
types have the same alignment (6.2.5 p9). (The wording is not really
clear on alignment, but a footnote tells us what the intent is).

If the OP were to post the declarations of cp and buffer along with a
note on the intent, things would be a lot clearer.

<snip>
 
R

Richard Bos

T Ryi said:
I've a code like this
if(cp < buffer + sizeof(int) -1 ) ....

Now buffer is unsigned int * , and cp is signed int *. Gcc gives a
warning .

I change it to
if(cp < (int *)(buffer + (sizeof(int) -1 ) )) ...

Is there anything wrong with this cast ?

Yes: it's a cast made as a knee-jerk reaction to a warning. Don't Do
That. Think about what you _really_ want to do. Why, to begin with, are
you comparing a pointer to unsigned int to one to signed int at all?
This strongly suggests that either cp or buffer has the wrong type.

Richard
 

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,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top