New members of C++ (8)

        We have previously learned that malloc is used to dynamically apply for memory in C language . In C++ , dynamic memory application is performed through the new keyword . Dynamic memory allocation in C++ is based on type , and the delete keyword is used for memory release .         Variable application: Type* pointer = new Type; delete pointer ;         Array application: Type* pointer = new Type[N]; delete[] pointer ;

        Let's take the code as an example to see dynamic memory allocation in C++

#include <stdio.h>

int main(int argc, char *argv[])
{
    int* p = new int;
    
    *p = 5;
    *p = *p + 10;
    
    printf("p = %p\n", p);
    printf("*p = %d\n", *p);
    
    delete p;
    
    p = new int[5];
    
    for(int i=0; i<5; i++)
    {
        p[i] = i + 1;
        
        printf("p[%d] = %d\n", i, p[i]);
    }
    
    delete[] p;
    
    return 0;
}

        Let's look at the compilation results

picture.png

        Let's talk about the difference between the new keyword and the malloc function: 1. The new keyword is a part of C++, and malloc is a function provided by the C library; 2. New allocates memory in units of specific types, and malloc takes bytes as 3. New can be initialized when applying for a single type variable, malloc does not have the characteristics of memory initialization .

        Let's take the code as an example to see how the new keyword is initialized

#include <stdio.h>

int main(int argc, char *argv[])
{
    int* pi = new int(1);
    float* pf = new float(2.0f);
    char* pc = new char('c');
    
    printf("*pi = %d\n", *pi);
    printf("*pf = %f\n", *pf);
    printf("*pc = %c\n", *pc);
    
    delete pi;
    delete pf;
    delete pc;
    
    return 0;
}

        Let's see if the compilation result is really initialized as our code wrote.

picture.png

        Let's talk about a new concept, namespaces in C++ . There is only one global scope in the C language , all global identifiers share the same scope, and conflicts between identifiers may occur . The concept of namespace is proposed in C++. The namespace divides the global scope into different parts; identifiers in different namespaces can have the same name without conflict; namespaces can be nested within each other; the global scope is also called default naming space .

        C++ namespaces are defined as follows:

namespace Name
{
    namespace Internal
    {
        /* ... */
    }
    
    /* ... */
}

        The use of C++ namespace: 1. Use the entire namespace: using namespace name; 2. Use the variables in the namespace: using namespace::variable; 3. Use the variables in the default namespace: ::variable .

        Let's take the code as an example to see how namespaces are used

#include <stdio.h>

namespace First
{
    int i = 0;
}

namespace Second
{
    int i = 1;
    
    namespace Internal
    {
        struct P
        {
            int x;
            int y;
        };
    }
}

int main(int argc, char *argv[])
{
    using namespace First;
    using Second::Internal::P;
    
    printf("First::i = %d\n", i);
    printf("Second::i = %d\n", Second::i);
    
    P p = {2, 3};
    
    printf("p.x = %d\n", p.x);
    printf("p.y = %d\n", p.y);
    
    return 0;
}

        We first define the space First, and define the variable i in it. The space Second is defined, the variable i is also defined in it, and another space is nested. Is it legal to define the same variable repeatedly like this? Look at what is printed on lines 27 and 28.

picture.png

        We see that the compilation passes, and it runs perfectly. Through the study of new keyword and namespace, the summary is as follows: 1. C++ has built-in special keywords for dynamic memory allocation; 2. Dynamic memory allocation in C++ can be initialized at the same time; 3. Dynamic memory allocation in C++ is Based on the type; 4. The namespace concept in C++ is used to resolve name conflicts.


        Welcome to learn C++ language together, you can add me QQ: 243343083 .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325382368&siteId=291194637