C ++ Lesson 10 --C ++ new member

This article learn from Ditai Software College Tangzuo Lin teacher of C ++ courses


  1. In the C language, dynamic memory allocation is done through a library function malloc (), C language itself does not support dynamic memory allocation, C ++ adds a new keyword dynamic memory allocation

  2. C is only one global scope, all identifiers share a global scope, it may be a conflict between identifiers. Thus put forward the concept of the role of C ++


Experiment 1: dynamic memory allocation in C ++
Experiment 2: Initialization dynamic memory (heap)
Experiment 3: name space used


Here Insert Picture Description

Dynamic memory allocation in C ++: Experiment 1

#include <stdio.h>

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

mhr@ubuntu:~/work/c++$ g++ 10-1.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
p = 0x23dfc20
*p = 15
p[0] = 1
p[1] = 2
p[2] = 3
p[3] = 4
p[4] = 5
p[5] = 6
p[6] = 7
p[7] = 8
p[8] = 9
p[9] = 10
mhr@ubuntu:~/work/c++$ 

Problem: p = new int [10] ; the size of the memory space are applying here is how much?
Answer: dynamic application space out will be more than actually needed it. The answer here is at least 40 bytes.

Here Insert Picture Description

Here Insert Picture Description

Experiment 2: Initialization of dynamic memory (heap space)

#include <stdio.h>

int main()
{
    int* pi = new int(1);
    // int* pa = 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;
}

mhr@ubuntu:~/work/c++$ g++ 10-2.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
*pi = 1
*pf = 2.000000
*pc = c
mhr@ubuntu:~/work/c++$ 

Here Insert Picture Description

Here Insert Picture Description

Here Insert Picture Description

Experiment 3: Use namespaces

#include <stdio.h>

namespace First
{
    int i = 0;
}

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

int main()
{
	//我们将要在 main()中使用 First 这个命名空间中的一切资源
    using namespace First;
	
	//我们将要在 main()中使用 Second命名空间中的Internal命名空间中的P这个标识符
    using Second::Internal::P;
    
    //直接通过名字访问i ,因为前面已经 提出了要使用 First 这个命名空间中的一切资源
    printf("First::i = %d\n", i);
	
	//通过 Second::i 访问 Second命名空间中的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;
}


mhr@ubuntu:~/work/c++$ g++ 10-3.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
First::i = 0
Second::i = 1
p.x = 2
p.y = 3
mhr@ubuntu:~/work/c++$ 

Here Insert Picture Description

Published 200 original articles · won praise 100 · views 80000 +

Guess you like

Origin blog.csdn.net/LinuxArmbiggod/article/details/104092595