I was implementing classes my_unique_ptr and my_shared_ptr that mimic the standard library smart pointers std::unique_ptr and std::shared_ptr to get to know it better.
When implementing the destructors I'm in this trouble to decide whether to use delete or delete[] to free the memory. As much as I read on SO and other sites, there is no portable way to know how many bytes were allotted by new[] (or whether new was used or new[]) (How many bytes were allocated by new?)
While implementing class my_unique_ptr I have no way of knowing how many bytes the user will request from the constructor , i.e.
Whether he will do my_unique_ptr<int> ptr1(new int)
or will he do
my_unique_ptr<int> ptr1(new int[5])
If there is a way please let me know!
Here is my class (simplified and without cpy/move constructors):
template<typename ValueType>
class my_unique_ptr {
    ValueType *internal_ptr = nullptr;
public:
    
    // Default constructor
    my_unique_ptr() {
       internal_ptr = nullptr;
    }
    // Paramterized constructor
    explicit my_unique_ptr(ValueType *ptr) {
       if (ptr)
        internal_ptr = ptr;
    }
    // Destructor
    ~my_unique_ptr() {
       
        // How do I know whether to use
           delete ptr;
        // or
           delete[] ptr;
        
     }
I read somewhere that the compiler keeps track of the argument of new[] which is then used by delete[] to free the memory correctly. Also read that "It is the responsibility of the programmer to match new with delete and new[] with delete[]". But how do I program this in my class so that it always matches the correct operators?
Sidenote:
Running valgrind while using delete everywhere instead of delete[] and passing more than 1 int (say with new int[5]) in my constructor, Valgrind says that all memory blocks were freed and there is no chance of leak! (although shows warnings like mismatched new[] with delete. Does this mean delete is successfully freeing all the 5 ints allocated by new[]? Please help.
I'm on Ubuntu 18.04 and using gcc 7.5.0.
 
     
    