Thread: Dynamic (Numeric) Arrays

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    5

    Dynamic (Numeric) Arrays

    Just joined this forum last night. Found it by way of the STL tutorial elsewhere on this site:

    The STL Vector Class

    I happen to be just getting my feet wet when it comes to dynamic arrays. All my programming involves arrays of numerical data, usually type double, and generally includes 1D and 2D arrays, nothing with more dimensions. Learning these concepts is proving to be quite tricky; finding a decent tutorial is proving to be much more difficult than I thought.

    Does anybody here know where I can find a decent tutorial, with source code snippets, that teaches a person how to use <vector> to create 1D and 2D arrays? The programs I plan to write will be quite large, and the arrays will be passed in and out of sub-routines frequently.

    Thanks.


    David

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Well... I have a really good STL book: The C++ Standard Library, by Nicolai M. Josuttis.

    I'm a little "rusty" on this stuff, so I don't have any specific suggestions about your 2d / 3d vectors.... Make that "very rusty!"
    Last edited by DougDbug; 04-19-2006 at 02:35 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm not sure that you'll need much help beyond the examples in that tutorial. You have to think of vector<double> as a type like any other. So a 2D array of doubles is really just a vector of vector<double>. And you can pass a vector<double> to a function like you would any other variable that you wanted to pass to a function. Generally, with vectors that is done as a const reference if the vector won't be modified by the function, and as a non-const reference if it will.

    cppreference.com or the sgi STL site might have more information to help you out, and Josuttis' The C++ Standard Library is probably the best reference for the standard library containers and algorithms.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    There is one little gotcha. You have to leave a space between the closing angled brackets or the compiler will think you are trying to use the >> operator rather than closing a vector of vectors.

    std::vector<vector<int> > myVectorOfVectorOfInts;
    You're only born perfect.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    Thanks for all the feedback.

    I now have a working program. One question I wondered about, but didn't post: does the section of code in which vector variables are resized have to be wrapped in a try block? I found an answer and thought I'd share it in case others have the same question: Yes, it does.

    <vector> does no exception catching internally when trying to allocate space for, say, a 1000000x1000000 2D array.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Normally, you wouldn't want to handle an out-of-memory exception everywhere in your code, you would have one or more higher-level functions that can handle that exception relatively gracefully. That function would have the try/catch block around the code that does the bulk of the work.

    So you wouldn't actually put the resize() into a try/catch block any more than you'd put any call to new, any declaration of a string object, vector, or any other container, any call to push_back, or any use of your own classes and functions that use dynamic memory allocation inside its own try/catch block. That may have been what you meant, but I just wanted to clarify. Any construction, insertion or resize on a vector can cause a memory allocation which can throw an exception.

    Of course, if you know that a particular call to resize has a high likelihood of generating such an exception (like in your 1000000x1000000 example), then that is one case where it may make sense to put it specifically into its own try/catch block.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 11:32 AM
  4. dynamic arrays and structures
    By godofbabel in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2002, 03:45 PM