关于一个申请内存的有关问题
关于一个申请内存的问题
偶是用DEV编译器
一、
定义一个结构
struct aa
{
int no;
char name[20];
struct aa * pNext;
};
二、写函数分配内存空间及写入数据
fu_input( struct aa * pItem)
{
//如果在函数中分配内存空间,超出函数范围后内存就不存在了
pItem = ( struct aa *)malloc( sizeof(struct aa) );
pItem-> no=10;
strcpy(pItem-> name, "hello ");
pItem-> pNext = NULL;
}
三、主函数里调用
int main()
{
struct aa * pHeader;
fu_input(pHeader);
//返回主函数后pHeader为空的,printf调用时就会报错
printf( "%d,%s ",pHeader-> no,pHeader-> name);
return 0;
}
//但如果偶在主函数里分配内存空间,再调用就没有问题
//调整后函数如下:
fu_input( struct aa * pItem)
{
pItem-> no=10;
strcpy(pItem-> name, "hello ");
pItem-> pNext = NULL;
}
//主函数里调用时
pHeader = ( struct aa *)malloc( sizeof(struct aa ));
fu_input(pHeader); //在主函数中分配内存空间就没有问题
现向各位请教一下,在函数fu_input中申请内存空间后,超出函数范围后,该内存空间就不存了吗?
而要在主函数里申请内存空间
这是个什么概念?
小的初学,请各位出手指点
------解决方案--------------------
注意指针作为参数传递的概念
struct aa * pHeader; //这里使用前没有初始化
fu_input(pHeader);
//在这个函数调用后pHeader指针根本没有指向你分配的内存,而你分配的内存
//在函数调用完已经泄露了。。。
//原因如下
fu_input( struct aa * pItem)
{
// 你的参数是传递指针的拷贝,而不是引用,而开始
//的pHeader指针是没有初始化,及指向未知内存空间
//这时pItem指向了和pHeader一样的内存空间
pItem = ( struct aa *)malloc( sizeof(struct aa) );
//这条语句只是使pItem指针指向了分配的内存,pHeade指针没有变
//即还是未知内存
pItem-> no=10;
strcpy(pItem-> name, "hello ");
pItem-> pNext = NULL;
//函数结束时pItem生命期结束,而没有使用free(pItem)释放内存造成内存
//泄漏, 同时pHeade指针还是指向的最开始的未知内存
}
printf( "%d,%s ",pHeader-> no,pHeader-> name);
//由于在fu_input( struct aa * pItem)调用过程中pHeader从未变化
//还是一个未知内存,引用当然会出错
//可以这样小改一下
#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct aa
{
int no;
char name[20];
struct aa * pNext;
};
void fu_input( struct aa * &pItem)//这里改为传递指针的引用
{
pItem = ( struct aa *)malloc( sizeof(struct aa) );
pItem-> no = 10;
strcpy(pItem-> name, "hello ");
pItem-> pNext = NULL;
}
int main()
{
struct aa * pHeader;
fu_input(pHeader);
printf( "%d,%s ",pHeader-> no,pHeader-> name);
free(pHeader); //最后记得释放空间
return 0;
}
而你的第二个方法不存在pHeader无效的问题,所以可以通过编译。。。。。
------解决方案--------------------
传递指针的引用或者返回分配内存后的指针值。
偶是用DEV编译器
一、
定义一个结构
struct aa
{
int no;
char name[20];
struct aa * pNext;
};
二、写函数分配内存空间及写入数据
fu_input( struct aa * pItem)
{
//如果在函数中分配内存空间,超出函数范围后内存就不存在了
pItem = ( struct aa *)malloc( sizeof(struct aa) );
pItem-> no=10;
strcpy(pItem-> name, "hello ");
pItem-> pNext = NULL;
}
三、主函数里调用
int main()
{
struct aa * pHeader;
fu_input(pHeader);
//返回主函数后pHeader为空的,printf调用时就会报错
printf( "%d,%s ",pHeader-> no,pHeader-> name);
return 0;
}
//但如果偶在主函数里分配内存空间,再调用就没有问题
//调整后函数如下:
fu_input( struct aa * pItem)
{
pItem-> no=10;
strcpy(pItem-> name, "hello ");
pItem-> pNext = NULL;
}
//主函数里调用时
pHeader = ( struct aa *)malloc( sizeof(struct aa ));
fu_input(pHeader); //在主函数中分配内存空间就没有问题
现向各位请教一下,在函数fu_input中申请内存空间后,超出函数范围后,该内存空间就不存了吗?
而要在主函数里申请内存空间
这是个什么概念?
小的初学,请各位出手指点
------解决方案--------------------
注意指针作为参数传递的概念
struct aa * pHeader; //这里使用前没有初始化
fu_input(pHeader);
//在这个函数调用后pHeader指针根本没有指向你分配的内存,而你分配的内存
//在函数调用完已经泄露了。。。
//原因如下
fu_input( struct aa * pItem)
{
// 你的参数是传递指针的拷贝,而不是引用,而开始
//的pHeader指针是没有初始化,及指向未知内存空间
//这时pItem指向了和pHeader一样的内存空间
pItem = ( struct aa *)malloc( sizeof(struct aa) );
//这条语句只是使pItem指针指向了分配的内存,pHeade指针没有变
//即还是未知内存
pItem-> no=10;
strcpy(pItem-> name, "hello ");
pItem-> pNext = NULL;
//函数结束时pItem生命期结束,而没有使用free(pItem)释放内存造成内存
//泄漏, 同时pHeade指针还是指向的最开始的未知内存
}
printf( "%d,%s ",pHeader-> no,pHeader-> name);
//由于在fu_input( struct aa * pItem)调用过程中pHeader从未变化
//还是一个未知内存,引用当然会出错
//可以这样小改一下
#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct aa
{
int no;
char name[20];
struct aa * pNext;
};
void fu_input( struct aa * &pItem)//这里改为传递指针的引用
{
pItem = ( struct aa *)malloc( sizeof(struct aa) );
pItem-> no = 10;
strcpy(pItem-> name, "hello ");
pItem-> pNext = NULL;
}
int main()
{
struct aa * pHeader;
fu_input(pHeader);
printf( "%d,%s ",pHeader-> no,pHeader-> name);
free(pHeader); //最后记得释放空间
return 0;
}
而你的第二个方法不存在pHeader无效的问题,所以可以通过编译。。。。。
------解决方案--------------------
传递指针的引用或者返回分配内存后的指针值。