1

I got stuck in a programming task. I want the elements of my stl vector to be placed in a

contiguous memory to send it with MPI_Send() routine.

here is an example:

class Tem
{

//...
private: 
 vector<double> lenghtVector (4500);//this gives a compilation error but I need to have a fixed sized vector

};

how can I have a vector with a serial memory of should I do something else?

Thanks. Kindest Regards.

SRec

1
  • nevermind (question was changed while answering) Commented Mar 1, 2010 at 11:38

2 Answers 2

2

The elements of a vector are stored contiguously according to C++ Standard (23.2.4/1). To resize it you could use appropriate constructor in the initializer list of Tem class.:

class Tem
{
  Tem() : lenghtVector(4500) {};
private: 
 vector<double> lenghtVector;
};
Sign up to request clarification or add additional context in comments.

2 Comments

Well thank you Krill, Can I state the following ? Providing I do not exceed the initial length of the vector I can guarantee that the elements are held in contiguous. is it true? Regards
Elements of a vector are stored contiguously all the time. Using resize or any other functions of std::vector will not change this rule.
1

vector will do what you want, as the data is guaranteed to be contiguous. Use &(v[0]) to get a pointer that you can pass to MPI_Send().

If you don't need the dynamic sizing of vector, you might want to look at the Boost Array class. The size is fixed at compile time, but it is an STL compatible container, so you get begin(), end(), size(), etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.