Boost logo

Boost Users :

From: bjorn.karlsson_at_[hidden]
Date: 2003-03-25 08:18:13


> From: Blake D [mailto:BlakeTNC_at_[hidden]]

> I am a new boost user, and I'm trying to convert a large
> project to use the
> smart pointers. I have a use for shared_array buy the memcpy
> line from the
> following code snippet(, and all memcpy's) throw an exception with the
> shared_arrays and I don't know why. Am I doing something
> obvious wrong?

[snip]

> boost::shared_array<uint8> p_data(new uint8[data_length]);

The above declaration of a local variable called p_data is probably not what
you intended (you want to initialize the member variable p_data). If you
cannot (or won't) perform the initialization in the constructor initializer,
you'll need to reset the shared_array like so:

  p_data.reset(new uint8[data_length]);

Or, in the initializer:

  c_Packet::c_Packet (const c_Packet & r_packet) : p_data(new
uint8[r_packed.data_length]) {
    data_length = r_packet.data_length;
    ...
  }
...and then perform the memcpy.

By the way, you are actually sharing this data, right? If not, scoped_array
would probably be a better choice for managing resource lifetime.

Bjorn Karlsson


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net