gogoWebsite

Two ways to instantiate c++ class: the difference between new and not new

Updated to 2 days ago

The difference between creating a class object in new and not new

A a;

A * a = new a();

Both of the above methods can realize instantiation of classes. The difference between new is:

  1. The former allocates memory on the stack, while the latter is dynamic memory allocation. There is no difference in general applications, but dynamic memory allocation will increase the controllability of objects.
  2. Allocate memory in the stack without adding new
  3. Use new for large programs, apply directly for small programs
  4. Just allocate the object in the stack memory
  5. New must be deleted, and the system will automatically recycle memory without using new

At first, when I first learned C++, I was not used to using new. Later, I looked at foreigners' programs and found that almost all of them were used. Thinking about it, the difference was not too big, but in larger project designs, sometimes not using new will indeed bring many problems. Of course, this is all related to the usage of new. Create a class object in new. After using it, you need to use delete to delete, which is similar to applying for memory. Therefore, new is sometimes not suitable. For example, in frequent calls, using local new class objects is not a good choice. Using global class objects or an initialized global class pointer seems to be more efficient.

1. The difference between creating a class object in new and not new
Below are some of the characteristics of creating new class objects that I have summarized:

  • Create a new class object requires pointer reception, initialization in one place, and use in multiple places.
  • New class object needs to be deleted after using it
  • Create new objects directly using heap space, but not locally
  • New defines the class object and uses stack space
  • New object pointers are widely used, such as function return value, function parameters, etc.
  • Frequent calls are not suitable for new, just like new requests and frees memory.

2. Create class object instances in new
1. Example of creating class object in new:

CTest* pTest = new CTest();

delete pTest;

pTest is used to receive pointers of class objects.

Without new, use the class definition declaration directly:

CTest mTest;

This creation method does not require manual release after use, and this class of destructor will be automatically executed. The object applied for new will only execute the destructor when delete is called. If the program exits without delete, it will cause a memory leak.

2. Only define class pointers
This is very different from declaring objects without new. Class pointers can be defined first, but class pointers are just general pointers, and any memory space is allocated to the class object before new. for example:

CTest* pTest = NULL;

However, class objects created using the normal method have allocated memory space at the beginning of creation. If the class pointer has not been initialized by the object, it does not need to release delete.

3. New object pointer as function parameter and return value
Here is an example, not very rigorous. It mainly illustrates that the class pointer is used as a return value and parameter.

class CTest {  public:   int a;  };   
class CBest {  public:   int b;  };    
CTest* fun(CBest* pBest) 
{  
  CTest* pTest = new CTest();
     pTest->a = pBest->b;  
 return pTest;  
}    
int main() 
{  
 CBest* pBest = new CBest();   
 CTest* pRes= fun(pBest);      
 if(pBest!=NULL)    
delete pBest;  
 if(pRes!=NULL)   
 delete pRes ; 
  return -1; 
}

Heap space Stack space

References:
/wujiangguizhen/article/details/30504777