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 ...
随机推荐
- C++多线程的几个重要方法解析CreateEvent / SetEvent /ResetEvent/ 等
1.CreateEvent 是创建windows事件的意思,作用主要用在判断线程退出,程锁定方面. 函功能描述:创建或打开一个命名的或无名的事件对象. HANDLE m_hExit; m_hExit= ...
- vim编译运行java程序
想让你的vim可以编译运行java只要在你的配置文件里面加一句: map <F5> :!javac %&&java %:r <CR> 就可以编译运行java了
- 安装ssh服务
1.先更新下源 sudo apt-get update 2.安装ssh服务 sudo apt-get openssh-server 3.配置ssh-server,配置文件位于/etc/ssh/sshd ...
- vs2012+qt5.2.0环境搭建
1.安装vs2012: 2.下载Qt 5.2.0 for Windows 32-bit(VS 2012, 579 MB) 和 Visual Studio Add-in 1.2.2for Qt5 注意: ...
- BZOJ 2124: 等差子序列
Sol 线段树+Hash. 首先暴力 等差子序列至少3项就可以了,就枚举中项,枚举公差就可以了,只需要一个数在中项前出现,另一个数在中项前没出现过就可以了.复杂度 \(O(n^2)\) 然后我想了一个 ...
- Intellij IDEA使用总结
查询快捷键CTRL+N 查找类CTRL+SHIFT+N 查找文件CTRL+SHIFT+ALT+N 查 找类中的方法或变量CIRL+B 找变量的来源CTRL+ALT+B 找所有的子类CTRL ...
- apache2 + virtualenv +djangocms
命令记录: cd /var/www makedir django cd django/ virtualenv env --no-site-packages source /var/www/django ...
- 【POI】修改Excel内容
package com.what21.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...
- linux下合并两个文件夹
一.我想把自己自定义的软件统一放到man手册路径里.如何和现有的/usr/local/share文件夹合并起来,原来的文件还在? (1)下面是解压出的自定义的bashdb调试软件==>
- Bitnami --https://bitnami.com/stacks
Bitnami is an app store for server software. Install your favorite applications in your own servers ...