GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM
  • C library:
  • <cassert> (assert.h)
  • <cctype> (ctype.h)
  • <cerrno> (errno.h)
  • <cfenv> (fenv.h)
  • <cfloat> (float.h)
  • <cinttypes> (inttypes.h)
  • <ciso646> (iso646.h)
  • <climits> (limits.h)
  • <clocale> (locale.h)

This view

  • Original page
  • New address

GeezerVison is showing cplusplus.com — std::allocate_shared

  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM

C++

  • Tutorials
  • Reference
  • Articles
  • Forum

Reference

  • C library:

    • <cassert> (assert.h)
    • <cctype> (ctype.h)
    • <cerrno> (errno.h)
    • C++11
      <cfenv> (fenv.h)
    • <cfloat> (float.h)
    • C++11
      <cinttypes> (inttypes.h)
    • <ciso646> (iso646.h)
    • <climits> (limits.h)
    • <clocale> (locale.h)
    • <cmath> (math.h)
    • <csetjmp> (setjmp.h)
    • <csignal> (signal.h)
    • <cstdarg> (stdarg.h)
    • C++11
      <cstdbool> (stdbool.h)
    • <cstddef> (stddef.h)
    • C++11
      <cstdint> (stdint.h)
    • <cstdio> (stdio.h)
    • <cstdlib> (stdlib.h)
    • <cstring> (string.h)
    • C++11
      <ctgmath> (tgmath.h)
    • <ctime> (time.h)
    • C++11
      <cuchar> (uchar.h)
    • <cwchar> (wchar.h)
    • <cwctype> (wctype.h)
  • Containers:

    • C++11
      <array>
    • <deque>
    • C++11
      <forward_list>
    • <list>
    • <map>
    • <queue>
    • <set>
    • <stack>
    • C++11
      <unordered_map>
    • C++11
      <unordered_set>
    • <vector>
  • Input/Output:

    • <fstream>
    • <iomanip>
    • <ios>
    • <iosfwd>
    • <iostream>
    • <istream>
    • <ostream>
    • <sstream>
    • <streambuf>
  • Multi-threading:

    • C++11
      <atomic>
    • C++11
      <condition_variable>
    • C++11
      <future>
    • C++11
      <mutex>
    • C++11
      <thread>
  • Other:

    • <algorithm>
    • <bitset>
    • C++11
      <chrono>
    • C++11
      <codecvt>
    • <complex>
    • <exception>
    • <functional>
    • C++11
      <initializer_list>
    • <iterator>
    • <limits>
    • <locale>
    • <memory>
    • <new>
    • <numeric>
    • C++11
      <random>
    • C++11
      <ratio>
    • C++11
      <regex>
    • <stdexcept>
    • <string>
    • C++11
      <system_error>
    • C++11
      <tuple>
    • C++11
      <type_traits>
    • C++11
      <typeindex>
    • <typeinfo>
    • <utility>
    • <valarray>

<memory>

  • classes

    • allocator
    • C++11
      allocator_arg_t
    • C++11
      allocator_traits
    • auto_ptr
    • auto_ptr_ref
    • C++11
      bad_weak_ptr
    • C++11
      default_delete
    • C++11
      enable_shared_from_this
    • C++11
      owner_less
    • C++11
      pointer_traits
    • raw_storage_iterator
    • C++11
      shared_ptr
    • C++11
      unique_ptr
    • C++11
      uses_allocator
    • C++11
      weak_ptr
  • enum classes

    • C++11
      pointer_safety
  • functions

    • C++11
      addressof
    • C++11
      align
    • C++11
      allocate_shared
    • C++11
      const_pointer_cast
    • C++11
      declare_no_pointers
    • C++11
      declare_reachable
    • C++11
      dynamic_pointer_cast
    • C++11
      get_deleter
    • C++11
      get_pointer_safety
    • get_temporary_buffer
    • C++11
      make_shared
    • return_temporary_buffer
    • C++11
      static_pointer_cast
    • C++11
      undeclare_no_pointers
    • C++11
      undeclare_reachable
    • uninitialized_copy
    • C++11
      uninitialized_copy_n
    • uninitialized_fill
    • uninitialized_fill_n
  • objects

    • C++11
      allocator_arg
  • Reference
  • <memory>
  • allocate_shared
function template
<memory>

std::allocate_shared

template <class T, class Alloc, class... Args>  shared_ptr<T> allocate_shared (const Alloc& alloc, Args&&... args);
Allocate shared_ptr
Allocates memory for an object of type T using alloc and constructs it passing args to its constructor. The function returns an object of type shared_ptr<T> that owns and stores a pointer to the constructed object (with a use count of 1).

This function uses alloc to allocate storage for the object. A similar function, make_shared uses ::new to allocate the storage instead.

Parameters

alloc
An allocator object.
Alloc is a type for which allocator_traits is well defined.
args
List of elements passed to T's constructor.
Args is a list of zero or more types.

Return Value

A shared_ptr object that owns and stores a pointer to a newly allocated object of type T.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// allocate_shared example
#include <iostream>
#include <memory>

int main () {
  std::allocator<int> alloc;    // the default allocator for int
  std::default_delete<int> del; // the default deleter for int

  std::shared_ptr<int> foo = std::allocate_shared<int> (alloc,10);

  auto bar = std::allocate_shared<int> (alloc,20);

  auto baz = std::allocate_shared<std::pair<int,int>> (alloc,30,40);

  std::cout << "*foo: " << *foo << '\n';
  std::cout << "*bar: " << *bar << '\n';
  std::cout << "*baz: " << baz->first << ' ' << baz->second << '\n';

  return 0;
}

Output:
*foo: 10
*bar: 20
*baz: 30 40


See also

make_shared
Make shared_ptr (function template)
shared_ptr::shared_ptr
Construct shared_ptr (public member function)
shared_ptr::operator=
shared_ptr assignment (public member function)
allocator_traits
Allocator traits (class template)
Home page | Privacy policy
© cplusplus.com, 2000-2026 - All rights reserved - v3.3.4s
Spotted an error? contact us
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.