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 ...
随机推荐
- BZOJ1030——文本生成器
给你若干给字符串,再给你一个m,问长度是m的字符串中包含给定字符串的数量mod 10007是多少 这个拿过来啥思路也没有,后来还是看了题解,才知道,原来,原来....那个带fail的Trie还可以搞别 ...
- GZIP压缩
(这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) zip压缩文件听说过,GZIP对我可是新鲜词儿,这个世界好复杂,压缩是无处不 ...
- [codeforces 339]E. Three Swaps
[codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...
- header()相关
header("Content-type: text/html; charset=utf-8"); header("refresh:3;url=biaodan.php?n ...
- 160809228_符瑞艺_C语言程序设计实验3 循环结构程序设计
#include <stdio.h> int main(){ //使用for循环完成1+2+......+100 ; ;i<=;i++) sum +=i; //sum = sum ...
- 无法解析类型 javax.servlet.http.HttpServletRequest。从必需的 .class 文件间接引用
java.lang.Error: 无法解析的编译问题: 无法解析类型 javax.servlet.http.HttpServletRequest.从必需的 .class 文件间接引用了它 无法解析类型 ...
- 7.2---蚂蚁相遇问题(CC150)
public class Ants { public double antsCollision(int n) { // write code here return (1 - Math.pow(0.5 ...
- django debug toolbar jquery加载配置
默认加载谷歌cdn的jquery: 显然国内是会悲剧的. 破解方案: 在settings.py中增加以下配置: DEBUG_TOOLBAR_CONFIG = {"JQUERY_URL&quo ...
- Node.js——Async
一:流程控制 为了适应异步编程,减少回调的嵌套,我尝试了很多库.最终觉得还是async最靠谱. 地址:https://github.com/caolan/async Async的内容分为三部分: 流程 ...
- Graph Valid Tree
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...