//used to save memory by moving ptr 
//delete memery aotomatically exit a loop
//no memeory leaks

#include <iostream>
#include <memory>

int functionA() {
    std::unique_ptr<int> ptr (new int(5));
    std::cout << "Inside functionA, value of the int object: " << *ptr << std::endl;
    // ptr is accessible here
    return *ptr;
}

int main() {
    std::cout << "Entering functionA" << std::endl;
    int a = functionA();
    std::cout << "Exiting functionA with value : " << a << std::endl;
    // ptr is not accessible here
    std::cout << "Back in main function" << std::endl;
    return 0;
}

/*
You are correct that in the code you provided, the unique pointer ptr is accessible
outside of the functionA scope, because the functionA returns the value that the
pointer is pointing to. This is different from the previous examples where I 
explained that the unique pointer is only accessible within the scope in which it
is defined and it is deleted when that scope is exited.

When a unique pointer is used to return a value, the unique pointer is destroyed 
when the function exits, but the memory it points to is not deleted, because the
memory is returned to the caller, and the caller can use the returned value.

It's important to note that returning a unique pointer by value is not recommended,
because it can lead to confusion about the ownership of the object, and it's better
to use a smart pointer such as shared_ptr or weak_ptr that are designed to handle 
shared ownership.

In the example you provided, the functionA returns the value of the int object
pointed to by the unique pointer ptr, and it is stored in the a variable in main
function. And the unique pointer ptr is destroyed when the functionA execution
completes, but the memory that it points to is still accessible via the a variable
and it's not deleted.

I apologize for any confusion caused by my previous explanation.
*/

#if 0

#include <iostream>
#include <memory>

std::unique_ptr<int> functionA() {//here we are moving the variavle ptr not coping
    std::unique_ptr<int> ptr (new int(5));
    return ptr;
}

int main() {
    std::unique_ptr<int> ptr2 = functionA();
    std::cout << "Value of the int object: " << *ptr2 << std::endl;
    // ptr2 owns the object now
    return 0;
}//scope rule works only here return type is unique

#include <iostream>
#include <memory>

int main() {
    std::unique_ptr<int> ptr (new int(5));
    std::cout << "Value of the int object: " << *ptr << std::endl;
    *ptr = 6;
    std::cout << "New value of the int object: " << *ptr << std::endl;
    return 0;
}
#endif

/*
A unique pointer in C++ is a type of smart pointer that exclusively owns and manages
the lifetime of the object it points to. This means that a unique pointer can only 
be moved, not copied, and that the object it points to will be automatically deleted
when the unique pointer goes out of scope.

Here is an example of how to use a unique pointer in C++:

Copy code
std::unique_ptr<int> ptr = std::make_unique<int>(5);
std::cout << *ptr << std::endl;   // output: 5
*ptr = 6;
std::cout << *ptr << std::endl;   // output: 6

In this example, ptr is a unique pointer that owns an int object with the value of 5.
You can use *ptr to access the value of the object just like a raw pointer, but the 
object will be automatically deleted when the ptr goes out of scope.

We use unique pointers because they automatically manage the memory they point to,
which helps prevent memory leaks and other memory-related errors. They also provide
a way to transfer ownership of an object without the risk of copy constructor. They
are used to avoid the problem of multiple ownership of the same object, which can
lead to memory leaks, double-free issues and other problems.

Additionally, unlike a raw pointer, unique pointers cannot be reassigned, which
makes it harder to shoot yourself in the foot by accident.

In C++11 and C++14, unique_ptr is the only smart pointer available to manage
the ownership of an object , and it is recommended to use unique_ptr instead of
raw pointer, if you are not sure about the ownership.

*/

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: