Array size at run time unknown

This is a discussion on Array size at run time unknown within the C++ Programming forums, part of the General Programming Boards category; Hello All, I'm looking to write a program which will read in a set of data points from a file ...

  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,344
    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
    22,313
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,344
    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, 09:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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