传递动态内存


1. 写出以下程序的输出结果

#include  < iostream >
using   namespace  std;
class  A
{
public virtual   void  print( void )
        {
            cout 
<<   " A::print() "   <<  endl;
        };

};

class  B :  public  A
{
public virtual   void  print( void )
        {
            cout 
<<   " B::print() "   <<  endl;
        };
};

class  C :  public  A
{
public void  print( void )
        {
            cout 
<<   " C::print() "   <<  endl;
        };
};

void  print(A a)
{
    a.print();
}

int  main()
{
    A a, 
* pa,  * pb,  * pc;
    B b;
    C c;

    pa 
=   & a;
    pb 
=   & b;
    pc 
=   & c;

    a.print();
    b.print();
    c.print();

    pa
-> print();
    pb
-> print();
    pc
-> print();

    print(a);
    print(b);
    print(c);

    
return   0 ;
}

答案:
A::print()
B::print()
C::print()
A::print()
B::print()
C::print()
A::print()
A::print()
A::print()


2. What will happen after running the "Test"?

#include  < iostream >

void  GetMemory( char   * p,  int  num)
{
    p 
=  ( char   * )malloc( sizeof ( char *  num);
}

int  main()
{
    
char   * str  =  NULL;

    GetMemory(str, 
100 );
    strcpy(str, 
" hello " );

    
return   0 ;
}


分析:由于void GetMemory(char *p, int num)中的*p实际上是主函数中的一个str的一个副本,编译器总是要为函数的每个参数制作临时副本。在本倒中,p申请了新的内存,只是把p把指的内存地址改变了,但str丝毫未变。因为GetMemory没有返回值,因此str并不指向p所申请的那段内存,所以函数GetMemory并不能输出任何东西。事实上,每执行一次GetMemory就会申请一块内存,但申请的内存却不能有效释放,结果是内存一直被独占,最终造成内存泄露。

答案:程序崩溃。因为GetMemory并不能传递动态内存,Test函数中的str一直都是NULL。

以下是正常的申请方式:

方法一:用指针参数去申请内存时,应用采用指向指针的指针,把str的地址传给函数GetMemory

#include  < iostream >

void  GetMemory( char   ** p,  int  num)
{
    
* =  ( char   * )malloc( sizeof ( char *  num);
}

int  main()
{
    
char   * str  =  NULL;

    GetMemory(
& str,  100 );
    strcpy(str, 
" hello " );

    std::cout 
<<   * str  <<  std::endl;
    std::cout 
<<  str  <<  std::endl;
    std::cout 
<<   & str  <<  std::endl;

    
return   0 ;
}


输出:
h
hello
0012FF7C

方法二:用函数返回值来传递动态内存

#include  < iostream >

char   * GetMemory( char   * p,  int  num)
{
    
return  p  =  ( char   * )malloc( sizeof ( char *  num);
}

int  main()
{
    
char   * str  =  NULL;

    str 
=  GetMemory(str,  100 );
    strcpy(str, 
" hello " );

    std::cout 
<<   * str  <<  std::endl;
    std::cout 
<<  str  <<  std::endl;
    std::cout 
<<   & str  <<  std::endl;

    
return   0 ;
}


3. What will happen after running the "Test"?

#include  < iostream >

char   * GetMemory( void )
{
    
char  p[]  =   " hello world " ;
    
return  p;
}

int  main()
{
    
char   * str  =  NULL;
    str 
=  GetMemory();
    std::cout 
<<  str  <<  std::endl;

    
return   0 ;
}

答案:可能是乱码,也有可能是正常输出。因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是NULL,但其原来的内容已经被清除,新内容不可知。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值