10.18 C++|C++編譯器對new和delete的底層實現及編譯器的額外行為

new、delete在C++中定義為操作符函數,用於動態分配堆上內存。C++編譯器對new、delete的底層實現還是調用了malloc()和free(),但做了封裝,且C++編譯器對new、delete語句在編譯時做了一些額外的工作。

1 在堆上new一個對象:先分配memory,再調用構造函數

有Complex類:

class complex {
private:
double re, im;
public:
complex(double r = 0, double i = 0) :re(r), im(i) {
}
complex& operator += (const complex&);
double real() const {
return re;
}
double imag() const {
return im;
}
friend complex& __doapl(complex*, const complex&);
};

在堆上new一個對象:

Complex* pc = new Complex(1,2);

編譯器轉化為:

Complex *pc;
void* mem = operator new(sizeof(Complex)); // ① 分配內存,內部調用malloc(n)
pc = static_cast<complex>(mem); // ② 轉型
pc->Complex::Copmlex(1,2); // ③ 調用構造函數
/<complex>

2 delete :先調用析構函數,在釋放memory

Complex* pc = new Complex(1,2);
...
delete pc;

編譯器轉化為:

Complex::~Complex(pc); // ① 調用析構函數
operator delete(pc); // ② 釋放內存,內部調用free(pc)

附complex類代碼:

//complex.h
#ifndef __MYCOMPLEX__
#define __MYCOMPLEX__
#include<iostream>
using namespace std;
class complex;
complex&
__doapl (complex* ths, const complex& r);
complex&
__doami (complex* ths, const complex& r);
complex&
__doaml (complex* ths, const complex& r);
class complex
{
public:
complex (double r = 0, double i = 0): re (r), im (i) { }
complex& operator += (const complex&);
complex& operator -= (const complex&);
complex& operator *= (const complex&);
complex& operator /= (const complex&);
double real () const { return re; }
double imag () const { return im; }
private:
double re, im;
friend complex& __doapl (complex *, const complex&);
friend complex& __doami (complex *, const complex&);
friend complex& __doaml (complex *, const complex&);
//friend ostream& operator << (ostream& os, const complex& x);
};
inline complex&
__doapl (complex* ths, const complex& r)
{
ths->re += r.re;
ths->im += r.im;
return *ths;
}

inline complex&
complex::operator += (const complex& r)
{
return __doapl (this, r);
}
inline complex&
__doami (complex* ths, const complex& r)
{
ths->re -= r.re;
ths->im -= r.im;
return *ths;
}

inline complex&
complex::operator -= (const complex& r)
{
return __doami (this, r);
}

inline complex&
__doaml (complex* ths, const complex& r)
{
double f = ths->re * r.re - ths->im * r.im;
ths->im = ths->re * r.im + ths->im * r.re;
ths->re = f;
return *ths;
}
inline complex&
complex::operator *= (const complex& r)
{
return __doaml (this, r);
}

inline double
imag (const complex& x)
{
return x.imag ();
}
inline double
real (const complex& x)
{
return x.real ();
}
inline complex
operator + (const complex& x, const complex& y)
{
return complex (real (x) + real (y), imag (x) + imag (y));
}
inline complex
operator + (const complex& x, double y)
{
return complex (real (x) + y, imag (x));
}
inline complex
operator + (double x, const complex& y)
{
return complex (x + real (y), imag (y));
}
inline complex
operator - (const complex& x, const complex& y)
{
return complex (real (x) - real (y), imag (x) - imag (y));
}
inline complex
operator - (const complex& x, double y)
{
return complex (real (x) - y, imag (x));
}
inline complex
operator - (double x, const complex& y)
{
return complex (x - real (y), - imag (y));
}
inline complex
operator * (const complex& x, const complex& y)
{
return complex (real (x) * real (y) - imag (x) * imag (y),
real (x) * imag (y) + imag (x) * real (y));
}
inline complex
operator * (const complex& x, double y)
{
return complex (real (x) * y, imag (x) * y);
}
inline complex
operator * (double x, const complex& y)
{
return complex (x * real (y), x * imag (y));
}
complex
operator / (const complex& x, double y)
{
return complex (real (x) / y, imag (x) / y);
}
inline complex
operator + (const complex& x)
{
return x;
}
inline complex
operator - (const complex& x)
{
return complex (-real (x), -imag (x));
}
inline bool
operator == (const complex& x, const complex& y)
{
return real (x) == real (y) && imag (x) == imag (y);
}
inline bool
operator == (const complex& x, double y)
{
return real (x) == y && imag (x) == 0;
}
inline bool
operator == (double x, const complex& y)
{
return x == real (y) && imag (y) == 0;
}
inline bool
operator != (const complex& x, const complex& y)
{
return real (x) != real (y) || imag (x) != imag (y);
}
inline bool
operator != (const complex& x, double y)
{
return real (x) != y || imag (x) != 0;
}
inline bool
operator != (double x, const complex& y)
{
return x != real (y) || imag (y) != 0;
}
#include <cmath>
inline complex
polar (double r, double t)
{
return complex (r * cos (t), r * sin (t));
}
inline complex
conj (const complex& x)
{
return complex (real (x), -imag (x));
}
inline double
norm (const complex& x)
{
return real (x) * real (x) + imag (x) * imag (x);
}
ostream& operator << (ostream& os, const complex& x)
{
return os << '(' << real (x) << ',' << imag (x) << ')';
}
#endif //__MYCOMPLEX__
//complex.cpp
#include <iostream>
#include "complex.h"
using namespace std;
int main()
{
complex c1(2, 1);
complex c2(4, 0);
cout << c1 << endl;
cout << c2 << endl;

cout << c1+c2 << endl;
cout << c1-c2 << endl;
cout << c1*c2 << endl;
cout << c1 / 2 << endl;

cout << conj(c1) << endl;
cout << norm(c1) << endl;
cout << polar(10,4) << endl;

cout << (c1 += c2) << endl;

cout << (c1 == c2) << endl;
cout << (c1 != c2) << endl;
cout << +c2 << endl;
cout << -c2 << endl;

cout << (c2 - 2) << endl;
cout << (5 + c2) << endl;
system("pause");
return 0;
}
/<iostream>/<cmath>/<iostream>

-End-


分享到:


相關文章: