zoukankan      html  css  js  c++  java
  • 布局new分配 ,

    布局new和普通New不同,相当于建立一个小堆空间 。

    #include <iostream>
    #include <string>
    #include <new>
    using namespace std;
    const int BUF = 512;
    class testing
    {
    private :
    string word;
    int len;
    public:
    testing(const string & s = "tesing",int n = 0 )
    {
    word = s;
    len = n;
    cout <<word <<"construted"<<endl;
    }
    ~testing()
    {
    cout << word <<"destroy" <<endl;
    }
    void show() const
    {
    cout << word <<"," <<len <<endl;
    }
    };

    int main()
    {
    char *buffer = new char[BUF];
    testing *pc1,*pc2;
    pc1 = new( buffer) testing;
    pc2 = new testing("heap1" , 20);

    cout <<"memory block address :" <<(void *)buffer << "heap1" << pc2<<endl;

    cout <<"memory content" << pc1 <<":";
    pc1->show();
    cout << pc2 <<":";
    pc2->show();

    testing *p3 , *p4;
    p3 = new (buffer + sizeof(testing)) testing("third",3);
    delete pc2;
    pc1->~testing();
    p3->~testing();
    delete [] buffer;
    return 0;
    }
  • 相关阅读:
    第三章 AOP
    第二章 IoC
    第一章 Spring 概述
    框架整合
    后台管理工程搭建
    技术架构
    淘淘商城简介
    电商行业背景
    前言
    FutureTask的使用
  • 原文地址:https://www.cnblogs.com/lzhenf/p/2414352.html
Copyright © 2011-2022 走看看