Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Smart pointers -- Types/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <string>
#include <memory>


class Entity
{
public :
Entity()
{
std::cout << "Created Entity" << std::endl;
}

~Entity()
{
std::cout << "Destroyed Entity" << std::endl;
}

void Print() {}

};

int main()
{
{
//std::unique_ptr<Entity> entity(new Entity());

std::unique_ptr<Entity> entity = std::make_unique<Entity>(); //prefferd to call make unique to avoid constructor exceptions[ptr with no ref and thats a memory leak]!

//std::unique_ptr<Entity> e0 = entity; copy constructor and assignment operator actually deleted!

entity->Print(); //EXACTLY SAME AS RAW POINTER
}


std::cin.get();
}



// SMART POINTERS ARE TO AUTO DELETE THE MEMORY ADDRESS THAT IS HEAP ALLOCATED!!
// AND SOME ARE NOT GONNA MENTION NEW AND ALSO DELETE KEYWORDS!!
// SMART POINTERS ARE THE WRAPPER AROUND A REAL RAW POINTER
// MEMORY AUTO DELETES ACCORDING TO THE POINTER YOU USED
// UNQ POINTER IS A SCOPED PTR[YOU CANT COPY UNQ PTR] OR TWO UNQ PTRS POINTING TO SAME BLOCK OF MEMORY AND ONE OF THEM DIES IT FREES THE MEMORY
// THIS IS THE ONLY PTR AND THAT ONLY REF TO THAT PTR YOU ACTUALLY WANTS
// WAY OF WRITTING IS UNQ PTR HAS EXPLICIT CONSTRUCTOR AND FIRST WITH CLASS NAME AND GIVING A OBJECT NAME AND KEEPING IT A CONSTRUCTOR
48 changes: 48 additions & 0 deletions Smart pointers -- Types/shared.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <string>
#include <memory>


class Entity
{
public :
Entity()
{
std::cout << "Created Entity" << std::endl;
}

~Entity()
{
std::cout << "Destroyed Entity" << std::endl;
}

void Print() {}

};

int main()
{
{
{
std::shared_ptr<Entity> e0;
{


std::shared_ptr<Entity> sharedEntity = std::make_shared<Entity>();
e0 = sharedEntity;
// std::shared_ptr<Entity> e0 = std::make_shared<Entity>();
/*Here this is preferrable due to this shared ptr stores memory in control block
so if you do allocation it allocate first new Entity and then construct the shared
ptr in control block which is 2 allocations*/

} //not yet destroyed
}
}


std::cin.get();//after this memory frees after all shared ptr dies
}
//shared ptr based on ref counting
//when shared ptr gives to other shared ptr countis 2
//when all ref summation is 0 the memory frees

43 changes: 43 additions & 0 deletions Smart pointers -- Types/weak.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <iostream>
#include <string>
#include <memory>


class Entity
{
public :
Entity()
{
std::cout << "Created Entity" << std::endl;
}

~Entity()
{
std::cout << "Destroyed Entity" << std::endl;
}

void Print() {}

};

int main()
{
{
{
std::weak_ptr<Entity> e0;
{
std::shared_ptr<Entity> sharedEntity = std::make_shared<Entity>();
// std::weak_ptr<Entity> weakEntity = sharedEntity;//copy Entity but not increase the ref counting
e0 = sharedEntity;

}
}//DESTROYED ENTITY
}


std::cin.get();
}

//shared ptr to shared ptr ref count increases
//shared ptr to weak ptr ref count remain same like you have Entities but you dont take ownership[it wont keep you alive]
//try to use unq ptr and then shared always