thrust

◆ malloc() [2/2]

template<typename T , typename DerivedPolicy >
__host__ __device__ pointer<T,DerivedPolicy> thrust::malloc ( const thrust::detail::execution_policy_base< DerivedPolicy > &  system,
std::size_t  n 
)

This version of malloc allocates typed uninitialized storage associated with a given system.

Parameters
systemThe Thrust system with which to associate the storage.
nThe number of elements of type T which the storage should accomodate.
Returns
If allocation succeeds, a pointer to an allocation large enough to accomodate n elements of type T; a null pointer otherwise. The pointer must be deallocated with thrust::free.
Template Parameters
DerivedPolicyThe name of the derived execution policy.
Precondition
DerivedPolicy must be publically derived from thrust::execution_policy<DerivedPolicy>.

The following code snippet demonstrates how to use malloc to allocate a range of memory to accomodate integers associated with Thrust's device system.

#include <thrust/memory.h>
...
// allocate storage for 100 ints with thrust::malloc
const int N = 100;
thrust::device_system_tag device_sys;
thrust::pointer<int,thrust::device_system_tag> ptr = thrust::malloc<int>(device_sys, N);
// manipulate memory
...
// deallocate ptr with thrust::free
thrust::free(device_sys, ptr);
See also
free
device_malloc