What are the resources managed by smart pointer while their memory are not allocated by new?

I continue to see "resource" on the contrary to "dynamic memory" from C++ Primer 5th:

By default, a pointer used to initialize a smart pointer must point to dynamic memory because, by default, smart pointers use delete to free the associated object. We can bind smart pointers to pointers to other kinds of resources. However, to do so, we must supply our own operation to use in place of delete.


If you use a smart pointer to manage a resource other than memory allocated by new, remember to pass a deleter.

What are the resources mentioned here?

And is it indicating that :

There is(are) other way(s) to allocate dynamic memory compared to using new. ( Aren't all dynamic memory essentially allocated by using new?)


Update:

The reason why I am unclear about the quoted text is that I was thinking wrong about something, which it's that I had been thinking of smart pointer is only invented for/related to dynamic memory management. So the example uses smart pointer to manage a non-dynamic memory stuff makes me confused.

A good explanation from a senior:

The smart pointer doesn't care at all about something being dynamic memory as such. It's just a way to keep track of something while you need it, and destroy that something when it goes out of scope. The point of mentioning file handles, network connections, etc., was to point out that they're not dynamic memory, but a smart pointer can manage them just fine anyway.

3 answers

  • answered 2018-07-11 05:52 tkausl

    If you use a smart pointer to manage a resource other than memory allocated by new, remember to pass a deleter.

    What are the resources mentioned here?

    A resource can be anything. Anything that can be aquired and need to be released afterwards. You (your textbook) even mention one exampe: TCP (or other) connections. They need to be closed once they are not needed anymore.

    And is it indicating that :

    There is(are) other way(s) to allocate dynamic memory compared to using new.

    No it's not, it means that smart pointers can manage resources not aquired through new and not deleteable through delete.

    ( Aren't all dynamic memory essentially allocated by using new?)

    There are other ways like malloc, but usually you should always use new in C++.

    Secondly, I wanna ask why and when do I need to supply our own deleter? Isn't keyword delete sufficient enough?

    For memory allocated with new, yes. For other resources, no. You can't delete a socket descriptor.


    You might want to google the term RAII which describes this concept of acquiring and releasing resources automatically in C++.

  • answered 2018-07-11 06:34 bartop

    Imagine following situation:

    SomeSystemHandle createHandle();
    void freeHandle(SomeSystemHandle *);
    
    std::unique_ptr<SomeSystemHandle, void(*)(SomeSystemHandle *)> handle(
        createHandle(), 
        freeHandle
    );
    

    As You can see unique_ptr is not managing memory per se. It manages some handle, about which it is oblivious about. This is an example of non memory resource that is mentioned in Your question.

  • answered 2018-07-11 12:07 Red.Wave

    concrete useful examples are C API with OO design. Here comes one such:

    std::shared_ptr<std::FILE> myfile{std::fopen("myfile.dat","r"),std::fclose};