3
votes

i try to learn how to use smart pointers. I use for the long time normal pointers and i think i need some upgrade of my skills.

I make some research, i understand some aspects of smart pointers, but i try to implement in a clear project to see how smart pointers work. I try:

#include <iostream>
#include <array>
#include <memory>

class Entity
{
public:
    Entity()
    {
        std::cout << "Entity called!" << std::endl;
    }
    ~Entity()
    {
        std::cout << "Entity destroyed!" << std::endl;
    }
    void print() { std::cout << "Message!"; }
};

int main()
{
    std::shared_ptr<int>f1(new int[100]);

    f1.get()[1] = 1;
    std::cout << f1.get()[1];
}

all good, message it's print. But when i try:

#include <iostream>
#include <array>
#include <memory>

class Entity
{
public:
    Entity()
    {
        std::cout << "Entity called!" << std::endl;
    }
    ~Entity()
    {
        std::cout << "Entity destroyed!" << std::endl;
    }
    void print() { std::cout << "Message!"; };
};

int main()
{
    std::shared_ptr<Entity>f1(new Entity[100]);

    f1.get()[1].print();
}

I get this error: [img]https://i.imgur.com/U30gTgC.png[/img]

next:

int main()
{
    std::shared_ptr<Entity>f1(new Entity[100]);

    (f1.get() + 1)->print();
}

same error.

I try to use std::make_shared:

#include <iostream>
#include <array>
#include <memory>

class Entity
{
public:
    Entity()
    {
        std::cout << "Entity called!" << std::endl;
    }
    ~Entity()
    {
        std::cout << "Entity destroyed!" << std::endl;
    }
    void print() { std::cout << "Message!"; };
};

int main()
{
    std::shared_ptr<Entity>f1 = std::make_shared<Entity>();

    f1->print();
}

everything it's ok.

I try to alocate continue memory with int:

#include <iostream>
#include <array>
#include <memory>

class Entity
{
public:
    Entity()
    {
        std::cout << "Entity called!" << std::endl;
    }
    ~Entity()
    {
        std::cout << "Entity destroyed!" << std::endl;
    }
    void print() { std::cout << "Message!"; };
};

int main()
{
    std::shared_ptr<int>f1 = std::make_shared<int>(100);

    f1.get()[1] = 10;
    std::cout << f1.get()[1];
}

message it's printed, output: 10 but error: https://i.imgur.com/UPu7VZo.png

i try in another way:

int main()
{
    std::shared_ptr<int>f1 = std::make_shared<int>(100);

    *(f1.get() +1) = 10;
    std::cout << *(f1.get() + 1);
}

same error.

std::shared_ptr<Entity[]>f1 = std::make_shared<Entity[]>(new Entity[100]);

i have error...

What i try to make something like this:

int main()
{
    Entity* f1 = new Entity[100];

    f1[0].print();
    f1[1].print();
    f1[2].print();

}

but i want to use smart pointers.

for int i want to make some assigned value like this:

int main()
{
    int* f1 = new int[100];

    f1[0] = 14;
    f1[1] = 20;
    f1[2] = 5;
}

How can i make something like this with smart pointers. I want to use: std::make_shared(new Entity[100]) or something like this.

I can try with library like vector or array but i don't want to use this library for the moment. I want to keep my code clear for the moment. After i understand 100% smart pointers i will use array and vector library

2
Error messages are text. Paste the text here; don't link to an image of text. - tenfour
You should use std::shared_ptr<Entity[]> or the container will try to call delete instead of delete[] which is wrong stackoverflow.com/questions/1553382/is-delete-equal-to-delete - Cory Kramer
Does this answer your question? shared_ptr to an array : should it be used? - a1ezh
make_shared creates ("makes") a shared object. It does not take a pointer to an object and "make it shared". - molbdnilo
While std::shared_ptr<Entity[]> is possible, std::vector<Entity> and std::array<Entity, 100> are more common. Smart pointers are only one of the replacements of dumb pointers. The standard containers are another replacement. - MSalters

2 Answers

5
votes

Not

std::shared_ptr<Entity[]> f1 = std::make_shared<Entity[]>(new Entity[100]);

but

std::shared_ptr<Entity[]> f1 = std::make_shared<Entity[]>(100);

or even simpler

auto f1 = std::make_shared<Entity[]>(100);

When you use make_shared you should not use new. One of the points of make_shared is to allocate memory more efficiently that it would be if you used new.

Note: the array form of make_shared requires C++20.

2
votes

The code sample that should work for you is:

int main() {
    std::shared_ptr<Entity[]>f1(new Entity[100]);

    f1.get()[1].print();
}

As for make_shared, you're out of luck - the current CPP standard, 17, doesn't seem to support it (see https://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared) However, make_shared is only used to eliminate a race condition in allocation and can be implemented manually or just omitted. I encourage reading more about it.