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.
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.
>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
My best code is written with the delete key.
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.
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; }
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Actually, I have:
And in the main()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)
When I try to compile it, i have:Code:double *grid; grid=malloc(lin*col*sizeof(double)); //line 33
blah.c:32: warning: large integer implicitly truncated to unsigned type
But when I used the lines
I got integer overflow on line 33.Code:# define lin (unsigned long)((y_max-y_min)/h+2) # define col (unsigned long)((t_max-t_min)/h+1)
I'm using these macros because y_min/max etc are parameters that i'm constantly changing.
Thanks
why to divide on 1e-2 and not multiply on 100? - It can be done using unsigned int arithmetics
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
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?
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Hi
I just realized that this matrix is unnecessarily large...
I can do my stuff with much smaller matrices...
Thanks anyway!!!
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.