Thread: Set ARRAY to INFINITY!

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    22

    Set ARRAY to INFINITY!

    hello! i just have a quick question about array. Is there anyway i can set my array declaration [ ] to infinity? because i don't want to set a huge number in the array declaration like [10000000]

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you have an infinity-bit CPU and infinity bytes of RAM, then yes.
    Although in that case your program would probably take infinite time to run too.
    Last edited by cpjust; 12-02-2008 at 07:30 AM.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    There is a MAX_INT constant that represents the largest size an integer can hold, but no system with an operating system can create an array of that size.

    You should choose a preferred size and declare that as a constant. Use the constant to declare your array. Also, just because your array is very large does not alleviate the need to make sure that user input is not even longer.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    I think what you need is dynamic memory.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    22
    all right guys! thanks for the tips and advice!

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could also use C++ instead of C. Then you wouldn't have to worry about the buffer size, since std::string will grow as needed.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by King Mir View Post
    There is a MAX_INT constant that represents the largest size an integer can hold, but no system with an operating system can create an array of that size.
    If you had said MAX_LONG or MAX_SIZE_T or some such, then I would have agreed. However, a int array[MAX_INT] is "only" 8GB, so a 64-bit machine with plenty of memory can do that without even breaking into a sweat. (each AMD64 processor can deal with 32GB in an Opteron type setup).

    Ok, so I have never actually had in my posession a machine with more than 6GB of RAM, but it's certainly not "no system with an operating system can create an array of that size" scenario. I've been told we have one (or pair of?) server(s) here where I work that has 1TB or RAM (or if it was 256GB that can be extended to 1TB?).

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

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cpjust View Post
    You could also use C++ instead of C. Then you wouldn't have to worry about the buffer size, since std::string will grow as needed.
    Yep, that or std::vector, depending on the need.
    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.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by matsp View Post
    If you had said MAX_LONG or MAX_SIZE_T or some such, then I would have agreed. However, a int array[MAX_INT] is "only" 8GB, so a 64-bit machine with plenty of memory can do that without even breaking into a sweat. (each AMD64 processor can deal with 32GB in an Opteron type setup).

    Ok, so I have never actually had in my posession a machine with more than 6GB of RAM, but it's certainly not "no system with an operating system can create an array of that size" scenario. I've been told we have one (or pair of?) server(s) here where I work that has 1TB or RAM (or if it was 256GB that can be extended to 1TB?).

    --
    Mats
    Except that's mostly heap memory.

    Declaring an array implies stack space which is much more limited.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    [] doesn't mean infinity. It simply means "i dunno"

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    1

    Pointers

    Why don't you use a dynamic table with pointers?

  12. #12
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    Why do u need an array that big? You know very well that computers cannot evaluate infinity so u should only think about it in manual (e.g. paper-based) operations. Pointers seem like a better idea, but u will never get unlimited space in which to store an array of infinite size, it just cannot happen. If it could, the exact value of pi would be common knowledge.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by P4R4N01D
    Why do u need an array that big?
    More likely davewang merely needs a large array of a size that cannot be determined at compile time. One solution to this is to define an array as large as you would ever need, and since davewang does not know how much is needed, defining an array of infinite size sounded right. In reality, what davewang probably wants is an array whose size can change dynamically to meet the runtime requirements, hence hauzer's suggestion is probably the most appropriate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    22
    yo got my mind laserlight! that's something that i'm struggling on! now, things is clear! probably have to consider back to set an appropriate value for the array! thanks guys anyway!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Set an array with unknown dimensions as global
    By paok in forum C Programming
    Replies: 11
    Last Post: 12-07-2007, 01:37 PM
  2. way to check 3 elements of array and set 4th
    By Syneris in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2006, 11:30 AM
  3. Can you set array contents on initialization?
    By Baron in forum C Programming
    Replies: 2
    Last Post: 12-12-2005, 08:01 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM