C几个经典的关于内存的笔试题



Getmemory的几个经典的关于内存的笔试题:

<NO 1>

  1. void GetMemory( char *p )  
  2. {  
  3.     p = (char *) malloc( 100 );  
  4. }  
  5.   
  6. void Test( void )   
  7. {  
  8.     char *str = NULL;  
  9.     GetMemory( str );   
  10.     strcpy( str, "hello world" );  
  11.     printf( str );  
  12. }  
void GetMemory( char *p )
{
 	p = (char *) malloc( 100 );
}

void Test( void ) 
{
	char *str = NULL;
 	GetMemory( str ); 
 	strcpy( str, "hello world" );
 	printf( str );
}
运行错误 】传入GetMemory(char* p)函数的形参为字符串指针,在函数内部修改形参并不能真正的改变传入形参的值。执行完

  1. char *str = NULL;    
  2. GetMemory( str );  
char *str = NULL;  
GetMemory( str );
后的str仍然为NULL。

实质:编译器总是要为每个参数制作临时副本,指针参数p的副本是_p,编译器使_p=p。如果函数体内的程序修改了_p的内容,就导致参数p的内容作相应的修改,这就是指针可以用作输出参数的原因。在本例中,_p申请了新的内存,只是把_p所指的内存地址改变了,但是p丝毫未变。所以GetMemory并不能输出任何东西。事实上,每执行一次GetMemory就会泄露一块内存,因为没有用free释放内存。

<NO 2>

  1. char *GetMemory( void )    
  2. {     
  3.     char p[] = "hello world";     
  4.     return p;     
  5. }    
  6.     
  7. void Test( void )    
  8. {     
  9.     char *str = NULL;     
  10.     str = GetMemory();     
  11.     printf( str );     
  12. }   
char *GetMemory( void )  
{   
 	char p[] = "hello world";   
 	return p;   
}  
  
void Test( void )  
{   
 	char *str = NULL;   
 	str = GetMemory();   
 	printf( str );   
} 
运行错误 】GetMemory中的p[]为函数内的局部自动变量,在函数调用返回后,内存已经被释放。所以打印出来的数据不是我们要求打印的数据,但是返回的指针指向的地址是一定的。这是很多程序员常犯的错误,其根源在于不理解变量的生存期。用调试器逐步跟踪Test,发现执行str=GetMemory语句后str不再是NULL指针,但是str的内容不是“hello world”,而是垃圾。


<NO 3>

  1. void GetMemory( char **p, int num )    
  2. {    
  3.     *p = (char *) malloc( num );    
  4. }    
  5.     
  6. void Test( void )    
  7. {    
  8.     char *str = NULL;    
  9.     GetMemory( &str, 100 );    
  10.     strcpy( str, "hello" );     
  11.     printf( str );     
  12. }    
void GetMemory( char **p, int num )  
{  
 	*p = (char *) malloc( num );  
}  
  
void Test( void )  
{  
 	char *str = NULL;  
 	GetMemory( &str, 100 );  
 	strcpy( str, "hello" );   
 	printf( str );   
}  
运行正确,但有内存泄露 <NO 3> 避免了题目一的问题,传入GetMemory的参数为字符串指针的指针,但是在GetMemory中执行申请及赋值语句

  1. *p = (char *) malloc( num );  
*p = (char *) malloc( num );
后未判断内存是否申请成功,应加上

  1. if ( *p == NULL )    
  2. {    
  3.  ...//进行申请内存失败处理    
  4. }   
if ( *p == NULL )  
{  
 ...//进行申请内存失败处理  
} 
也可以将指针str的引用传给指针p,这样GetMemory函数内部对指针p的操作就等价于对指针str的操作:

  1. //对指针的引用,函数内部对指针p的修改就等价于对指针str的修改   
  2. void GetMemory( char *&p)       
  3. {    
  4.     p = (char *) malloc( 100 );    
  5. }    
  6.     
  7. void Test(void)    
  8. {    
  9.     char *str=NULL;    
  10.     GetMemory(str);    
  11.     strcpy( str, "hello world" );    
  12.     puts(str);    
  13. }   
//对指针的引用,函数内部对指针p的修改就等价于对指针str的修改 
void GetMemory( char *&p)     
{  
    p = (char *) malloc( 100 );  
}  
  
void Test(void)  
{  
    char *str=NULL;  
    GetMemory(str);  
    strcpy( str, "hello world" );  
    puts(str);  
} 

<NO 4>

  1. void Test( void )    
  2. {    
  3.     char *str = (char *) malloc( 100 );    
  4.     strcpy( str, "hello" );    
  5.     free( str );     
  6.     if (str != NULL)  
  7.     {  
  8.         strcpy(str, "world");  
  9.         printf(str);  
  10.     }  
  11. }    
void Test( void )  
{  
 	char *str = (char *) malloc( 100 );  
 	strcpy( str, "hello" );  
 	free( str );   
 	if (str != NULL)
	{
		strcpy(str, "world");
		printf(str);
	}
}  
运行正确,但有内存泄露 <NO 4> <NO 3> 存在同样的问题,在执行malloc后未进行内存是否申请成功的判断。此外,在free(str)后未置str为空,导致可能变成一个“野指针”,应加上

  1. str = NULL;   
str = NULL; 
<NO 3> 的Test函数中也未对malloc的内存进行释放。


<NO 5>

  1. char* GetMemory(int num)    
  2. {    
  3.     char* p = (char*)malloc(100);    
  4.     return p;    
  5. }    
  6.     
  7. void Test(void)    
  8. {    
  9.     char* str = NULL;    
  10.     str = GetMemory(100);    
  11.     strcpy(str, "hello");    
  12.     cout<<str<<endl;    
  13. }  
char* GetMemory(int num)  
{  
    char* p = (char*)malloc(100);  
    return p;  
}  
  
void Test(void)  
{  
    char* str = NULL;  
    str = GetMemory(100);  
    strcpy(str, "hello");  
    cout<<str<<endl;  
}
运行正确】注意<NO 5><NO 2>的区别。虽然都是局部变量,但<NO 5>用函数返回值来传递动态内存;而 <NO 2> return语句返回指向“栈”内存的指针,因为该内存在函数结束时自动消亡。


<NO 6>

  1. char* GetMemory(void)    
  2. {    
  3.     char* p = "hello world";    
  4.     return p;    
  5. }    
  6.     
  7. void Test(void)    
  8. {    
  9.     char* str = NULL;    
  10.     str = GetMemory();    
  11.     cout<<str<<endl;    
  12. }  
char* GetMemory(void)  
{  
    char* p = "hello world";  
    return p;  
}  
  
void Test(void)  
{  
    char* str = NULL;  
    str = GetMemory();  
    cout<<str<<endl;  
}
运行正确,但不合理 】虽然Test运行不会出错,但是函数GetMemory的设计概念却是错误的。因为GetMemory内的“hello world”是常量字符串,位于静态存储区,它在程序生命期内恒定不变。无论什么时候调用GetMemory,它返回的始终是同一个“只读”的内存块。例如,如想执行

  1. strcpy(str, "hello test");  
strcpy(str, "hello test");
则程序会中断,并提示内存错误。


<NO 7>

  1. int* GetMemory(int* ptr)    
  2. {    
  3.     ptr = new int(999);    
  4.     return ptr;    
  5. }    
  6.     
  7. int main()    
  8. {    
  9.     int *ptr1 = 0, *ptr2 = 0;    
  10.     ptr1 = GetMemory(ptr2);    
  11.     if(ptr1) { cout<<*ptr1<<'\n'; } else { cout<<"ptr1 == NULL\n"; }    
  12.     if(ptr2) { cout<<*ptr2<<'\n'; } else { cout<<"ptr2 == NULL\n"; }    
  13.         
  14.     system("pause");    
  15.     return 0;    
  16. }    
int* GetMemory(int* ptr)  
{  
    ptr = new int(999);  
    return ptr;  
}  
  
int main()  
{  
    int *ptr1 = 0, *ptr2 = 0;  
    ptr1 = GetMemory(ptr2);  
    if(ptr1) { cout<<*ptr1<<'\n'; } else { cout<<"ptr1 == NULL\n"; }  
    if(ptr2) { cout<<*ptr2<<'\n'; } else { cout<<"ptr2 == NULL\n"; }  
      
    system("pause");  
    return 0;  
}  
程序输出:

999

ptr2 == NULL

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值