new and delete overloads, templates

Keywords: C Back-end

new and delete

Tmp* p=new Tmp();------new operator(Not reloadable)
Request memory (call) operator new)Can overload
 Call construct


delete p ;-----delete operator((not reloadable)
Call destructor
 Memory release(operator delete)Can overload



void* operator new(size_t size)overloaded function 
{
    cout<<"void* operator new(size_t size)"<<endl;
    void* p=malloc(size);
    return p;
}


void operator delete(void* p)
{
    cout<<"void operator delete(void* p)"<<endl;
    free(p);
}

The resources on the heap are external resources

Apply for / release external resources - user status does not have permission, kernel status has permission

The process of memory request and release on the heap needs to be completed in kernel state -- switching from user state to kernel state

System calls: cout, printf, CIN, scanf (system calls are required for those calling external resources)

Memory pool: a large section of memory maintained by the user

Overloading new and delete is equivalent to leaving a modifiable port, which can specify the memory application and released memory pool

c + + built-in memory pool:_ Alloc

MFC compiler: a tool for detecting memory leaks when new and delete are globally overloaded
There is a global linked list. When new, the address will be added to the linked list. When delete, the address will be deleted from the linked list. After the program is executed, the data in the linked list will be printed. If any, there will be a memory leak

Template

Overload: same function name, different parameter list (static polymorphism, polymorphism at compile time, early binding)

The template cannot be run directly. The template will generate corresponding function instructions according to the usage during compilation

If the template is not compiled, the function instructions generated by the template will be compiled, and the syntax errors in the template will be compiled when the corresponding instructions are generated
The function template has the ability of type self push. You can use the function template without passing the template type parameter eg: compare (10,20) and int without writing

template<typename T>
bool compare(T a,T b)
{
    return a>b;
}

int main()
{
   compare(10,20);
   if(compare("aaa","bbb"))afferent const char*Will compare addresses directly
   {
      cout<<"aaa">"bbb"<<endl;
   } 
}


template<typename T>
void sort(T* arr,int len)
{
if(srr==NULL)return;
 for(int i=0;i<len-1;++i)
 {
   for(int j=i+1;j<len-i-1;++j)
   {
      T temp=arr[i];
      if(arr[j]<arr[i])
      {
        arr[i]=arr[j];
        arr[j]=tmp;
      }
 }
 
}


Specialization of function templates: higher than ordinary templates and lower than ordinary functions
It can be solved that the type of const char * is passed in. When there are some exceptional types, write code for this type

Special case template
template<>
bool compare(const char* a,const char* b)
{
   cout<<"bool compare(const char* a,const char* b) "<<endl;
   return strcmp(a,b);
}

Class template

If it is not compiled, the corresponding class code (instruction) will be generated according to the usage mode during compilation
The member method that is not used in the main function in the class template is not false, and it will be reported only if it is invoked in main.
When using a class template, you must add the parameters of the template type. You cannot deduce the type function yourself
The member methods of the class template can only be implemented in the header file or in the used cpp file, and cannot be implemented separately in. h and another. CPP (except the. Cpp file that writes the main function)
Because the class template generates corresponding instructions for the member methods to be used during compilation, the compiler only targets a single file. If the member methods of the class template are implemented in other files, and the files of the method used during compilation are not visible, the corresponding instructions cannot be generated, -- unresolved external symbols
In another file, it is not compiled because it is not used, so it cannot be found when linking

template<typename T>

class Arr
{
 private:
      T* arr;
      int _len;
      int _val_len;
public:
    Arr()
    {
       cout<<"Arr()"<<endl;
    }   
    void push_back(const T&val);
};
int main()
{
   Arr<int> arr;Type must be added, and it will not be deduced by itself
}


Specialization of class templates
template<>
class Arr<const char*>
{
private:
    const char** arr;
      int _len;
      int _val_len;
const char** arr;
  public:
   Arr()
    {
       cout<<"Arr<const char*>"<<endl;
    }   
    void push_back(const char*val);
Friend function template
template<typename TT>
friend ostream& operator<<(ostream& out,Arr<TT>& src);
};

When the member methods in the template are implemented outside the class, there are more than one T´╝îbecause Arr It's a template, plus T Is the class name
template<typedef T>
void Arr<T>::push_back(const T&val);

template<typedef T>
ostream& operator<<(ostream& out,Arr<T>& src)
{
   for(int i=0;i<src.size();++i)
   {   
        out<<src[i]<<" ";
   }
   cout<<endl;
}




int main()
{
   Arr<const char*> arr;
}

Posted by Fredix on Sun, 28 Nov 2021 06:10:33 -0800