C++面试之GetMemory问题
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问题的更多相关文章
- C语言面试
最全的C语言试题总结 第一部分:基本概念及其它问答题 1.关键字static的作用是什么? 这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1). 在函数体,一个被 ...
- C/C++ 笔试、面试题目大汇总(二)
一.找错题 试题1: void test1() { charstring[10]; char* str1 ="0123456789"; strcpy( string, str1 ) ...
- 【C++基础】内存操作 getMemory改错
内存操作的考察点:①指针 ②变量生存期及作用范围 ③动态内存申请和释放 笔试题************************************************************* ...
- 【转】C++ 笔试面试题目
原文:http://blog.csdn.net/txgc1009/article/details/6700830 许多面试题看似简单,却需要深厚的基本功才能给出完美的解答.企业要求面试者写一个最简单的 ...
- iOS开发——面试指导
iOS面试指导 一 经过本人最近的面试和对面试资料的一些汇总,准备记录这些面试题,以便ios开发工程师找工作复习之用,本人希望有面试经验的同学能和我同时完成这个模块,先出面试题,然后会放出答案. 1. ...
- iOS面试贴士
iOS面试小贴士 ———————————————回答好下面的足够了------------------------------------- 多线程.特别是NSOperation 和 GCD 的内部原 ...
- 改错+GetMemory问题
试题1: void test1() { ]; "; strcpy( string, str1 ); } 试题2: void test2() { charstring[],str1[]; in ...
- C++面试宝典2011版
1.new.delete.malloc.free关系 delete会调用对象的析构函数,和new相应free仅仅会释放内存,new调用构造函数.malloc与free是C++/C语言的标准库函数,ne ...
- (转)C/C++ 程序设计员应聘常见 面试笔试 试题深入剖析
C/C++ 程序设计员应聘常见 面试笔试 试题深入剖析 http://www.nowcoder.com/discuss/1826?type=2&order=0&pos=23&p ...
随机推荐
- BZOJ3277——串
0.题意:给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串(注意包括本身). 1.分析:这个题我问了吴大爷做法 首先建立后缀自动机,然后利用离线搞出每一个 ...
- gradle类重复的问题解决方法
今天遇到一个gradle的类重复问题,学习到一个命令 gradle -q dependencies,可以查看项目里包的依赖关系,发生这个错误是因为我用了一个相册的项目,这个项目里用到了v4包,我自己的 ...
- [codeforces 317]A. Perfect Pair
[codeforces 317]A. Perfect Pair 试题描述 Let us call a pair of integer numbers m-perfect, if at least on ...
- homework160809207刘兆轩
# include <stdio.h> int main() { float a,b,c,m,n,l,k,j,i; printf("请输入三个数:\n"); scanf ...
- git 教程(7)--撤销修改
自然,你是不会犯错的.不过现在是凌晨两点,你正在赶一份工作报告,你在readme.txt中添加了一行:
- linux kernel 杂谈
首先介绍一下背景吧,工作三个星期了.复习了一波u-boot,跟了一下事件上报,搞了下平台设备,扣了一个内存检查代码. 想想生活是不是有点无聊.对啊,真的很无聊!!!! 无聊也没有办法啊,所以找点方法去 ...
- 一个完整的Http请求
一个完整的http请求,通常有以下7点: 1.建立tcp连接 2.web浏览器web服务器发送请求命令 3.web浏览器发送请求头信息 4.web服务器应答 5.web服务器发送应答信息 6.web服 ...
- http statusCode(状态码)
转自:1) http://specs.openstack.org/openstack/keystone-specs/api/v3/identity-api-v3.html#unauthorized ...
- Shell数组相关操作
参考:http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html 创建数组 a=( ) 获得数组长度 ${#a[@]} #${#变量} ...
- 十一天 python操作rabbitmq、redis
1.启动rabbimq.mysql 在""运行""里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd, ...