3

Please consider the following code:

unsigned int var1 = 4294967295;

unsigned int var2 = 1000000;

unsigned int var3;

var3 = some_expression - (var1*var2)/some_expression;

Bug:

In the expression for var3, the value:

(var1*var2) is being truncated to a 32-bit Integer (since it is obtained by multiplying 2 32-bit Integers).

Possible Fix:

var3 = some_expression - ((unsigned long int)var1*var2)/some_expression;

Problem:

Solaris does NOT accept this typecasting & throws the following error:

"conversion to non-scalar type requested"

Can I fix this issue without typecasting?

14
  • 4
    Nothing in that expression is wrong. Either the Solaris compiler is broken, so use a better compiler, or there's an error in some_expression, so show us your real code. Commented Nov 10, 2012 at 3:47
  • Why not just avoid the overflow in the first place? I.e do the division first, then the multiplication - (var1/someExpression)*var2 Commented Nov 10, 2012 at 3:49
  • 1
    @enhzflep In general, var1/someExpression*var2 does not give the same answer as var1*var2/someExpression, because integer division truncates. Commented Nov 10, 2012 at 3:50
  • 1
    E.g. 2*100/5 is 40 exactly, but computing it as 2/5*100 gives 0. Commented Nov 10, 2012 at 3:52
  • 1
    @Sandeep Singh: Yes, it does matter because the error message you quoted undoubtedly suggests that your type doesn't map to unsigned long, as you apparently incorrectly believe. It maps to some struct type, as I already noted in my answer. Commented Nov 10, 2012 at 7:10

3 Answers 3

1

Introduce an intermediate variable:

unsigned int var1 = 4294967295U;
unsigned int var2 = 1000000U;
unsigned int var3;

{
  unsigned long int vartmp = var1;
  vartmp *= var;

  var3 = some_expression - vartmp/some_expression;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but I can't change the data type of var1. I have simplified the scenario here, for easy understanding. In real code, var1 draws its value from a function whose return type is unsigned int. Changing the type of var1 will shift the error there. I was hoping that maybe someone can solve this using bitwise operators.
Anyways, +1 for your kind effort.
In which way did I change the type of var1 in comparsion to the OP, please? @SandeepSingh
Sorry, I overlooked this point. Yes, this is fine. I will try this approach & let you know. Thanks !
0

There has got to be a better way, but the number theorist in me says to find common factors in var1 var2 and some_expression and try to cancel our common factors. This solution is conditional on them sharing some factors which may not even happen.

The only other thing I can think of is to do some sort of simulated long long big int type math and that can get messy.

Comments

0

Well without explicit typecasting it can be done as

unsigned long tmp = var1;
var3 = some_expression - (tmp * var2) / some_expression;

(assuming unsigned long is larger type than unsigned int) which is pretty much the same thing as your "possible fix".

However, your "possible fix" should work by itself. I don't believe any self-respecting compiler would generate such error message in response to your expression. Post real code that produces this error message.

In fact, I remember an extremely similar question already asked here a day or two ago. In that case the author of the code was typecasting to some typedef-ed type like UL64 believing that it stood for unsigned long int, while in reality that typedef was referring to a struct type. That caused the error message.

4 Comments

I would be shocked if implicit type casting works where explicit doesn't :)
"unsigned long int tmp = var1;" will give the same error. Its almost equivalent to typecasting it in the expression for var3.
Then for what ever reason you have a struct long in your context. You might like to check the pre-processore output for the file giving this error, to see whats really fed to the compiler behind the curtain. @SandeepSingh
Is it unsigned long int tmp = var1; or unsigned long tmp = var1; giving the error? @SandeepSingh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.