void GetMemory( char *p )
{
p = (char *) malloc( 100 );
}
void Test( void )
{
char *str = NULL;
GetMemory( str );
strcpy( str, "hello world" );
printf( “%s”,str );
}
结果会打印什么?
本文通过一个简单的GetMemory函数示例,介绍了C++中指针使用不当可能导致的问题,包括内存泄漏和野指针,并探讨了如何避免这些问题。文章还讨论了形参与实参的区别及其在内存分配中的作用。
void GetMemory( char *p )
{
p = (char *) malloc( 100 );
}
void Test( void )
{
char *str = NULL;
GetMemory( str );
strcpy( str, "hello world" );
printf( “%s”,str );
}
结果会打印什么?
276
583
6193
3054

被折叠的 条评论
为什么被折叠?