pre defined arrays

This is a discussion on pre defined arrays within the C++ Programming forums, part of the General Programming Boards category; hi..i wanted to do something like this: Code: int size; cin>>size; int numList[size]; but i get error....how do i make ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    24

    pre defined arrays

    hi..i wanted to do something like this:

    Code:
    int size;
    cin>>size;
    
    int numList[size];

    but i get error....how do i make a int array of specific size?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You need to use dynamic memory allocation.
    Code:
    #include <iostream>
    
    int main() {
        int size;
        std::cin >> size;
    
        int *numList = new int[size];
        /* do something */
        delete [] numList;  /* very important */
    }
    If you forget to delete the memory, it will be lost (at least until your program exits, usually). If you do this too often your program might crash. So, when you're using new, don't forget to use delete too.
    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.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    24

    finding end of array

    thanx for the reply.

    now if i want to output all the data of the list i would do this ?

    Code:
    while(numList!=0){
        cout<<*numList;
        numList++'
    }
    it doesnt work though,.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Arrays don't end in 0 (that is just how C style strings work so they can save their length). You already know the size so just use a for loop and loop from index 0 to size-1.

    BTW, in C++ there is little reason to use a dynamic array for this. Use a vector instead:
    Code:
    #include <iostream>
    #include <vector>
    
    int main() {
        int size;
        std::cin >> size;
    
        std::vector<int> numList(size);
        /* do something */
    
        /* no need to delete, its done automatically */
    }
    The for loop to output it is the same.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You already know the size so just use a for loop and loop from index 0 to size-1.
    Inclusive, of course.

    Code:
    std::vector<int> numList(size);
    The nice thing about vectors is that they resize to hold however much data you want. You don't need to know the size beforehand. You can supply a size, if you wish, but that just causes the vector to allocate at least that much memory.

    Here's a vector reference: http://cppreference.com/cppvector/index.html

    The method you will probably find most useful for vectors is push_back(), which adds something to the end of the vector. That and size(), which returns the number of elements in the vector. You can access elements of a vector just like an array.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Nasty linker problem; multiply defined
    By cboard_member in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2005, 03:06 PM
  5. errors initializing D3D
    By Vacation Guy in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2005, 12:20 PM

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