Thread: Array size at run time unknown

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    11

    Array size at run time unknown

    Hello All,

    I'm looking to write a program which will read in a set of data points from a file and place these in a float-array, the problem is i don't know how many data points i will have. I have been making oversized arrays and then just filling them up to N values and keeping track of N but I would like a more dynamic way of doing this. I have looked at the vector class but I am using a third-party library in my program and i need to pass float arrays to some of it's functions otherwise the vector would be the perfect solution.

    is there a way to initailise an array dynamically or initalise an array with a vector ?
    How does the vector class get around the issue of dynamic allocation?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'd say a vector is definitely the way to go. You can use a vector and still pass the array to the third party library:
    Code:
    std::vector<float> data(5, 3.14);
    thirdPartyFunction(&data[0], data.size());
    The &data[0] part provides a pointer that will work just like a pointer to a normal array.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I have looked at the vector class but I am using a third-party library in my program and i need to pass float arrays to some of it's functions
    If you have a std::vector<float> named vec with at least one element, pass &vec[0] to those functions (they should take a float*). Effectively, this is how you can access a vector as an array.
    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

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Magic. It works !

    Thanks you both.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Just make sure the size of the vector is big enough for what the function expects. For example, if the function is expecting an array with three elements, you can't use that trick with an empty vector:
    Code:
    std::vector<float> data;
    fillArrayWithThreeValues(&data[0]); // Bad!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  2. array of unknown size
    By richdb in forum C Programming
    Replies: 7
    Last Post: 02-25-2006, 11:48 AM
  3. A vector or an array of unknown size
    By strickey in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2005, 09:11 AM
  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