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
> 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).
>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) ...
--
Remove del for email
> On 24 Mar, 06:54, T Ryi <tryi...@gmail.com> wrote:
>
>> 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).
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>
--
Ben.
My guess is that
if((unsigned char *)cp < buffer + sizeof *cp)
will probably do what OP wants.
> But then the question would be - why process all but the last byte of
> an int?
I don't think that's what OP is doing.
cp can only be altered to values which are aligned to the type of *cp.
> So the real question becomes - what are you really trying to do?
That's a good question.
--
pete
But taking that into consideration,
if((unsigned char *)cp < buffer + 1)
would be just as good.
> 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