0

I have a situation where a class holds a vector of constant size 5. I need the data from the vector as an array of size 5 as our std::vector implementation doesn't appear to use contiguous memory (please don't argue about that, I know it should and we've checked it to death). The contiguous memory block is required so we can uuencode/uudecode the memory block easily (those turn an arbitrary memory block into a string).

I'm finding it difficult to return a statically sized array. I can do it by reference or I can do it by wrapping a statically sized array in a structure - but both are a little agitating. By reference requires the calling code to declare the array first and pass it in to the code, and the other option requires that I make an extra structure just for this purpose.

class A {
    public:
        /*return type*/ GetVectorAsArray(/*params*/) { /* implementation */ }

    private:
        std::vector<X> m_vec;
};

So, assuming I need to call GetVectorAsArray, what's the cleanest way to return the 5 values in m_vec to the calling code? You can put whatever you want in return type or params and implementation.

10
  • 5
    I know you've said not to argue about it, but a vector by definition should be holding elements in contiguous memory. What STL implementation are you using so I know to avoid it in the future? Commented Sep 8, 2011 at 19:28
  • 3
    If its size is always five, why not use a C-style array? Commented Sep 8, 2011 at 19:30
  • What is ok to use? Boost? C++11? Anything else? Commented Sep 8, 2011 at 19:32
  • Maybe padding is causing the issue with contiguous memory. For example, maybe sizeof(X) == 7. I think that objects are usually 8-aligned or something like that. Commented Sep 8, 2011 at 19:35
  • 1
    @w00te LynxOS uses GNU toolchain and gcc's implementation of vector is fine, at least in my LynxOS 4.0.0 (gcc 2.95.3, vector is stored as three pointers: start, finish, end_of_storage) Commented Sep 8, 2011 at 19:43

2 Answers 2

6

boost::array is intended for arrays of fixed size, you can always use that. It uses contiguous memory.

Later edit: as correctly pointed out by commenters, std::array is available in the new standard.

Sign up to request clarification or add additional context in comments.

5 Comments

Note that its now part of C++ Standard Library as of TR1
Nowadays that's also available as std::array.
it is indeed, just making sure there's an answer for people not having access to the new standard.
And if neither is available, just a normal C-style array is fine.
@GMan: You can't return C-style arrays by value. The OP didn't specify whether he wanted a copy or the original...
1
template <typename X, size_t N>
class A {
private:
  X m_vec[N];
public:
  const X* GetVectorAsArray() const { return m_vec; }
  size_t GetVectorLength() const { return N; }
};

Usage:

A<int, 5> vec5_int;

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.