How large can arrays be?

This is a discussion on How large can arrays be? within the C++ Programming forums, part of the General Programming Boards category; Hi, I`mworking on a cnc application, which involves some c++ programming. I wonder what would be an upper limit for ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    21

    How large can arrays be?

    Hi,
    I`mworking on a cnc application, which involves some c++ programming.
    I wonder what would be an upper limit for array size?
    If I'd store all individual steps of a steppermotor in an array, it could amount to over 10000.
    Is this acceptable?
    for example:
    Code:
     int x[10000];

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Depends entirely on your compiler / OS / hardware
    It also depends whether the array is global / static / local

    The language itself has no constraints on how big arrays can be.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    are you surer about that? I though C specified a portable maximum of 65535, so probably C++ does it too.
    Of course a compiler can choose something bigger but not smaller

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    168
    Wouldn't it depend on whether you used int, float or whichever type of variable?
    -Felix
    Rots Soft
    If the facts don't fit the theory, change the facts.
    Albert Einstein (1879 - 1955)

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The language would specify the minimum size, as is the case with switch, no pun intended. The rest is as stated by Salem. (No surprise there.)

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    LANGUAGE LIMITATIONS - I looked through the language standard a bit, and the only thing I found was that the size-specifier in the brackets must evaluate to a "constant integral value". If I understand that correctly, you should be able to use an unsigned long int, which can hold values up to 4,294,967,295. (4 bytes).

    COMPILER LIMITATIONS - The language standard guarantees that a type-long will hold at least 4 bytes, but your compiler may allow larger values!!! (It's generally bad practice to exceed the limits of the language standard, and probably not a good idea to make an array that big anyway.)

    HARDWARE/MEMORY LIMITATIONS - You might run out of memory before hitting the above limits.

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    LANGUAGE LIMITATIONS - I looked through the language standard a bit, and the only thing I found was that the size-specifier in the brackets must evaluate to a "constant integral value". If I understand that correctly, you should be able to use an unsigned long int, which can hold values up to 4,294,967,295. (4 bytes).
    I'm afraid you don't understand that correctly. Integral doesn't mean it has to be of type int, integral refers to any number evenly divisible by 1 in general. That's where all the ints get their name. If that was the limit, I'm sure it would've been specified in more direct terms.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    No... I think he understands. The unsigned long int is the largest standard integral type, and therefore, you can index anything up to its max value. Of course, as he said, you may not have enough memory (or perhaps patience) to make an array of anything that large.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Thanks for the support Zach!

    I'm sure it would've been specified in more direct terms.
    Hmmm... Really??? Well, perhaps you could say the standard is "direct", because it IS very specific. (A specification must be specific.) But, it is also very "technical" and it is NOT easy to understand. And, it's not easy to find what you are looking for.

    The C++ standard is about 700 pages long, and in places it refers-back to the C standard which is about 500 pages. I have the standards in PDF format which adds to the difficulty of finding what I'm looking for.

    The language standard is the most complete, and most accurate C++ publication available, but I still find my other books quite useful. I don't think I could have figured-out how to write "Hello World" by just reading the published standard!

    You can download the standards from ANSI for about $20 each. Hard copies of both the C and C++ standards would cost you over $500 !!!!

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Different amounts of space are allocated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Large 2D Array's Problem
    By kas2002 in forum C Programming
    Replies: 4
    Last Post: 05-25-2009, 01:42 PM
  2. Allocating a number of large 3d arrays in C
    By Surrender in forum C Programming
    Replies: 22
    Last Post: 08-19-2008, 09:36 AM
  3. Large arrays, malloc, and strcmp
    By k2712 in forum C Programming
    Replies: 1
    Last Post: 09-24-2007, 09:22 PM
  4. Problem declaring large arrays
    By mattAU in forum C Programming
    Replies: 5
    Last Post: 09-28-2006, 06:47 AM
  5. Large Arrays
    By xurumin in forum C++ Programming
    Replies: 7
    Last Post: 08-26-2003, 06:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21