面试题Getmemory
来源:互联网 发布:尼康d800调焦软件 编辑:程序博客网 时间:2024/05/16 01:10
题目一:
- void GetMemory( char *p )
- {
- p = (char *) malloc( 100 );
- }
- void Test( void )
- {
- char *str = NULL;
- GetMemory( str );
- strcpy( str, "hello world" );
- printf( str );
- }
【运行错误】传入GetMemory(char* p)函数的形参为字符串指针,在函数内部修改形参并不能真正的改变传入形参的值。执行完
- char *str = NULL;
- GetMemory( str );
题目二:
- char *GetMemory( void )
- {
- char p[] = "hello world";
- return p;
- }
- void Test( void )
- {
- char *str = NULL;
- str = GetMemory();
- printf( str );
- }
题目三:
- void GetMemory( char **p, int num )
- {
- *p = (char *) malloc( num );
- }
- void Test( void )
- {
- char *str = NULL;
- GetMemory( &str, 100 );
- strcpy( str, "hello" );
- printf( str );
- }
- *p = (char *) malloc( num );
- if ( *p == NULL )
- {
- ...//进行申请内存失败处理
- }
也可以将指针str的引用传给指针p,这样GetMemory函数内部对指针p的操作就等价于对指针str的操作:
- void GetMemory( char *&p) //对指针的引用,函数内部对指针p的修改就等价于对指针str的修改
- {
- p = (char *) malloc( 100 );
- }
- void Test(void)
- {
- char *str=NULL;
- GetMemory(str);
- strcpy( str, "hello world" );
- puts(str);
- }
题目四:
- void Test( void )
- {
- char *str = (char *) malloc( 100 );
- strcpy( str, "hello" );
- free( str );
- ... //省略的其它语句
- }
- str = NULL;
题目五:
- char* GetMemory(int num)
- {
- char* p = (char*)malloc(100);
- return p;
- }
- void Test(void)
- {
- char* str = NULL;
- str = GetMemory(100);
- strcpy(str, "hello");
- cout<<str<<endl;
- }
题目六:
- char* GetMemory(void)
- {
- char* p = "hello world";
- return p;
- }
- void Test(void)
- {
- char* str = NULL;
- str = GetMemory();
- cout<<str<<endl;
- }<strong> </strong>
- strcpy(str, "hello test");
题目七:
- int* GetMemory(int* ptr)
- {
- ptr = new int(999);
- return ptr;
- }
- int main()
- {
- int *ptr1 = 0, *ptr2 = 0;
- ptr1 = GetMemory(ptr2);
- if(ptr1) { cout<<*ptr1<<'\n'; } else { cout<<"ptr1 == NULL\n"; }
- if(ptr2) { cout<<*ptr2<<'\n'; } else { cout<<"ptr2 == NULL\n"; }
- system("pause");
- return 0;
- }
999
ptr2 == NULL
阅读全文
0 0
- GetMemory面试题
- 面试题Getmemory
- getmemory面试题
- 【面试题】关于GetMemory的故事
- GetMemory函数详解 (360面试题)
- C语言面试题之——getmemory
- C++常见面试题—内存管理GetMemory()
- C++常见面试题—内存管理GetMemory()
- C/C++ 笔试面试(3)——内存管理GetMemory 确实遇到的面试题
- GetMemory
- GetMemory
- GetMemory()
- GetMemory
- GetMemory()
- GetMemory
- 面试题....
- 面试题
- 面试题
- MongoDB笔记三:C Driver 含日期类型字段document添加和查询,指定document返回的field
- live555二次开发经验总结:RTSPClient客户端与RTSPServer服务器
- 汇编语言实现冒泡算法
- leetcode-14-Longest Common Prefix
- UML图中的依赖、关联、继承、实现、聚合、组合关系的学习
- 面试题Getmemory
- Python fork 操作
- Django教程之八-----编写你的第一个Django应用(7)
- 如何保存python程序执行完毕后的全部变量
- Android中LeakCanary检测内存泄漏
- 李宏毅机器学习课程11~~~为何要"深"?
- String:459. Repeated Substring Pattern
- Centos 6.2 安装Mysql笔记
- List of Java keywords