# Db::set_malloc

`

###

    #include <db_cxx.h>

    extern "C" {
    	typedef void *(*db_malloc_fcn_type)(size_t);
    };
    int
    Db::set_malloc(db_malloc_fcn_type db_malloc);

# Description

Set the allocation function used by the Db methods to allocate memory in which to return key/data items to the application.

The DB_DBT_MALLOC flag, when specified in the Dbt object, will cause the Db methods to allocate and re-allocate memory which then becomes the responsibility of the calling application. See Dbt for more information.

On systems where there may be multiple library versions of malloc (notably Windows NT), specifying the DB_DBT_MALLOC flag will fail because the Db library will allocate memory from a different heap than the application will use to free it. To avoid this problem, the Db::set_malloc method can be used to pass Berkeley DB a reference to the application's allocation routine, in which case it will be used to allocate the memory returned when the DB_DBT_MALLOC flag is set.

The method specified must match the calling conventions of the ANSI C X3.159-1989 (ANSI C) library routine of the same name.

The Db::set_malloc interface may only be used to configure Berkeley DB before the Db::open interface is called.

The Db::set_malloc method either returns a non-zero error value or throws an exception that encapsulates a non-zero error value on failure, and returns 0 on success.

# Errors

EINVAL
  An invalid flag value or parameter was specified.

### Class

Db

# See Also

Db::close, Db::cursor, Db::del, Db::err, Db::fd, Db::get, Db::get_byteswapped, Db::get_type, Db::join, Db::key_range, Db::open, Db::put, Db::remove, Db::set_bt_compare, Db::set_bt_minkey, Db::set_bt_prefix, Db::set_cachesize, Db::set_dup_compare, Db::set_errcall, Db::set_errfile, Db::set_errpfx, Db::set_flags, Db::set_h_ffactor, Db::set_h_hash, Db::set_h_nelem, Db::set_lorder, Db::set_malloc, Db::set_pagesize, Db::set_paniccall, Db::set_q_extentsize, Db::set_realloc, Db::set_re_delim, Db::set_re_len, Db::set_re_pad, Db::set_re_source, Db::stat, Db::sync, Db::upgrade and Db::verify.`

Copyright Sleepycat Software