Chapter 25. DbstlDbt
Prev |  | Next

## Chapter 25. DbstlDbt

You can persist all bytes in a chunk of contiguous memory by constructing an DbstlDbt object A(use malloc to allocate the required number of bytes for A.data and copy the bytes to be stored into A.data, set other fields as necessary) and store A into a container, e.g.

db_vector<DbstlDbt>, this stores the bytes rather than the object A into the underlying database. The DbstlDbt class can help you avoid memory leaks, so it is strongly recommended that you use DbstlDbt rather than Dbt class.

DbstlDbt derives from Dbt class, and it does an deep copy on copy construction and assignment --by calling malloc to allocate its own memory and then copying the bytes to it; Conversely the destructor will free the memory on destruction if the data pointer is non-NULL. The destructor assumes the memory is allocated via malloc, hence why you are required to call malloc to allocate memory in order to use DbstlDbt .

DbstlDbt simply inherits all methods from Dbt with no extra new methods except the constructors/destructor and assignment operator, so it is easy to use.

In practice you rarely need to use DbstlDbt or Dbt because dbstl enables you to store any complex objects or primitive data. Only when you need to store raw bytes, e.g. a bitmap, do you need to use DbstlDbt .

Hence, DbstlDbt is the right class to use to store any object into Berkeley DB via dbstl without memory leaks.

Don't free the memory referenced by DbstlDbt objects, it will be freed when the DbstlDbt object is destructed.

Please refer to the two examples using DbstlDbt in TestAssoc::test_arbitrary_object_storage and TestAssoc::test_char_star_string_storage member functions, which illustrate how to correctly use DbstlDbt in order to store raw bytes.

This class handles the task of allocating and de-allocating memory internally. Although it can be used to store data which cannot be handled by the DbstlElemTraits class, in practice, it is usually more convenient to register callbacks in the DbstlElemTraits class for the type you are storing/retrieving using dbstl.

#### Public Members

Member | Description
--- | ---
DbstlDbt | Construct an object with an existing chunk of memory of size1 bytes, refered by data1,.
~DbstlDbt | The memory will be free'ed by the destructor.
operator= | The memory will be reallocated if neccessary.

#### Group

Dbstl Helper Classes

## DbstlDbt

### Function Details

    DbstlDbt(void *data1,
        u_int32_t size1)

Construct an object with an existing chunk of memory of size1 bytes, refered by data1,.

    DbstlDbt()

    DbstlDbt(const DbstlDbt &d)

This copy constructor does a deep copy.

### Class

DbstlDbt

Prev |  | Next
_DB_STL_value | Home | ~DbstlDbt