当前位置:网站首页>New keyword learning and summary

New keyword learning and summary

2022-04-23 16:54:00 The interview was rejected 10000 times

new Operator

 Reviewing c And review c++ in , Always put new Keyword to simply allocate memory , Obviously not enough .
 First of all, I learned from two blogs .  

new Create objects ,

new Create an array of object pointers ,

new

《c++ primer plus》

  1. The real use of pointers is Dynamic memory allocation , Allocate unnamed memory space during runtime
  2. typeName* pointer_name = new typeName;
    typeName Tell the compiler , Used to specify what kind of ( What size ) Memory space and what kind of pointer to declare
  3. new and Delete Must be used together , Release Memory requested by heap or free storage

Static binding and Dynamic linking

Define static binding
: In large data , For example, array , If you create memory at the time of declaration , No use , It always takes up memory , Like this in Allocating memory during compilation is called static chaining

> int tacos[10];

Define dynamic binding
: Use new when , If you need an array at run time , Specify the length of the array to allocate memory , This kind of Determining memory space during runtime is called dynamic binding

int* pz = new int[10];
delete [] pz;

In the actual project , Some frequently used memory space , It doesn't release , Initialize after use , Wait until the assignment is activated next time and use

new and malloc The difference between

  1. malloc and free yes c Standard library functions for languages ,new and delete It's the operator
  2. Why add new and delete?
    In non built-in data types (class class ) In terms of objects c++ Built in type , You need to call the constructor and destructor .
    new Call the constructor of the object while creating the object in memory .
    delete Call the memory destructor when cleaning up memory .( It's very important )
  3. By default new[] Allocated memory , Give it to delete[] To release
  4. Default operator new() Allocated memory , Give it to operator delete To release

new Create objects

class Person {
    int age ;

public:
   Person(/* args */){
          age = 0;
          cout << "  Default constructor  " << endl;
   } ;

   Person(int val):age(val)   { cout << "  Constructor with parameters  " << endl; };
   int  getPerson() const {  return age; };
   ~Person(){};
 };
int main()
{
  int ret = 0;
  Person tep = Person();  //  The stack area , Default structure 

  Person* pa =  new Person;  // Without brackets , Heap area  , Default structure 
  ret = pa->getPerson();   
  delete pa;

  Person* pc =  new Person(); //  Bracketed , Heap area , Default structure  , It is not the default parameter construction with parentheses 
  ret = pc->getPerson();
  delete pa;

  Person* pb =  new Person(100); //  There are parametric structures 
  ret = pb->getPerson();
  delete pb;
}

new Create an array of pointers to the object

   When looking at the project code , Created a TcpScoket[m_nSocketNum] Object array for , Every element is TcpScoke object ,    
   So if you create an array of pointers , You can create these nodes in the heap ( When used in the project , None of this space will be released , Difference one   
   One on the stack , Just one on the pile ), use new  How to create a pointer array ?
    class Person{ };
    int main()
    {
      int nSize = 10;
      Person* pa = new Person[nSize];  //  Open up an array element for Person Of memory space ,

      //  open up Person  Pointer array of , How to open up 
      //  I can't write this as  new (Person*)[nSize] , Also can not   It's written in new Person(*[nSize])
      Person** pb = new Person*[nSize]; //  Opening up an array element is  (Person*) Of memory space 

      for( int i=0; i<nSize ;i++)
      {
          pb[i] = new Person();  //  to Person  Open up space 
      }

      // Release space 
      for( int i=0; i<nSize ;i++)
      {
            delete pb[i];
      }      
      delete[] pb;
    }

版权声明
本文为[The interview was rejected 10000 times]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231359046402.html