GetMemory函數詳解


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

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

//p = new char[num];
return p;

}
int main(void)
{
char *str ;
GetMemory(str, 100);
strcpy(str, "hello");
cout << str << endl;
return 0;
}

//上面是一個正確的程序,下面是轉載的一些相關的知識

GetMemory錯誤講解(指針練習) 錯誤程序:

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;
}


本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/xiven/archive/2009/07/13/4345199.aspx


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM