Thread: largest integer in c

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    largest integer in c

    Hi,

    I was trying to execute the following code which was given in an instruction.
    However, the program crashes every time I try to execute it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      int nb = 4194304;
      
      int arr[nb];
      int i=0;
      for(i=0; i<nb; i++)
               arr[i] = i;
      
      system("PAUSE");	
      return 0;
    }
    What is the largest int value that can be represented?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is not the size of the integer that is the problem, it's the size of a stack-allocated array - the variable arr is about 2MB large, which probably blows the stack.

    Either use a global/static variable, or use dynyamic memory allocation.

    And to answer your actual question, the largest integer value is about 2147 million.

    --
    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.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's 0xFFFFFFFF. But the problem isn't that. You are creating a local variable on the stack with that value, which is too big for the stack, hence the crashing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by mpi_beginner View Post
    Hi,

    I was trying to execute the following code which was given in an instruction.
    However, the program crashes every time I try to execute it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      int nb = 4194304;
      
      int arr[nb];
      int i=0;
      for(i=0; i<nb; i++)
               arr[i] = i;
      
      system("PAUSE");	
      return 0;
    }
    What is the largest int value that can be represented?
    it depends on the size of the stack - use maloc for dynamic array allocation - at least you can check the failure to allocate array and exit without crash
    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

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    It's 0xFFFFFFFF.
    Unsigned yes. int is signed, so it is 0x7FFFFFFFF.

    --
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Oh, and I misread the number of digits, your variable is about 16MB, which is certainly beyond the scope of normal stack sizes.

    --
    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.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    Quote Originally Posted by matsp View Post
    It is not the size of the integer that is the problem, it's the size of a stack-allocated array - the variable arr is about 2MB large, which probably blows the stack.

    Either use a global/static variable, or use dynyamic memory allocation.

    And to answer your actual question, the largest integer value is about 2147 million.

    --
    Mats
    Thanks for the info. It works now with dynamic allocation.

    Just a question out of interest: Isnt it more than 2MB?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mpi_beginner View Post
    Thanks for the info. It works now with dynamic allocation.

    Just a question out of interest: Isnt it more than 2MB?
    Yes, I realized that and corrected it about 20 minutes ago in the last post before yours. It's a bit more than 16MB.

    --
    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.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    Unsigned yes. int is signed, so it is 0x7FFFFFFFF.

    --
    Mats
    True, but I always think of that 0xFFFFFFFF is the biggest hex number you can stuff into an integer. Obviously, if signed, it will just represent -1, but still...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    BTW: the minimum and maximum values specific data types can store are defined as constants in limits.h. Some of these constants are:
    • INT_MAX: max of (signed) int
    • INT_MIN: min of (signed) int
    • UINT_MAX: max of unsigned int
    • etc.

    Another way to figure it out: signed values stored in two's complement representation have a range of -2^(n-1) to +2^(n-1)-1, where n is the number of bits in the number. If you use sizeof, it will tell you the number of bytes in a number; multiply this by 8 (or CHAR_BIT in limits.h if you want to be picky) and you get the number of bits, which you can use in those formulae.
    Last edited by dwks; 02-25-2009 at 05:03 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Even if the stack was big enough, that's not standard C syntax. You are not allowed to declare an array using a variable to indicate the size. Your compiler apparently allows it, but it's wrong.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by brewbuck View Post
    You are not allowed to declare an array using a variable to indicate the size.
    True for C90, false for C99. The current standard allows variable length arrays. You can make gcc emit a warning which states this explicitly:

    Code:
    int main(int argc)
    {
    	int x[argc];
    	return 0;
    }
    Tell gcc to compile with "-std=c89 -pedantic" and it will reply:

    Code:
    warning: ISO C90 forbids variable length array ‘x’
    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

Popular pages Recent additions subscribe to a feed

Similar Threads

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