http://blog.csdn.net/zhuxiaoyang2000/article/details/8084629

 #include <iostream>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <vector>
 #include <time.h>
 using namespace std;

 void GetMemory(char *p) {
     p = ();
 }
 int main(){
     char *str = NULL;
     GetMemory(str);
     strcpy(str, "hello world");
     printf("%s\n",str);
     ;
 }

请问运行Test函数会有什么样的结果?

答:程序崩溃。  因为GetMemory并不能传递动态内存, Test函数中的 str一直都是 NULL。 strcpy(str, "hello world");将使程序崩溃。

 #include <iostream>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <vector>
 #include <time.h>
 using namespace std;

 char *GetMemory(void) {
     char p[] = "hello world";
     return p;
 }
 int main(){
     char *str = NULL;
     str = GetMemory();
     printf("%s\n",str);
     ;
 }

请问运行Test函数会有什么样的结果?

答:可能是乱码。  因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是 NULL,但其原现的内容已经被清除,新内容不可知。

 #include <iostream>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <vector>
 #include <time.h>
 using namespace std;

 void GetMemory(char **p, int num) {
     *p = (char *)malloc(num);
 }
 int main(){
     char *str = NULL;
     GetMemory(&str, );
     strcpy(str, "hello");
     printf("%s\n",str);
 //    if(str != NULL){
 //        free(str);
 //        str = NULL;
 //    }
     ;
 }

请问运行Test函数会有什么样的结果?

答:  (1)能够输出hello (2)内存泄漏

 #include <iostream>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <vector>
 #include <time.h>
 using namespace std;

 int main(){
     );
     strcpy(str, "hello");
     free(str);
     if(str != NULL)  {
         strcpy(str, "world");
         printf("%s", str); }
     ;
 }

请问运行Test函数会有什么样的结果?

答:篡改动态内存区的内容,后果难以预料,非常危险。  因为free(str);之后,str成为野指针, if(str != NULL)语句不起作用。

C++面试之GetMemory问题的更多相关文章

  1. C语言面试

    最全的C语言试题总结 第一部分:基本概念及其它问答题 1.关键字static的作用是什么? 这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1). 在函数体,一个被 ...

  2. C/C++ 笔试、面试题目大汇总(二)

    一.找错题 试题1: void test1() { charstring[10]; char* str1 ="0123456789"; strcpy( string, str1 ) ...

  3. 【C++基础】内存操作 getMemory改错

    内存操作的考察点:①指针 ②变量生存期及作用范围 ③动态内存申请和释放 笔试题************************************************************* ...

  4. 【转】C++ 笔试面试题目

    原文:http://blog.csdn.net/txgc1009/article/details/6700830 许多面试题看似简单,却需要深厚的基本功才能给出完美的解答.企业要求面试者写一个最简单的 ...

  5. iOS开发——面试指导

    iOS面试指导 一 经过本人最近的面试和对面试资料的一些汇总,准备记录这些面试题,以便ios开发工程师找工作复习之用,本人希望有面试经验的同学能和我同时完成这个模块,先出面试题,然后会放出答案. 1. ...

  6. iOS面试贴士

    iOS面试小贴士 ———————————————回答好下面的足够了------------------------------------- 多线程.特别是NSOperation 和 GCD 的内部原 ...

  7. 改错+GetMemory问题

    试题1: void test1() { ]; "; strcpy( string, str1 ); } 试题2: void test2() { charstring[],str1[]; in ...

  8. C++面试宝典2011版

    1.new.delete.malloc.free关系 delete会调用对象的析构函数,和new相应free仅仅会释放内存,new调用构造函数.malloc与free是C++/C语言的标准库函数,ne ...

  9. (转)C/C++ 程序设计员应聘常见 面试笔试 试题深入剖析

    C/C++ 程序设计员应聘常见 面试笔试 试题深入剖析 http://www.nowcoder.com/discuss/1826?type=2&order=0&pos=23&p ...

随机推荐

  1. phpcms二次开发学习

    1.新建模块就是phpcms/modules/目录下面新建文件夹,文件夹名即为模块名. 2.模块内 一般新建三个文件夹:classes(模块要使用的类放置在这个文件夹,通过pc_base::load_ ...

  2. android socket编程用Bufferreader读取的一个失败教训

    由于我的手机需要用笔记本开的wifi,躺在床上玩手机时需要关电脑或者是要让电脑放歌的时候总是不想下床,于是我想能不能用一个APP,然后通过局域网实现在手机上对电脑进行操控呢?说干就干. 我在电脑上用的 ...

  3. 关于NGUI与原生2D混用相互遮盖的问题心得

    http://www.fzgh.org.cn/zuixindianying/144224.html Native2D自己可以使用Sort Layer来排序,每层又有不同的Order In Layer, ...

  4. Android解析服务器Json数据实例

    Json数据信息如下: { "movies": [ { "movie": "Avengers", "year": 201 ...

  5. 34 网络相关函数(二)——live555源码阅读(四)网络

    34 网络相关函数(二)——live555源码阅读(四)网络 34 网络相关函数(二)——live555源码阅读(四)网络 2)socketErr 套接口错误 3)groupsockPriv函数 4) ...

  6. PHP 文件下载流程

    前台HTML: 添加download属性,不打开download.php页面 <a style='color:blue' href='download.php' download='data/C ...

  7. Linux Ruijie登录命令

    cd rjsupplicant chmod +x rjsupplicant.sh sudo ./rjsupplicant.sh -u 1550590×××× -p ××××× -d 1

  8. MorkDown 常用语法总结

    推荐一款很好用的markdown编辑器:http://www.typora.io/ 基本技巧: 代码高亮 如果你只想高亮语句中的某个函数名或关键字,可以使用``实现 通常编辑器根据diamagneti ...

  9. pycharm 2016 注册码

    43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiI ...

  10. Android Studio (Gradle)编译错误

    Error:Execution failed for task ':app:processDebugResources' .com.android.ide.common.process.Process ...