views:

407

answers:

3

Hi all,

I'm new to boost shared arrays.

There is existing code that declares two arrays:

boost::shared_array<unsigned char> src; 
boost::shared_array<unsigned char> dest;

All I want to do is swap what each array is pointing to (src becomes dest, and dest becomes src). As I understand it, the shared_array.get() method returns a pointer to one of its elements (can be of any type).

So my added declaration is:

boost::shared_array<unsigned char> temp;

And my added code is:

temp.get() = src.get();
src.get() = dest.get();
dest.get() = temp.get();

Each of these lines fail; compile error is C2016: '=' : left operand must be l-value.

I am a C++ newbie, so I googled the error and see that that error message means that the left hand operand is not assignable. Yet the left hand sides are all pointers and pointers can be assigned to other pointers.

So I'm not sure what I'm doing wrong. Can someone please help me out?

Thanks in advance.

jbu

+2  A: 

swap(src,dest) should do the trick.

As for what you're doing wrong: src.get() gives you a copy of the pointer from the shared_array object, so even if you could assign to it, it wouldn't change the shared_array itself. You could assign one array to another, like this:

boost::shared_array<unsigned char> temp = src;
src = dest;
dest = temp;

but swap is more efficient, and guaranteed not to throw an exception.

Mike Seymour
Looking at swap(), it says it "exchanges the contents" of src and dest. This sounds like it's actually memcpying memory. I really want just the pointers to be swapped, not the contents swapped.
jbu
Why do you need this?
Mykola Golubyev
@jbu: it "exchanges the contents of the two smart pointers" (i.e. swaps the pointers and anything else in the smart pointer implementation), but doesn't touch the memory they point to.
Mike Seymour
@Mykola: why do I need what?
Mike Seymour
+7  A: 

The correct way to swap two shared_arrays (or most other Boost shared pointer types) is to use the swap member function:

src.swap(dest);

This swaps the pointers and reference counts used by each of the shared_arrays:

void swap(shared_array<T> & other)
{
    std::swap(px, other.px);
    pn.swap(other.pn);
}
James McNellis
my comment to the other guy who suggested swap(): Looking at swap(), it says it "exchanges the contents" of src and dest. This sounds like it's actually memcpying memory. I really want just the pointers to be swapped, not the contents swapped.
jbu
I think this does the trick. Thanks! Maybe it was only me, but that "exchanges the contents" comment is a little confusing.
jbu
It's confusing, but `swap` functions are usually written to be as efficient as possible, and such that they can't throw exceptions.
David Thornley
Can't you use the idiomatic `using std::swap; swap(src,dest);` and let ADL pick up the correct `swap` version ? I would be astonished that a `Boost` library did not provide a free-standing version in the class file...
Matthieu M.
You can do that as well; it calls `src.swap(dest)`.
James McNellis
+2  A: 

For your original problem: just use the swap() method of boost::shared_array. (Don't be afraid, that will not memcpy anything.)

The pointer returned by get() is not an l-value in your case, because you want to change value of the pointer itself, not the data it is pointing to. So you'd need a reference to the pointer here.

Steffen