Thread: integer overflow

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    20

    Question 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. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Quote Originally Posted by Prelude View Post
    >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. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Quote Originally Posted by matsp View Post
    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. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    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

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Quote Originally Posted by vart View Post
    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. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Quote Originally Posted by matsp View Post
    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. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    20
    Hi

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

    Thanks anyway!!!

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM