Claudiu Andries
Claudiu Andries

Reputation: 53

Smart Pointers(shared_ptr)

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.sstatic.net/hMDIZ.png

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

Upvotes: 3

Views: 201

Answers (2)

john
john

Reputation: 88027

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.

Upvotes: 5

NadavS
NadavS

Reputation: 777

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.

Upvotes: 2

Related Questions