GetMemory

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

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

 }

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

 char* GetMemory2(char* p, int num)
 {
     p=(char*)malloc(sizeof(char)*num);
     return p;
 }
 int main()
 {
     char* str=NULL ;
     GetMemory(str, 100);
     strcpy(str, "hello");
     cout<<str<<endl;
     free(str);

     char* str1=NULL ;
     GetMemory1(&str1,100);
     strcpy(str1,"hello");
     cout << str1 << endl;
    free(str1);
     char* str2=NULL;
     str2=GetMemory2(str2,100);
     strcpy(str2,"hello");
     cout<<str2<<endl;
    free(str2);
     return 0;
 }

 

posted on 2013-02-15 18:59  mymemory  阅读(195)  评论(0)    收藏  举报

导航