Details C++ dynamic memory allocation and namespaces

  • 2020-06-07 05:02:18
  • OfStack

1. Dynamic memory allocation in C++

Dynamic memory request via the new keyword Dynamic memory requests in C++ are type-based The delete key is used for memory release

C does not support dynamic memory allocation. It is implemented through malloc library functions. Some hardware may not support malloc at all. C++ new is a keyword, no matter on any compiler, any hardware platform can be dynamic memory allocation, this is the essential difference.

malloc dynamically allocates memory based on bytes, new dynamically allocates memory based on type

//  Variable application: 
Type * pointer = new Type;  //  From the heap space 1 A new one Type Type space 
//  The use and C language 1 sample 
delete pointer;    //  Here I'm referring to pointer The memory space of the element being pointed to is freed 

//  Array application: 
Type * pointer = new Type[N];  // N Exponential group size, number of array elements, not bytes 
delete[] pointer;  //  Array release, need to be in delete After it [] It's different from a variable 
          // delete[]  Indicates that the pointer to be freed is pointing to 1 Slice the array space, free the entire array space, if you use delete Words, pointer Points to the address of the first element of the array, and frees the memory space of the first element. The memory space of the rest elements is not freed, causing memory leak 

#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;    //  To release a single variable 
  p = new int[10]; // p Point to the 1 Slice array space, 
  // p Point to the memory space, at least occupied 40 Two bytes, guaranteed enough, may be allocated more 
  for(int i=0; i<10; i++)
    p[i] = i + 1;
    printf("p[%d] = %d\n", i, p[i]);
  delete[] p;  //  The release of the array 
  return 0;

The difference between new keyword and malloc function:

The new keyword is part 1 of C++ malloc is a function provided by the C library new allocates memory in units of a specific type malloc allocates memory in bytes new can be initialized when applying for a single type variable malloc does not have the memory initialization feature

Initialization of the new keyword:

int*  pi = new int(1);
float* pf = new float(2.0f);
char* pc = new char('c')l

2. Namespaces in C++

There is only one global scope in the C language

All global identifiers of the C language share one scope Identifiers may conflict with each other

The concept of namespaces is introduced 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 the default namespace


namespace Name
  namespace Internal
    /* ... */
  /* ... */

The use of namespaces

using namespace name;   //  Use the entire namespace 
using name::variable;   //  Use variables in the namespace 
::variable;        //  Use variables in the default namespace 

#include <stdio.h>

namespace First
  int i = 0;

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

int main()
  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;

Namespaces solve the problem of global variable naming conflicts

3, summary

C++ has built-in dedicated keywords for dynamic memory allocation

Dynamic memory allocation in C++ can be initialized simultaneously

Dynamic memory allocation in C++ is based on type

The namespace concept in C++ is used to resolve name conflicts

Related articles: