Geek Class geekband-c++ Advanced Development-2

Source: Internet
Author: User

Geek Class geekband-c++ Advanced Development-2

    • Geek Class Geekband-c Advanced Development-2
      • Lecturer-Houtie
      • Review the implementation process of complex class
      • Copy-copying and destruction of three-function copy construction
      • Stack heap and memory management
        • Stack VS Heap
        • Stack objectsstatic local Objectsglobal objects Lifetime
        • The life cycle of the heap objects
        • New allocates memory before calling ctor
        • Delete First calls dtor in the release memory
        • Dynamically allocated memory block Blockin VC
        • Dynamically allocated Array
      • Review the implementation process of the String class

Lecturer-Houtie Review of the implementation process of complex class

Complex.h

#ifndef __complex__#define __COMPLEX__#include <iostream>using namespace STD;class Complex{Private:DoubleRe, im; Public:Complex(DoubleR =0,Doublei =0): Re (r), Im (i) {}Complex&operator+= (Const Complex&);DoubleReal ()Const{returnRe }DoubleImag ()Const{returnIm }friend Complex& __DOAPL (Complex*,Const Complex&);};inline Complex&Complex::operator+= (Const Complex& R) {return__DOAPL ( This, r);}inline Complex& __DOAPL (Complex* ths,Const Complex& R) {ths->re + = R.re; Ths->im + = r.im;return*ths;}inline Complex operator+(Const Complex& X,Const Complex& y) {return Complex(X.real () + y.real (), X.imag () + Y.imag ());}inline Complex operator+(Const Complex& X,DoubleY) {return Complex(X.real () + Y, X.imag () + y);}inline Complex operator+(DoubleXComplex& y) {return Complex(x + y.real (), Y.imag ());} ostream&operator<< (ostream& OS,Const Complex& x) {OS <<"("<< x.real () <<","<< X.imag () <<")";returnOS;}#endif

Complex-test.cpp

#include "Complex.h"int main() {    complex c1(36);    5;    cout << c1 << endl;    complex c2;    5 + c1;    cout << c2 << endl;    cout << c1 << c2 << endl;    return0;}
Three major functions: Copy construction, copy copy, destructor

String.h

#include"String.h"int main() {    String s1;    String s2("hello");    String s3(s1);  //拷贝构造函数    <<<< endl;    = s2;    //拷贝赋值函数    <<<< endl;    return0;}

String-test.h

#ifndef __mystring__#define __MYSTRING__#include <iostream>using namespace STD;#include <string.h>classstring{ Public: String (Const Char* CStr);//class with a pointerString (Conststring& str); string&operator=(Conststring& str); ~string ();Char* GET_C_STR ()Const{returnm_data; }Private:Char* M_DATA;};inlineString::string (Const Char* CStr =0) {if(CStr) {m_data =New Char[strlen(CStr) +1];strcpy(M_data, CStr); }Else{m_data =New Char[1]; *m_data =' + '; }}inlineString::string (Conststring& str) {m_data =New Char[strlen(Str.m_data) +1];strcpy(M_data, str.m_data);}inlinestring& String::operator=(Conststring& str) {//Detect self-assignment    if( This= = &str)return* This;Delete[] m_data; M_data =New Char[strlen(Str.m_data) +1];strcpy(M_data, Str.m_data);return* This;}inlineString::~string () {Delete[] m_data;} ostream&operator<< (ostream& OS,Conststring& str) {OS << str.get_c_str ();returnOS;}#endif
Stack, heap, and memory management stack VS. Heap

Stack, which is a memory space that exists in a scope (scope). For example, when you call a function, the function itself forms a stack to place the parameters it receives, as well as the return address.
Any variable declared within the function body, the memory block used is taken from the appeal stack.
heap, or system heap, refers to a piece of global memory space provided by the operating system, and the program dynamically allocates (dynamic allocated) a number of chunks (blocks) from one.

class Complex {};{    Complex c1(12);   //c1所占用的空间来自stack    new Complex(3);    //Complex(3)是个临时变量,所占用的空间是以new动态分配而得,并由p指向。占用空间来自heap。}
Lifetime of Stack objects, static local objects, global objects
Complex{};Complex c3(12);{    Complex c1(1, 2);    static Complex c2(1, 2);}{}
    • C1 is the so-called stack object, whose life is introduced at the end of scope (scope). object in this scope, also known as auto objects, because it is " automatically " cleaned up.
    • C2 is the so-called static object whose life persists after scope (scope) until the end of the entire program.
    • C3 is the so-called global object whose life ends at the end of the entire program. You can also think of it as a static object whose scope is the entire program.
The life cycle of the heap objects
class Complex {};{    new Complex;    delete p;}
    • P refers to the heap object whose life ends when it is deleted.
Complex{};{    Complex* p = new Complex;}
    • Memory leak is present, because when the scope ends, the heap object referred to by P still exists, but the life of pointer P is over, and P is no longer visible outside the scope (there is no chance to delete p)
NEW: Allocate memory first, then call ctor
ComplexComplex(12);

The compiler translates to:

Complex* pc;voidoperatornew(sizeof(Complex));  //分配内存,内部调用malloc(n)static_cast<Complex*>(mem);    //转型pc->Complex::Complex(12//构造函数
Delete: Call Dtor First, release memory
StringnewString("Hello");delete ps;

The compiler translates to:

String::~String(ps);   //析构函数delete(ps);    //释放内存,内部调用free(ps)
Dynamically allocated memory block, in VC dynamically allocated array

The above two parts slightly, these two parts Houtie teacher mainly to explain is delete and delete[] difference. In fact, the open array is used delete[], otherwise it may be memory leaks, why is it possible? Because:

intnewint[1];
Review the implementation process of the String class

The same three major functions: Copy construction, copy copy, and destruction section.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Geek Class geekband-c++ Advanced Development-2

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.