Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ Supplements - malloc free and new delete the same and different     - Windows Server 2012 R2 Datacenter install SQL Server 2016 CTP (Database)

- Linux, Firefox unloading and installation and upgrade (Linux)

- Spring next ActiveMQ combat (Programming)

- Install the latest ATI / Nvidia graphics driver on Ubuntu (Linux)

- RMAN backup file is much larger than the size of the database Cause Analysis (Database)

- Zabbix configuration external network mail alarm (Server)

- open V switch port mirror in OpenStack neutron (Server)

- Linux crontab commands and detailed usage examples (Linux)

- Linux reserves the rest of the file to delete several (Linux)

- Nginx reverse proxy and self-signed https (Server)

- Objective-C basic program structure (Programming)

- Linux system ARP attack solution (Linux)

- Ubuntu Tutorial - Manually install Oracle Java JDK 8 (Linux)

- Manual cleaning Oracle audit records (Database)

- Try to use Lets Encrypt (Linux)

- IIS virtual host of safety knowledge (Linux)

- Volatile keyword in C language understanding (Programming)

- KUbuntu / Ubuntu 14.04 (downgrade) installed SVN 1.7 (Linux)

- Oracle how to maintain the consistency of read? (Database)

- Ubuntu Tutorial: E: Failed to get lock / var / lib / apt / lists / lock - open (Linux)

 
         
  C ++ Supplements - malloc free and new delete the same and different
     
  Add Date : 2018-11-21      
         
       
         
  Introduction

In C, we often use malloc and free to dynamically allocate and release memory, in C + + is the corresponding new and delete. Here we come to explore their differences.

Text

1. Built-in type

Debug the same code to view the memory

#include < iostream>
Using namespace std;
Int main ()
{
 Int * p = (int *) malloc (sizeof (int) * 10);
 Cout < < p < < endl;
 // Breakpoints
 For (int i = 0; i < 10; i ++)
  P [i] = i;
 // Breakpoints
 Free (p);
 // Breakpoints
 Cin.get ();
 Return 0;
}}

The same function uses new and delete to operate

#include < iostream>
Using namespace std;
Int main ()
{
 Int * p = new int [10];
 Cout < < p < < endl;
< Span style = "white-space: pre"> < / span> // breakpoint
 For (int i = 0; i < 10; i ++)
  P [i] = i;
< Span style = "white-space: pre"> < / span> // breakpoint
 Delete [] p;
< Span style = "white-space: pre"> < / span> // breakpoint
 Cin.get ();
 Return 0;
}}

For built-in types, the effect of the two sets of operations is the same.

2. Class types

Code one

#include < iostream>
Using namespace std;
Class MyClass
{
Public:
 MyClass ()
 {
  Cout < < "MyClass create" < < endl;
 }}
 ~ MyClass ()
 {
  Cout < < "MyClass delete" < < endl;
 }}
};
Int main ()
{
 MyClass * p1 = (MyClass *) malloc (sizeof (MyClass));
 Free (p1);
 Cout < < "--------------------" < < endl;
 MyClass * p2 = new MyClass;
 Delete p2;
 Cin.get ();
 Return 0;
}}

Malloc just allocates memory, and new not only allocates memory, it also calls the constructor.

Free only the release of memory, and delete not only the release of memory, also called the destructor.

Code two

#include < iostream>
#include < new>
Using namespace std;
Class MyClass
{
Public:
 Int * p;
 MyClass ()
 {
  400M memory allocation
  P = new int [1024 * 1024 * 100];
  Cout < < "MyClass create" < < endl;
 }}
 ~ MyClass ()
 {
  Delete [] p;
  Cout < < "MyClass delete" < < endl;
 }}
};
Int main ()
{
 MyClass * p1 = (MyClass *) malloc (sizeof (MyClass));
 // Breakpoints
 Free (p1);
 // Breakpoints
 MyClass * p2 = new MyClass;
 // Breakpoints
 Delete p2;
 // Breakpoints
 Cin.get ();
 Return 0;
}}

Start the Task Manager to see the memory consumption

Malloc free

Since malloc only allocates memory for the variable p itself, it does not allow p to point to an allocated memory because the constructor is not called. Similarly, free only the release of the variable p, because it does not call the destructor, so can not release the memory pointed to by p.
     
         
       
         
  More:      
 
- Linux software firewall ACL match point optimization (Linux)
- Android working with Volley Comments (Programming)
- CentOS 6.4 under PXE + Kickstart unattended operating system installation (Programming)
- Windows 8.1 hard drive to install Ubuntu 14.04 dual system reference tutorials and multi-drive Precautions (Linux)
- Video editing captions under Linux (Linux)
- Virtualization and IT cooperation (Linux)
- Fedora 19/20 and Debian Jessie / Sid users to install FFmpeg 2.3.2 (Linux)
- Android Application Development: an argument between Activity (Programming)
- Linux System Getting Started Learning: hard disk partition, and to deal with traps (Linux)
- Java Access Control (Programming)
- Linux firewall settings -DNS server articles (Server)
- Use small network command to check whether PC Security (Linux)
- Linux basis: a comprehensive study pwd command (Linux)
- Oracle 11g RAC manually playing GI PSU patch (11.2.0.4.8) (Database)
- Oracle Duplicate build DataGuard (Database)
- MySQL DATE_FORMAT () function (Database)
- xCAT deployment (Linux)
- Setting grep Highlight Matches (Linux)
- Detailed driver compiled into the Linux kernel (Programming)
- Installation of JDK and Tomcat under Linux (CentOS) (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.