kcc__
kcc__

Reputation: 1648

Boost Shared Pointer array

I am new to boost and I came across the boost pointer.

float *value = new float[9]; 
value[0] = 5; ...

The above is my initial c++ code.

I converted the above to boost shared pointer

boost::shared_ptr<float> value (new float);

But when I try to add to value it gives me error that i cant use operator[].

I guess this is too basic, but can I get some info on how to add values to memory pointed by the boost pointer.

Upvotes: 2

Views: 1096

Answers (1)

Bathsheba
Bathsheba

Reputation: 234635

boost::shared_ptr<> is not designed to be used to hold an array that's "decayed" to a pointer.

For starters, it wouldn't delete the memory correctly on destruction (it would call delete rather than delete[].) You'd have to build your own deallocator to circumvent this. Possible but tedious.

Keep things simple: use std::vector<float>. In the current standard, the underlying data are guaranteed to be contiguous, and data() can be used to extract the underlying array.

Upvotes: 3

Related Questions