第12章 动态内存 部分课后习题

12.6

/* 
编写函数,返回一个动态分配int的vector。将此vector传递给另一个函数,
这个函数读取标准输入,将读入的值保存在vector元素中。再将vector传递给另一个函数,
打印读入的值。记得在恰当的时刻delete vector
 */
#include<iostream>
#include<vector>
#include<memory>

using namespace std;

vector<int>* f1(){
    return new vector<int>();
}

void f2(vector<int> * a){
    int i;
    while(cin>>i){
        a->push_back(i);
    }  
}
void f3(vector<int> * a){
    for(const auto &v:*a){
        cout<<v<<endl;
    }
    delete a;
}

int main(){
   vector<int> *a=f1();
   f2(a);
   f3(a);
   return 0;

}

12.7

/* 
重做上一题,使用shared_ptr而不是内置指针
 */
#include<iostream>
#include<memory>
#include<vector>

using namespace std;

shared_ptr<vector<int>> f1(){
    return make_shared<vector<int>>();
}

void f2(shared_ptr<vector<int>> ptr_v){
    int i;
    while(cin>>i){
        ptr_v->push_back(i);
    }
}

void f3(shared_ptr<vector<int>> ptr_v){
    for(const auto &v:*ptr_v){
        cout<<v<<endl;
    }
}

int main(){
    shared_ptr<vector<int>> ptr_v=f1();
    f2(ptr_v);
    f3(ptr_v);
    return 0;
}

12.8

#include<iostream>

bool b(){
    int *p=new int;
    return p;
}
//p 将转换为 bool ,这意味着分配的动态内存没有机会被释放。
//结果,就会发生内存泄漏。

int main(){
    bool flag=b();
    std::cout<<flag<<std::endl;
}

12.10

#include<iostream>
#include<memory>

using namespace std;


void process(shared_ptr<int> ptr){
    //使用ptr
    cout<<"inside process function:"<<ptr.use_count()<<endl;//2
}//ptr离开作用域,被销毁


int main(){
    shared_ptr<int> p(new int(42));
    process(shared_ptr<int>(p));

    cout<<p.use_count()<<endl;// 1 
    auto q = p;
    std::cout << p.use_count() << "\n";//2
    std::cout << "the int p now points to is:" << *p << "\n";//42
    
    //12.12
    auto p_=new int();
    auto sp=make_shared<int>();
    process(sp);
    //process(new int());//错误
    //process(p_);//错误:不存在从int *到shared_ptr
    process(shared_ptr<int>(p_));//临时的share_ptr传递给process
    
    return 0;
}

12.13

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

int main(){
    auto sp=make_shared<int>();
    auto p=sp.get();
    delete p;
    //doule free or corruption(out)
}

12.16

/*  
试图拷贝或赋值unique_ptr,编译器会给出什么错误?

*/
#include<iostream>
#include<memory>
using namespace std;

int main(){
    unique_ptr<string> p1(new string("123"));
    unique_ptr<string> p2;
    //p2=p1;
    //unique_ptr<string>p2(p1);  
}

12.17

/* 
下面的unique_ptr声明中,哪些是合法的,哪些可能导致后续程序出错?
解释原因
 */
#include<iostream>
#include<memory>
using namespace std;

int main(){
    int ix=1024,*pi=&ix,*pi2=new int(2048);
    typedef unique_ptr<int> IntP;

    //IntP p0(ix); 错误
    //IntP p1(pi); 编译可以通过,但是运行会报错。因为pi指针不是new分配的,当采用delete释放时,系统会报错
    //IntP p2(pi2);此代码可以编译,但在运行时会导致指针悬空。原因是unique_ptr将释放原始指针指向的对象。
    //IntP p3(&ix);当 unique_ptr 超出范围时,它会调用 delete 来释放一个没有使用 new 分配的对象。
    IntP p4(new int(2048));//正确
    // IntP p5(p2.get());运行时双重释放或损坏两个unique_ptr指向同一个对象。因此,当两者都超出范围时,操作系统将抛出双重释放或损坏。
}

12.23

/* 
编写一个程序,连接两个字符串字面常量,将结果保存在一个动态分配的char数组中。
重写这个程序,连接两个标准库string对象
 */
#include<iostream>
#include<string>
#include<string.h>
using namespace std;

int main(){
    char *p=new char[10]();
    strcat(p,"hrh+");
    strcat(p,"tqq");
    cout<<p<<endl;
    delete [] p;
    string s1="1234";
    string s2="5678";
    cout<<s1+s2<<endl;
}

12.26

#include<iostream>
#include<string>
#include<memory>
using namespace std;


int main(){
    size_t n=5;
    allocator<string> alloc;
    auto const p=alloc.allocate(n);
    string s;
    auto q=p;
    while(cin>>s&&q!=p+n){
        alloc.construct(q++,s);
    }
    while(q!=p){
        cout<<*--q<<endl;
        alloc.destroy(q);
    }
    alloc.deallocate(p,n);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值