Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ Supplements - malloc free and new delete the same and different     - Use PXE with kickstart to install Linux automatically (Linux)

- Linux system server network security management tips (Linux)

- RedHat Linux 9.0 under P4VP-MX motherboard graphics resolution of problems (Linux)

- Hardware Firewall Basics (Linux)

- map and hash_map STL containers (Programming)

- OpenSUSE GPG Comments (Linux)

- Linux mount command Detailed (Linux)

- Java Builder mode (Programming)

- Binding unofficial Google Drive and Ubuntu 14.04 LTS (Linux)

- Difference in MySQL VARCHAR and CHAR data format (Database)

- Compare Dalvik virtual machine and the JVM (Linux)

- How to use SVN to manage our source code (Server)

- Linux System Getting Started Tutorial: How to update outdated version of Ubuntu (Linux)

- Use PuTTY key authentication mechanism for remote login Linux (Linux)

- DIY security of Linux platform (Linux)

- To_explore Linux system boot process (Linux)

- Ubuntu dual-card system configuration method (Server)

- Java MD5 encryption implementation (Programming)

- linux remote control software (Linux)

- Red Flag Linux Desktop 6.0 hard disk installation guide (Programming)

 
         
  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:      
 
- Why not use the ifconfig command under RedHat Linux 5 (Linux)
- Cobbler batch install Ubuntu / CentOS system (Linux)
- Ubuntu 14.04 and derivative versions of the user install Pantheon Photos 0.1 (Linux)
- Automatic Clear date directory shell script (Linux)
- How apt-get limited use IPv4 or IPv6 protocol to download (Linux)
- Nine artifact control disk partition under Linux (Linux)
- Linux Basic Course: Install the software from source code (Linux)
- Linux Network Programming - raw socket instance: MAC header message analysis (Programming)
- Reset CentOS / RHEL root account password 7 (Linux)
- Linux system started to learn: how to solve the xxx is not in the sudoers file error (Linux)
- Ubuntu security settings and tools (Linux)
- Enterprise-class GitHub warehousing environment build (Server)
- MySQL + Heartbeat + DRBD deployment architecture (Server)
- Linux virtual machine how to access the Internet in a virtual machine when using NAT mode (Linux)
- socket busy poll of Linux kernel 3.11 to avoid sleep switch (Linux)
- Struts2 configure a static resource files without Struts processing (regular match) (Programming)
- Java Builder mode (Programming)
- SaltStack installation and testing (Server)
- How to install the Linux text editor Atom 0.124.0 (Linux)
- Git Advanced Tutorial (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.