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 ...
随机推荐
- Web项目,F12调试的说明
sessionstorage,localstorage和cookie之间的区别 区别:cookie数据始终在同源的http请求中携带(即使不需要),即cookie在浏览器和服务器间来回传递.而sess ...
- 获取action name在asp.net mvc
Update for MVC 3 ViewContext.Controller.ValueProvider.GetValue("action").RawValue ViewCont ...
- 《C和指针(Pointer on c)》 学习笔记(转自:http://dsqiu.iteye.com/blog/1687944)
首先本文是对参考中三个连接的博客进行的整理,非常感谢三位博主的努力,每次都感叹网友的力量实在太强大了…… 第一章 快速上手 1. 在C语言中用/*和*/来注释掉这段代码,这个实际上并不是十分的安全, ...
- Unity3D获取Andorid设备返回键,主页键等功能
在Unity开发中捕捉Android的常用事件其实很简单 在新建的脚本文件中就加入: 比如: // 返回键 if ( Application.platform == RuntimePlatform.A ...
- 项目: 推送水木文章到Kindle
代码在github:https://github.com/Viyu/PushNewsmth2Mail 当年买Kindle的时候,想再Kindle上阅读水木的帖子,但Kindle的Web上网体验太差,想 ...
- VS2012创建UML项目
1.选择建模工具 2.添加新建项 3.添加UML图或用例图 4.打开工具箱添加
- 泛型约束 where T : class,new()
假如有这样一个方法签名 public List<T> GetSomethingList<T> (int a,int b,string c) where T:class,new( ...
- 基础知识《七》---Java多线程详解
- zookeeper部署及集群测试
zookeeper部署及集群测试 环境 三台测试机 操作系统: centos7 ; hostname: c1 ; ip: 192.168.1.80 操作系统: centos7 ; hostname: ...
- rabbitMQ中vhost虚拟主机的理解
每个virtual host本质上都是一个RabbitMQ Server,拥有它自己的queue,exchagne,和bings rule等等.这保证了你可以在多个不同的application中使用R ...