# Working with result sets

## SYNOPSIS

#include <libmemcached/memcached.h>

  Compile and link with -lmemcachedutil -lmemcached

typedef struct memcached_result_st memcached_result_st

memcached_result_st *memcached_result_create(memcached_st *ptr, memcached_result_st *result)
  Parameters:

    - ptr -- pointer to initialized `memcached_st` struct

    - result -- pointer to an `memcached_result_st` instance to initialize or nullptr to allocate a new instance

  Returns:

    pointer to initialized `memcached_result_st` instance

void memcached_result_free(memcached_result_st *result)
  Parameters:

    result -- pointer to initialized `memcached_result_st` struct

const char *memcached_result_key_value(memcached_result_st *result)
  Parameters:

    result -- pointer to initialized `memcached_result_st` struct

  Returns:

    the key value associated with the current result object

size_t memcached_result_key_length(const memcached_result_st *result)
  Parameters:

    result -- pointer to initialized `memcached_result_st` struct

  Returns:

    the key length associated with the current result object

const char *memcached_result_value(memcached_result_st *result)
  Parameters:

    result -- pointer to initialized `memcached_result_st` struct

  Returns:

    the result value associated with the current result object

char *memcached_result_take_value(memcached_result_st *result)
  Parameters:

    result -- pointer to initialized `memcached_result_st` struct

  Returns:

    the result value associated with the current result object

size_t memcached_result_length(const memcached_result_st *result)
  Parameters:

    result -- pointer to initialized `memcached_result_st` struct

  Returns:

    the result length associated with the current result object

uint32_t memcached_result_flags(const memcached_result_st *result)
  Parameters:

    result -- pointer to initialized `memcached_result_st` struct

  Returns:

    the flags associated with the current result object

uint64_t memcached_result_cas(const memcached_result_st *result)
  Parameters:

    result -- pointer to initialized `memcached_result_st` struct

  Returns:

    the cas associated with the current result object

memcached_return_t memcached_result_set_value(memcached_result_st *result, const char *value, size_t length)
  Parameters:

    - result -- pointer to initialized `memcached_result_st` struct

    - value -- byte array to set the value of the current result object to

    - length -- the length of `value` wothout any terminating zero

  Returns:

    `memcached_return_t` indicating success

void memcached_result_set_flags(memcached_result_st *result, uint32_t flags)
  Parameters:

    - result -- pointer to initialized `memcached_result_st` struct

    - flags -- a new value for the flags field

void memcached_result_set_expiration(memcached_result_st *result, time_t expiration)
  Parameters:

    - result -- pointer to initialized `memcached_result_st` struct

    - expiration -- a new value for the expiration field

## DESCRIPTION

`libmemcached` can optionally return a `memcached_result_st` which acts as a result object. The result objects have added benefits over the character pointer return values, in that they are forward compatible with new return items that future memcached servers may implement (the best current example of this is the CAS return item). The structures can also be reused, which will save on calls to malloc(3). It is suggested that you use result objects over char * return functions.

The structure of `memcached_result_st` has been encapsulated, you should not write code directly accessing members of the structure.

`memcached_result_create()` will either allocate memory for a `memcached_result_st` or will initialize a structure passed to it.

`memcached_result_free()` will deallocate any memory attached to the structure. If the structure was also allocated, it will deallocate it.

`memcached_result_key_value()` returns the key value associated with the current result object.

`memcached_result_key_length()` returns the key length associated with the current result object.

`memcached_result_value()` returns the result value associated with the current result object.

`memcached_result_take_value()` returns and hands over the result value associated with the current result object. You must call free(3) to release this value, unless you have made use of a custom allocator. Use of a custom allocator requires that you create your own custom free() to release it.

`memcached_result_length()` returns the result length associated with the current result object.

`memcached_result_flags()` returns the flags associated with the current result object.

`memcached_result_cas()` returns the cas associated with the current result object. This value will only be available if the server tests it.

`memcached_result_set_value()` takes a byte array and a size and sets the result to this value. This function is used for trigger responses.

`memcached_result_set_flags()` takes a result structure and stores a new value for the flags field.

`memcached_result_set_expiration()` takes a result structure and stores a new value for the expiration field (this is only used by read through triggers).

## RETURN VALUE

Varies, see particular functions. All structures must have `memcached_result_free()` called on them for cleanup purposes. Failure to do this will result in leaked memory.

## SEE ALSO

- memcached(1)

- C/C++ Client Library for memcached

- Converting Error Codes to Messages

- Use custom allocators for embedded usage