输入特性
在主调函数中分配内存,被调函数使用
输出特性
被调函数中分配内存,主调函数使用
测试源码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void func(char * p)
{
strcpy(p, "hello world");
}
void test01()
{
char buf[1024] = { 0 };
func(buf);
printf("%s\n", buf);
}
void printString(char * str)
{
printf("%s\n", str);
}
void test02()
{
char * p = malloc(sizeof(char)* 64);
memset(p, 0, 64);
strcpy(p, "hello world");
printString(p);
free(p);
}
void allocateSpace(char ** pp)
{
char * temp = malloc(sizeof(char)* 64);
memset(temp, 0, 64);
strcpy(temp, "hello world");
*pp = temp;
}
void test03()
{
char * p = NULL;
allocateSpace(&p);
printf("%s\n", p);
free(p);
}
int main(){
test01();
test02();
test03();
system("pause");
return EXIT_SUCCESS;
}
测试结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/26a90e573140821411a934d80945de8b.png)
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int getMem(char **myp1 , int *mylen1 , char **myp2 , int *mylen2 )
{
int ret = 0;
char *tmp1, *tmp2;
tmp1 = (char *)malloc(100);
strcpy(tmp1, "1132233");
*mylen1 = strlen(tmp1);
*myp1 = tmp1;
tmp2 = (char *)malloc(200);
strcpy(tmp2, "aaaaavbdddddddd");
*mylen2 = strlen(tmp2);
*myp2 = tmp2;
return ret;
}
char * getMem1(int num)
{
int ret = 0;
char *tmp1;
tmp1 = (char *)malloc(num);
strcpy(tmp1, "1132233");
return tmp1;
}
int main(int argc, const char * argv[])
{
int ret = 0;
char *p1 = NULL;
int len1 = 0;
char *p2 = NULL;
int len2 = 0;
ret = getMem(&p1, &len1, &p2, &len2);
if (ret != 0)
{
printf("func getMem41() err:%d \n", ret);
return ret;
}
printf("p1:%s \n", p1);
printf("p2:%s \n", p2);
if (p1 != NULL)
{
free(p1);
p1 = NULL;
}
if (p2 != NULL)
{
free(p2);
p2 = NULL;
}
p1 = getMem1(100);
printf("p1:%s \n", p1);
if (p1 != NULL)
{
free(p1);
p1 = NULL;
}
return ret;
}