错误程序:
void GetMemory( char *p )
{
p = (char *) malloc( 100 );
}
void Test( void )
{
char *str = NULL;
GetMemory( str );
strcpy( str, "hello world" );
printf( “%s”,str );
}
这个一个考验对指针理解的题目,上面程序在运行之后:
1,调用GetMemory( str )后, str并未产生变化,依然是NULL.只是改变的str的一个拷贝的内存的变化
2,strcpy( str, "hello world" );程序运行到这将产生错误。
3,new的时候有可能内存出错,应该在*p = (char *) malloc( num ); 后判断内存是否申请成功,应加上:
if ( *p == NULL )
{
...//进行申请内存失败处理
}
4,动态创建的内存没释放。
错误分析:
错认为 GetMemory(char *p)中的 p “就是” GetMemory(str)中的str。但p“不是”str,它只是“等于”str 。
就象: int a = 100;
int b = a; // 现在b等于a
b = 500; // 现在能认为a = 500 ?
显然不能认为a = 500,因为b只是等于a,但不是a! 当b改变的时候,a并不会改变,b就不等于a了。 因此,虽然p已经有new的内存,但str仍然是null
GetMemory(str); //把str传进去,str是一个指针,而他实际上是一个int
void GetMemory(char *p) // p是str的一个副本
{
p=(char *)new char[100]; // p的值改变,但是str的值并没有改变。
}
而双重指针为什么就可以了呢:
GetMemory(&str); //把str的地址传进去
void GetMemory(char ** p) // p是str地址的一个副本
{
*p = (char *)new char[100]; // p指向的值改变,也就是str的值改变。
}
修改方法1:(推荐使用这种方法)
void GetMemory2(char **p)变为二级指针.
void GetMemory2(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
}
void Test(void)
{
char *str=NULL;
GetMemory=(&str);
strcpy(str,"hello world");
printf(str);
}
修改方法2:
char *GetMemory()
{
char *p=(char *)malloc(100);
return p;
}
void Test(void){
char *str=NULL;
str=GetMemory();
strcpy(str,"hello world");
printf(str);
}
附录A(相关资料)
试题5:
char *GetMemory( void )
{
char p[] = "hello world";
return p;
}
void Test( void )
{
char *str = NULL;
str = GetMemory();
printf( str );
}
试题6:
void GetMemory( char **p, int num )
{
*p = (char *) malloc( num );
}
void Test( void )
{
char *str = NULL;
GetMemory( &str, 100 );
strcpy( str, "hello" );
printf( str );
}
试题7:
void Test( void )
{
char *str = (char *) malloc( 100 );
strcpy( str, "hello" );
free( str );
... //省略的其它语句
}
解答:
试题5中
char p[] = "hello world";
return p;
的p[]数组为函数内的局部自动变量,在函数返回后,内存已经被释放。这是许多程序员常犯的错误,其根源在于不理解变量的生存期。
试题6中
1、GetMemory避免了试题4的问题,传入GetMemory的参数为字符串指针的指针,但是在GetMemory中执行申请内存及赋值语句
*p = (char *) malloc( num );
后未判断内存是否申请成功,应加上:
if ( *p == NULL )
{
...//进行申请内存失败处理
}
2、试题6的Test函数中也未对malloc的内存进行释放。
试题7中
存在与试题6同样的问题,在执行
char *str = (char *) malloc(100); 后未进行内存是否申请成功的判断;另外,在free(str)后未置str为空,导致可能变成一个“野”指针,应加上: str = NULL;
剖析:
试题4~7考查面试者对内存操作的理解程度,基本功扎实的面试者一般都能正确的回答其中50~60的错误。但是要完全解答正确,却也绝非易事。
对内存操作的考查主要集中在:
(1)指针的理解;
(2)变量的生存期及作用范围;
(3)良好的动态内存申请和释放习惯。
再看看下面的一段程序有什么错误:
swap( int* p1,int* p2 )
{
int *p;
*p = *p1;
*p1 = *p2;
*p2 = *p;
}
在swap函数中,p是一个“野”指针,有可能指向系统区,导致程序运行的崩溃。在VC++中DEBUG运行时提示错误“Access Violation”。该程序应该改为:
swap( int* p1,int* p2 )
{
int p;
p = *p1;
*p1 = *p2;
*p2 = p;
}
题目一:
- 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)函数的形参为字符串指针,在函数内部修改形参并不能真正的改变传入形参的值。执行完
- char *str = NULL;
- GetMemory( str );
题目二:
- char *GetMemory( void )
- {
- char p[] = "hello world";
- return p;
- }
- void Test( void )
- {
- char *str = NULL;
- str = GetMemory();
- printf( str );
- }
题目三:
- void GetMemory( char **p, int num )
- {
- *p = (char *) malloc( num );
- }
- void Test( void )
- {
- char *str = NULL;
- GetMemory( &str, 100 );
- strcpy( str, "hello" );
- printf( str );
- }
- *p = (char *) malloc( num );
- if ( *p == NULL )
- {
- ...//进行申请内存失败处理
- }
也可以将指针str的引用传给指针p,这样GetMemory函数内部对指针p的操作就等价于对指针str的操作:
- void GetMemory( char *&p) //对指针的引用,函数内部对指针p的修改就等价于对指针str的修改
- {
- p = (char *) malloc( 100 );
- }
- void Test(void)
- {
- char *str=NULL;
- GetMemory(str);
- strcpy( str, "hello world" );
- puts(str);
- }
题目四:
- void Test( void )
- {
- char *str = (char *) malloc( 100 );
- strcpy( str, "hello" );
- free( str );
- ... //省略的其它语句
- }
- str = NULL;
题目五:
- 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;
- }
题目六:
- char* GetMemory(void)
- {
- char* p = "hello world";
- return p;
- }
- void Test(void)
- {
- char* str = NULL;
- str = GetMemory();
- cout<<str<<endl;
- }<strong> </strong>
- strcpy(str, "hello test");
题目七:
- 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