Programming learning | c++ three ways to create objects

There is no difference between the first and the second, an implicit call and an explicit call, both of which allocate memory in the stack in the process virtual address space, while the third uses new and allocates memory in the heap. , And the allocation and release of the memory in the stack is managed by the system, and the allocation and release of the memory in the heap must be manually released by the programmer.

When using the third method, you must pay attention to the following issues:

New to create a class object requires pointer reception, one initialization, and multiple use

The new created class object needs to be destroyed after use

New creates objects directly use heap space, and locally does not use new to define class objects to use stack space

The new object pointer has a wide range of uses, such as function return values, function parameters, etc.

Frequent call occasions are not suitable for new, just like new application and release of memory

The size of the stack is much smaller than the size of the heap

The stack is a data structure provided by the machine system, and the computer will provide support for the stack at the bottom level: it allocates special registers to store the address of the stack, and pushes and pops the stack to have special instructions to execute, which determines the efficiency of the stack is relatively high. The heap is provided by the C/C++ function library, and its mechanism is very complicated.

For example, in order to allocate a piece of memory, the library function will search the heap memory for enough space available in the heap memory according to a certain algorithm (for specific algorithms, please refer to the data structure/operating system). If there is not enough space (maybe due to memory fragmentation) many),

It is possible to call system functions to increase the memory space of the program data segment, so that there is a chance to allocate enough memory, and then return. Obviously, the efficiency of the heap is much lower than the stack

#include <iostream>

using namespace std;

class A

{

private:

    int n;

public:

    A(int m):n(m)

    { }

    ~A(){}

};

int main()

{

    A a(1);  //栈中分配

    A b = A(1);  //栈中分配

    A* c = new A(1);  //堆中分配

  delete c;

    return 0;

}

 


In addition, if you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster! I may be able to help you here~

UP has uploaded some video tutorials on learning C/C++ programming on the homepage. Those who are interested or are learning must go and take a look! It will be helpful to you~

Sharing (source code, actual project video, project notes, basic introductory tutorial)

Welcome partners who change careers and learn programming, use more information to learn and grow faster than thinking about it yourself!

Programming learning:

Programming learning:

 

Guess you like

Origin blog.csdn.net/weixin_45713725/article/details/115082877