1. ## integer overflow

Hi

I need help to know why am I getting "integer overflow" when trying to assign 10^10 to a
unsigned long int. My compiler is (gcc 4.2).

Doesn't unsigned long int vary from 0 to 4,294,967,295 in a ordinary 32bit IMB/PC?

Thanks. 2. >why am I getting "integer overflow" when trying to assign 10^10 to a unsigned long int
>Doesn't unsigned long int vary from 0 to 4,294,967,295 in a ordinary 32bit IMB/PC?
You're aware that 10^10 has one more digit than a 32-bit unsigned integer can hold, right?

10000000000 - 10^10
4294967296 - 2^32 3. Originally Posted by Prelude >why am I getting "integer overflow" when trying to assign 10^10 to a unsigned long int
>Doesn't unsigned long int vary from 0 to 4,294,967,295 in a ordinary 32bit IMB/PC?
You're aware that 10^10 has one more digit than a 32-bit unsigned integer can hold, right?

10000000000 - 10^10
4294967296 - 2^32
Sorry. My fault.

But I still receive integer overflow when switching to unsigned long long... 4. You need to make sure your arithmetic is being done using unsigned long long. In particular, there's a suffix, either LLU or ULL, for constants to make them unsigned long long. 5. What does your code look like?

The following works for MS VC.Net:
Code:
```#include <stdio.h>
#include <math.h>

int main()
{
_int64 a;

a = 1000I64 * 1000 * 1000 * 1000;
printf("%I64d\n", a);
return 0;
}``` 6. Originally Posted by matsp What does your code look like?

The following works for MS VC.Net:
Code:
```#include <stdio.h>
#include <math.h>

int main()
{
_int64 a;

a = 1000I64 * 1000 * 1000 * 1000;
printf("&#37;I64d\n", a);
return 0;
}```
Actually, I have:
Code:
```# define h (1e-2)
# define t_min -500.0
# define t_max 500.0
# define y_min -500.0
# define y_max 500.0
# define lin (unsigned long long)((y_max-y_min)/h+2)
# define col (unsigned long long)((t_max-t_min)/h+1)```
And in the main()
Code:
```   double *grid;
grid=malloc(lin*col*sizeof(double));    //line 33```
When I try to compile it, i have:
blah.c:32: warning: large integer implicitly truncated to unsigned type

But when I used the lines
Code:
```# define lin (unsigned long)((y_max-y_min)/h+2)
# define col (unsigned long)((t_max-t_min)/h+1)```
I got integer overflow on line 33.

I'm using these macros because y_min/max etc are parameters that i'm constantly changing.

Thanks 7. why to divide on 1e-2 and not multiply on 100? - It can be done using unsigned int arithmetics 8. Originally Posted by vart why to divide on 1e-2 and not multiply on 100? - It can be done using unsigned int arithmetics
It's because I need h, t_max/min, y_max/min to be double and they're not always cute like 500 and 1e-2...

Thanks again...

PS I've just read the following: http://gcc.gnu.org/ml/gcc-bugs/2000-10/msg00479.html

Can it possibly be my case? 9. Right, you do not want to use long long for malloc - if you ACTUALLY need long long size numbers, you won't be able to malloc that memory anyways!

Sorry for the slow reply, but I had to double-check the math: You are creating an array that is 100001 * 100002 * 8 bytes long - that is just a few times larger than the absolute amount of memory you can use in a 32-bit OS. So no matter what you do, you can't achieve that unless you get y'self a 64-bit OS. [And even then, you need a fair chunk of RAM in it, like 80GB of RAM - better get one o' them tha' 4 CPU Opteron motherboards with 4 sticks of 8GB per CPU...

Get the idea that this isn't going to fly?

--
Mats 10. Originally Posted by matsp Right, you do not want to use long long for malloc - if you ACTUALLY need long long size numbers, you won't be able to malloc that memory anyways!

Sorry for the slow reply, but I had to double-check the math: You are creating an array that is 100001 * 100002 * 8 bytes long - that is just a few times larger than the absolute amount of memory you can use in a 32-bit OS. So no matter what you do, you can't achieve that unless you get y'self a 64-bit OS. [And even then, you need a fair chunk of RAM in it, like 80GB of RAM - better get one o' them tha' 4 CPU Opteron motherboards with 4 sticks of 8GB per CPU...

Get the idea that this isn't going to fly?

--
Mats
LOL, my adviser wouldn't let me use the super... :P
Can you point me a way to read such a big matrix as a (100e3)x(100e3) of doubles in a ordinary PC?

Thanks 11. Hi

I just realized that this matrix is unnecessarily large...
I can do my stuff with much smaller matrices...

Thanks anyway!!! 12. Yes, using smaller matrices is the only solution I can think of immediately.

If you only need some of the values (as in, there is a large amount of the matrix that holds some constant value that you don't actually need to store), you could use a technique called "sparse arrays" or "sparse matrices", where you only store some of the data in the matrix, and other parts (not filled in) are considered a constant value (such as zero, 1.0, -1.0 or whatever is a common value in the matrix). That way, you only need a fraction of the memory, because you are "recycling" the same constant value for all those cells that don't hold a specific value.

--
Mats Popular pages Recent additions 