Leigh Johnston said:
On 02/02/2011 00:03, Paul wrote:
On 01/02/2011 23:23, Paul wrote:
On 01/02/2011 22:25, Paul wrote:
On 01/02/2011 19:39, Ian Collins wrote:
On 02/ 2/11 07:40 AM, mathieu wrote:
Dear all,
Could someone please let me know what is wrong with the
following:
#include<vector>
int main()
{
typedef double (OriginType)[3];
std::vector< OriginType> OriginArray;
// OriginArray.reserve( 1 );
OriginArray.resize( 1 ); // does not compile
return 0;
}
As Leigh said, you can't sore an array in a container because you
can't
assign one to another (try it!).
But you can put an array in a struct:
struct OriginType
{
double data[3];
};
Or alternatively use:
typedef std::tr1::array<double, 3> OriginType;
or possibly
typedef std::array<double, 3> OriginType;
depending on your compiler and if it is fairly up-to-date and
includes
TR1.
/Leigh
Or store a pointer to a dynamically allocated array.
That would be a sub-optimal solution as it defeats the purpose of
using a vector in the first place but I wouldn't expect *you* to
know
this given your past record.
I don't know what you are talking about but a pointer is probably
more
optimal than your suggested techniques.
vector<double*> OriginArray;
As I said sub-optimal; instead of a single allocation for all elements
in the vector you now have one allocation for every element in the
vector which does not scale well; you lose the property of
contiguousness which is sometimes an important requirement; you now
have to write extra code to delete the arrays the vector elements
point to to avoid memory leaks.
The memory allocation is optional , the same can be done on the stack.
You said:
"store a pointer to a dynamically allocated array"
You are now moving the goal posts which is the typical behaviour of a
troll. There is no indication that the OP wants a stack based solution.
You said my method was sub optimal because it required allocation.
So I simply pointed out that my pointer method can be used on the stack
also.
If your method was on the heap you'd still need to allocate/delete each
array so your comparsison of the two methods was completely incomparable.
Any goalpost shifting has been done by your attempt to make dynamic
allocation appear sub-optimal.