Thread: Array Question

  1. #1

    Array Question

    I want to get this out of the way. It may sound stupid, but I always end up doing it wrong and causing all sorts of problems.

    How do I make an array with an unlimited amount of elements.

    Thanks
    -Save the whales. Collect the whole set.

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    You can't. To create a static or even dynamic array you have to specify the size.
    If you want to have a container that can hold an "unlimited" (which of course is not possible because you can always run out of memory ) number of elements, you can use a linked list or other such data structure.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Use one of the provided STL containers, probably a vector would be best for you. For example...
    Code:
    #include <vector>
    ...
    vector<int> v1;  // Create a vector to hold ints
    ...
    // Now just keep pushing ints onto the vector to your heart's content.
    // It will keep growing and growing...
    v1.push_back(1);
    v1.push_back(14);
    ...
    v1.push_back(-6);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Originally posted by hk_mp5kpdw
    Use one of the provided STL containers, probably a vector would be best for you. For example...
    Code:
    #include <vector>
    ...
    vector<int> v1;  // Create a vector to hold ints
    ...
    // Now just keep pushing ints onto the vector to your heart's content.
    // It will keep growing and growing...
    v1.push_back(1);
    v1.push_back(14);
    ...
    v1.push_back(-6);
    That's nice, but what's the liscence info on that.
    Do big game developers use it?
    -Save the whales. Collect the whole set.

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    It's part of the STL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM