Casting (signed long)(unsigned long)

C

curious_one

All,
I have a sample code that does something like this,

unsigned long i = SOME_HUGE_VALUE;
unsigned long j = SOME_HUGE_VALUE;

void decrement_count()
{
if((signed long)j > 0)
j--
else
printf("Empty");
}

void add_count()
{
if((signed long)j < i)
{
j++;
}
else
{
printf("Error");
}
}

Will the casting in add_count truncate the j value while
comparision,as a result j++ will have a value more than i at some
point?

regds
curious
 
D

Dan Pop

In said:
I have a sample code that does something like this,

unsigned long i = SOME_HUGE_VALUE;
unsigned long j = SOME_HUGE_VALUE;

void decrement_count()
{
if((signed long)j > 0)
j--
else
printf("Empty");
}

void add_count()
{
if((signed long)j < i)
{
j++;
}
else
{
printf("Error");
}
}

Most likely, this code was written by someone who didn't know what he
was doing. To behave correctly, both casts must be eliminated.

Dan
 
J

Jack Klein

All,
I have a sample code that does something like this,

unsigned long i = SOME_HUGE_VALUE;
unsigned long j = SOME_HUGE_VALUE;

void decrement_count()
{
if((signed long)j > 0)

This may NEVER be true. An unsigned long with a value greater than
ULONG_MAX will result in a negative value when cast to signed long on
most implementations.
j--
else
printf("Empty");
}

void add_count()
{
if((signed long)j < i)
{
j++;
}
else
{
printf("Error");
}
}

Will the casting in add_count truncate the j value while
comparision,as a result j++ will have a value more than i at some
point?

No, there is no such guarantee.
 
P

Peter Nilsson

Jack Klein said:
This may NEVER be true. An unsigned long with a value greater than
ULONG_MAX will result in a negative value when cast to signed long on ^^^^^^^^^
most implementations.

You mean: LONG_MAX
 
C

curious_one

Dan,
Thanks for your reply, I am not entirely clear on why the casts
need to be eliminated. [Not that I know the reson why the casts are
there in the first place :)], may i ask you the reason why the code
will result in undefined behaviour ?

thanks
-curious
 
P

pete

curious_one said:
All,
I have a sample code that does something like this,

unsigned long i = SOME_HUGE_VALUE;
unsigned long j = SOME_HUGE_VALUE;

void decrement_count()
{
if((signed long)j > 0)
j--
else
printf("Empty");
}

void decrement_count()
{
if (j != 0) j--;
else printf("Empty");
}
void add_count()
{
if((signed long)j < i)
{
j++;
}
else
{
printf("Error");
}
}


void add_count()
{
if (j != 0lu - 1) {
j++;
} else {
printf("Error");
}
}
Will the casting in add_count truncate the j value while
comparision,as a result j++ will have a value more than i at some
point?

Conversion of out of range values to signed types,
is implementation defined.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top