測試用例:
// DoublePTest.cpp : 定義控制台應用程序的入口點。
//
#include "stdafx.h"
#include <stdlib.h>//malloc需要的頭文件
#include "cstring"//memset需要的頭文件
using namespace std;
void Func(char** p)
{
*p = (char*)malloc(3);
char* c = new char[3];
memset(c, 'a', 3);
//c[3]='\0'; //if there is open,below delete is wrong
memcpy(*p, c, 3);
delete c;
c=NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *p = NULL;
Func(&p);
if(p)
{
char *sp =p;
int len= strlen(sp);
delete p;
p = NULL;
}
return 0;
}
這裡的測試代碼只是測試返回一個指針類型的變量,比如字符串或者是內存中的一塊buffer,當然返回一般的參數或者一般的局部變量的值通過引用就可以實現。