dynamic arrays (convert vector to array)

Discussion in 'C++' started by NeeZee, Feb 4, 2006.

  1. NeeZee

    NeeZee Guest

    hi,
    im new to this group and to the c++-language.
    the problem:
    i need an array-like class/data structure whose dimension can be set
    dynamically. i noticed that there is a vector-class in STL, but i need
    to find a way to convert that structure back to an ordinary array
    because there's a function from a matrix-library that needs an array as
    parameter and that i want to call.
    i guess its not as easy as in java for example, where u got a method
    like vector.toarray(). maybe someone knows how to do it anyway.
    thanks.
     
    NeeZee, Feb 4, 2006
    #1
    1. Advertising

  2. NeeZee

    Art Stamness Guest

    Its as easy just not as pretty.

    vector<int> foo ;

    .....
    void ProcessArray ( const int * , size_t numInts ) ;

    ProcessArray ( &foo[0], foo.size() );

    The address of foo[0] will return a pointer to a contiguous memory
    block that contains the values of foo. Vectors are guaranteed to store
    there memory elements in sequential order, so this operation is legal,
    and commonly used.

    The above examples is (poorly ) transcribed from Effective STL , Item
    16, Scott Meyers.

    -Art
     
    Art Stamness, Feb 4, 2006
    #2
    1. Advertising

  3. NeeZee

    Ian Collins Guest

    NeeZee wrote:
    > hi,
    > im new to this group and to the c++-language.
    > the problem:
    > i need an array-like class/data structure whose dimension can be set
    > dynamically. i noticed that there is a vector-class in STL, but i need
    > to find a way to convert that structure back to an ordinary array
    > because there's a function from a matrix-library that needs an array as
    > parameter and that i want to call.
    > i guess its not as easy as in java for example, where u got a method
    > like vector.toarray(). maybe someone knows how to do it anyway.
    > thanks.
    >

    Please avoid txt speak such as 'u' in postings.

    While not mandated by the standard, every vector implementation I have
    seen uses contiguous memory (to do anything else would have a
    detrimental effect on the [] operator performance), so you can pass the
    address of vector[0] to functions that expect an array.

    --
    Ian Collins.
     
    Ian Collins, Feb 4, 2006
    #3
  4. NeeZee

    NeeZee Guest

    Thx.
    tried it that way,
    had to modify it a bit, cos im using double values:

    vector<double> foo ;

    void ProcessArray ( const double * , size_t numDoubles ) ;

    ProcessArray ( &foo[0], foo.size() );

    when i call the function: Coord_Mat_double
    r(matSize,matSize,nonZeros,foo,row,col);
    which is part of the sparse-matrix-package im using and expects a
    double-array as 4th parameter the compiler still says i called it using
    a double-vector as 4th parameter.
    Any idea why? Isn't the size_t type suitable for double-values?
     
    NeeZee, Feb 4, 2006
    #4
  5. NeeZee

    Kai-Uwe Bux Guest

    Ian Collins wrote:

    > NeeZee wrote:
    >> hi,
    >> im new to this group and to the c++-language.
    >> the problem:
    >> i need an array-like class/data structure whose dimension can be set
    >> dynamically. i noticed that there is a vector-class in STL, but i need
    >> to find a way to convert that structure back to an ordinary array
    >> because there's a function from a matrix-library that needs an array as
    >> parameter and that i want to call.
    >> i guess its not as easy as in java for example, where u got a method
    >> like vector.toarray(). maybe someone knows how to do it anyway.
    >> thanks.
    >>

    > Please avoid txt speak such as 'u' in postings.
    >
    > While not mandated by the standard, every vector implementation I have
    > seen uses contiguous memory (to do anything else would have a
    > detrimental effect on the [] operator performance), so you can pass the
    > address of vector[0] to functions that expect an array.


    And it gets even better: as of 2003, contiguity for std::vector<T> is
    mandated by the standard as long as T is not bool. [See 23.2.4./1]


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Feb 4, 2006
    #5
  6. NeeZee

    NeeZee Guest

    Thanx Ian.
    It worked that way.
     
    NeeZee, Feb 4, 2006
    #6
  7. NeeZee

    Art Stamness Guest

    please print out the actual error from the compiler, and the line of
    code that it is saying is causing the error.

    There should be no way that " &foo[0] " is interpretted any other way
    than a pointer to const X where X it seems is double in your case.
    There may be other problems.

    -Art
     
    Art Stamness, Feb 4, 2006
    #7
  8. NeeZee

    Aaron Graham Guest

    [...]
    > While not mandated by the standard, every vector implementation I have
    > seen uses contiguous memory (to do anything else would have a
    > detrimental effect on the [] operator performance), so you can pass the
    > address of vector[0] to functions that expect an array.


    See the following:

    http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#69

    It wasn't stated by the original draft of the standard, but it was
    intended. The fact that it wasn't stated explicitly was noticed and a
    defect report/technical corrigenda was issued. A standards-conforming
    vector implementation must be contiguous.

    Aaron
     
    Aaron Graham, Feb 4, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,820
  2. Replies:
    8
    Views:
    1,930
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    567
    James Kanze
    Sep 4, 2007
  4. Philipp
    Replies:
    21
    Views:
    1,134
    Philipp
    Jan 20, 2009
  5. Phlip
    Replies:
    13
    Views:
    199
    Phlip
    Feb 4, 2008
Loading...

Share This Page