C++拾遺--定位new運算式,new運算式

來源:互聯網
上載者:User

C++拾遺--定位new運算式,new運算式

                        C++拾遺--定位new運算式

前言

    new運算式,預設下把記憶體開闢到堆區。使用定位new運算式,可以在指定地址地區(棧區、堆區、靜態區)構造對象,這好比是把記憶體開闢到指定地區。

本文定位new運算式的常見形式

  • new(address) type;
  • new(address) type(initializers);
  • new(address) type[size];
  • new(address) type[size]{braced initializer list};
定位new運算式調用 void *operator new(size_t, void *); 分配記憶體。範例程式碼
#include <iostream>using namespace std;char addr1[100];int main(){cout << "******定位new運算式示範***by David***" << endl;char addr2[100];char *addr3 = new char[100];cout << "addr1 = " << (void*)addr1 << endl;cout << "addr2 = " << (void*)addr2 << endl;cout << "addr3 = " << (void*)addr3 << endl;int *p = nullptr;//把記憶體配置到靜態區p = new(addr1)int;*p = 1;cout << (void*)p << "  " << *p << endl;//把記憶體配置到棧區p = new(addr2)int;*p = 2;cout << (void*)p << "  " << *p << endl;//把記憶體配置到堆區p = new(addr3)int;*p = 3;cout << (void*)p << "  " << *p << endl;cin.get();return 0;}
運行










本專欄目錄
  • C++拾遺 目錄
所有內容的目錄
  • CCPP Blog 目錄






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.