For example, find compelling authoritative C + + intermediate malloc with new

Source: Internet
Author: User

For example, find compelling authoritative C + + intermediate malloc with new

Problem:

Many people know that malloc and new are used to apply for space, and that space is derived from the heap.

But in C + + very few use malloc to apply for space, why?


The following small series are illustrated in a very convincing example. I believe we can make it clear at a glance.


The layout of C + + programs can be divided into 4 zones, which are "pattern",

1. Global data area//global variable, static variable belongs to global data area

2. Code area // all classes and non-member functions are stored in code area

3. The space of the local variables allocated for the execution of the member function in the stack area

4, the heap area// the rest of the space belong to the heap area

Among the global variables, the static variables belong to the global data area. The code for all classes and non-member functions is stored in the code area. The space for the local variables allocated for the member function execution is in the stack area. The rest of the space belongs to the heap area.


Here's a simple example: malloc.cpp

#include <iostream>using namespace std; #include <stdlib.h>class test{public    :        Test () {            cout << "The Class has constructed" <<endl;        }           ~test () {            cout<< "the Class has disconstructed" <<endl;        }   ; int main () {    Test *p = (test*) malloc (sizeof (Test));    Free (p);    Delete p;    return 0;}

Compile execution: The Class has disconstructed

The result is that no constructor is called. As can be seen from this example, malloc is only responsible for allocating space to the object pointer after malloc is called. Instead of calling the constructor to initialize it. The object construction of a class in C + + must be allocated space. Call the constructor. The initialization of a member, or an initialization of an object. Using the example above, I hope that you will try not to use malloc in C + +. and to use new.

<span style= "FONT-SIZE:14PX;" > #include <iostream>using namespace std; #include <stdlib.h>class test{public    :        Test () {            cout<< "The Class has constructed" <<endl;        }           ~test () {            cout<< "the Class has disconstructed" <<endl;        }   ; int main () {  //test *p = (test*) malloc (sizeof (Test));    Test *p = new test;    cout<< "Test" <<endl;        Free (p);    Delete p;    return 0;} </span>


The results of the execution are as follows:

the Class has constructed

 the Class has disconstructed

Suppose you want to learn more about the similarities and differences of C + + New/delete,malloc/free, and be able to refer to "deep C + + new/delete,malloc/free parsing" for details.


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

For example, find compelling authoritative C + + intermediate malloc with new

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.