dynamic arrays (convert vector to array)

N

NeeZee

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.
 
A

Art Stamness

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
 
I

Ian Collins

NeeZee said:
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.
 
N

NeeZee

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?
 
K

Kai-Uwe Bux

Ian said:
NeeZee said:
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
 
A

Art Stamness

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
 
A

Aaron Graham

[...]
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top