C++ Primer Plus学习:第九章 内存模型和名称空间(2)

本文通过代码示例介绍了C++中new操作符的使用、内存分配与释放,以及命名空间的作用和应用。包括静态内存分配、动态内存分配及内存地址输出等关键概念的解释。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

new
  new操作符
  布局new操作符

  eg

#include <iostream>
#include <new>
using namespace std;

const int BUF = 512;
const int N = 5;
char buffer[BUF];

int main()
{
    double *pd1,*pd2;
    int i;
    cout<<"Calling new and placement new:\n";
    pd1 = new double[N];
    pd2 = new (buffer) double[N];
    for(i = 0;i<N;i++)
          pd2[i] = pd1[i] = 1000+20.0*i;
    cout<<"Buffer addresses:\n"<<" heap: "<<pd1<<" static: "<<(void *)buffer<<endl;
    cout<<"Buffer contents:\n";
    for(i = 0;i<N;i++)
    {
          cout<<pd1[i]<<" at "<<&pd1[i]<<"; ";
          cout<<pd2[i]<<" at "<<&pd2[i]<<endl;
          }
          
          
    cout<<"\nCalling new and placement new a second time:\n";
    double *pd3,*pd4;
    pd3 = new double[N];
    pd4 = new (buffer) double[N];
    for(i = 0;i<N;i++)
          pd4[i] = pd3[i] = 1000+20.0*i;
    cout<<"Buffer contents:\n";
    for(i = 0;i<N;i++)
    {
          cout<<pd3[i]<<" at "<<&pd3[i]<<"; ";
          cout<<pd4[i]<<" at "<<&pd4[i]<<endl;
          }
          
    cout<<"\nCalling new and placement new a third time:\n";
    delete []pd1;
    pd1 = new double[N];
    pd2 = new(buffer+N*sizeof(double)) double[N];
    for(i = 0;i<N;i++)
          pd2[i] = pd1[i] = 1000+20.0*i;
    cout<<"Buffer contents:\n";
    for(i = 0;i<N;i++)
    {
          cout<<pd1[i]<<" at "<<&pd1[i]<<"; ";
          cout<<pd2[i]<<" at "<<&pd2[i]<<endl;
          }
    
    delete []pd1;
    delete []pd3;
    
    system("pause");
    return 0;
    }

Calling new and placement new:
Buffer addresses:
 heap: 0xb31810 static: 0x443010
Buffer contents:
1000 at 0xb31810; 1000 at 0x443010
1020 at 0xb31818; 1020 at 0x443018
1040 at 0xb31820; 1040 at 0x443020
1060 at 0xb31828; 1060 at 0x443028
1080 at 0xb31830; 1080 at 0x443030

Calling new and placement new a second time:
Buffer contents:
1000 at 0xb31840; 1000 at 0x443010
1020 at 0xb31848; 1020 at 0x443018
1040 at 0xb31850; 1040 at 0x443020
1060 at 0xb31858; 1060 at 0x443028
1080 at 0xb31860; 1080 at 0x443030

Calling new and placement new a third time:
Buffer contents:
1000 at 0xb31810; 1000 at 0x443038
1020 at 0xb31818; 1020 at 0x443040
1040 at 0xb31820; 1040 at 0x443048
1060 at 0xb31828; 1060 at 0x443050
1080 at 0xb31830; 1080 at 0x443058


命名空间
  声明区域
  潜在作用域
  using namespace XXX;

  eg

namesp.h

#include <iostream>
namespace pers
{
          const int LEN = 40;
          struct Person
          {
                 char fname[LEN];
                 char lname[LEN];
                 };
          void getPerson(Person &);
          void showPerson(const Person &);
          }

namespace debts
{
          using namespace pers;
          struct Debt
          {
                 Person name;
                 double amount;
                 };
                 
          void getDebt(Debt &);
          void showDebt(const Debt &);
          double sumDebts(const Debt ar[],int n);
          }

namesp.cpp

#include <iostream>
#include "namesp.h"

namespace pers
{
          using std::cout;
          using std::cin;
          void getPerson(Person & rp)
          {
               cout<<"Enter first name:";
               cin>>rp.fname;
               cout<<"Enter last name:";
               cin>>rp.lname;
               }
          
          void showPerson(const Person & rp)
          {
               std::cout<<rp.lname<<","<<rp.fname;
               }
          }
          
namespace debts
{
          void getDebt(Debt & rd)
          {
               getPerson(rd.name);
               std::cout<<"Enter debt:";
               std::cin>>rd.amount;
               }
               
          void showDebt(const Debt & rd)
          {
               showPerson(rd.name);
               std::cout<<":{1}quot;<<rd.amount<<std::endl;
               }
               
          double sumDebts(const Debt ar[],int n)
          {
                 double total = 0;
                 for(int i = 0;i<n;i++)
                 {
                         total+=ar[i].amount;
                         }
                 return total;
                 }
          }

namessp.cpp

#include <iostream>
#include "namesp.h"

void other(void);
void another(void);

int main()
{
    using debts::Debt;
    using debts::showDebt;
    
    Debt golf = {{"Benny","Goatsniff"},120.0};
    showDebt(golf);
    other();
    another();
    
    system("pause");
    return 0;
    }
    
void other(void)
{
     using std::cout;
     using std::endl;
     using namespace debts;
     Person dg = {"Doodles","Glister"};
     showPerson(dg);
     cout<<endl;
     Debt zippy[3];
     int i;
     
     for(i = 0;i<3;i++)
     {
           getDebt(zippy[i]);
           }
     for(i = 0;i<3;i++)
     {
           showDebt(zippy[i]);
           }
     cout<<"Total debt:{1}quot;<<sumDebts(zippy,3)<<endl;
     return ;
     }
     
void another(void)
{
     using pers::Person;
     Person collector = {"Milo","Rightshift"};
     pers::showPerson(collector);
     std::cout<<std::endl;
     }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值