(C/C++) Interview in English - Points.
Q: What is a dangling pointer?
A: A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning addresses of the automatic variables from a function or
using the address of the memory block after it is freed.
Q: What is Memory Leak?
A: Memory which has no pointer pointing to it and there is no way to delete or reuse this memory(object), it causes Memory leak.
{
Base *b = new base();
}
Out of this scope b no longer exists, but the memory it was pointing to was not deleted. Pointer b
itself was destroyed when it went out of scope.
Q: What is auto pointer?
A: The simplest example of a smart pointer is auto_ptr, which is included in the standard C++ library. Auto Pointer only takes care of Memory leak and does nothing about dangling pointers issue. You can find it in the header . Here is part of auto_ptr's implementation, to illustrate what it does:
template class auto_ptr
{
T* ptr;
public:
explicit auto_ptr(T* p = 0) : ptr(p) {}
~auto_ptr() {delete ptr;}
T& operator*() {return *ptr;}
T* operator->() {return ptr;}
// ...
};
As you can see, auto_ptr is a simple wrapper around a regular pointer. It forwards all meaningful operations to this pointer (dereferencing and indirection). Its smartness in the destructor: the destructor takes care of deleting the pointer. For the user of auto_ptr, this means that instead of writing:
void foo()
{
MyClass* p(new MyClass);
p->DoSomething();
delete p;
}
You can write:
void foo()
{
auto_ptr p(new MyClass);
p->DoSomething();
}
And trust p to cleanup after itself.
Q: What issue do auto_ptr objects address?
A: If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown.
Q: What is a smart pointer?
A: A smart pointer is a C++ class that mimics a regular pointer in syntax and some semantics, but it does more. Because smart pointers to different types of objects tend to have a lot of code in
common, almost all good-quality smart pointers in existence are templated by the pointee type.
Q: Is there any problem with the following : char*a=NULL; char& p = *a;?
A: The result is undefined. You should never do this. A reference must always refer to some object.
Q: What is the difference between a pointer and a reference?
A: A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects
while a reference always refers to an object with which it was initialized.
Q: What is the difference between const char *myPointer and char *const myPointer?
A: Const char *myPointer is a non constant pointer to constant data; while char *const myPointer is a constant pointer to non constant data.
Q:When should I use references, and when should I use pointers?
A: Use references when you can, and pointers when you have to. References are usually preferred over pointers whenever you don't need "reseating". This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside. The exception to the above is where a function's parameter or return value needs a "sentinel" reference a reference that does not refer to an object. This is usually best done by returning/taking a pointer, and giving the NULL pointer this special significance (references should always alias objects, not a dereferenced NULL pointer).
Note: Old line C programmers sometimes don't like references since they provide reference semantics that isn't explicit in the caller's code. After some C++ experience, however, one quickly realizes this is a form of information hiding, which is an asset rather than a liability. E.g., programmers should write code in the language of the problem rather than the language of the machine.
(C/C++) Interview in English - Points.的更多相关文章
- (C++) Interview in English. - Constructors/Destructors
Constructors/Destructors. 我们都知道,在C++中建立一个类,这个类中肯定会包括构造函数.析构函数.复制构造函数和重载赋值操作:即使在你没有明确定义的情况下,编译器也会给你生成 ...
- (C/C++) Interview in English - Basic concepts.
Question Key words Anwser A assignment operator abstract class It is a class that has one or more pu ...
- (C/C++) Interview in English - Class
Q: What is a class? A: A class is an expanded concept of a data structure: instead of holding only d ...
- (C/C++) Interview in English - Threading
Q. What's the process and threads and what's the difference between them? A. A process is an execut ...
- (C/C++ )Interview in English - Virtual
Q: What is virtual function?A: A virtual function or virtual method is a function or method whose be ...
- (C/C++) Interview in English. - Memory Allocation/Deallocation.
Q: What is the difference between new/delete and malloc/free? A: Malloc/free do not know about const ...
- step 1:begin to identify something in english(to becaome a baby again)
long long ago , i think if i want to improve my english especially computer english . i must do so m ...
- MS Chart 学习心得
利用Chart控件对学生信息进行统计,最终结果如下: Chart图形控件主要由以下几个部份组成: 1.Annotations --图形注解集合2.ChartAreas --图表区域集合3.Legen ...
- English interview!
Q1:Why are you interested in working for our company?为什么有兴趣在我们公司工作?A1:Because your company has a goo ...
随机推荐
- Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 自动将String类型的XML解析成实体类
package com.mooc.freemarker2dto; public class BaseDto { } package com.mooc.freemarker2dto; public cl ...
- JS初学之-点击元素,当前的显示样式,其他变灰色
点击按钮或者其他元素,当前的变化,其他的不变(比如选项卡按钮,点击当前的变为黄色,其他的不变色),这样的情况我们有两种思路: 1.全部清空,当前添加 for(var i=0;i<aBtn.len ...
- python 核心编程第5章(习题)
1.标准类型运算符. 写一段脚本,输入一个测验成绩,根据下面的标准,输出他的评分成绩(A-F). #coding:utf8 a = raw_input() a = int(a) if (a > ...
- 牧场安排(usaco NOV06.cowfood)
ohn新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地.FJ打算在牧场上的某几格土地里种上美味的草,供他的奶 ...
- jQuery插件 -- 表单验证插件jquery.validate.js, jquery.metadata.js
原文地址:http://blog.csdn.net/zzq58157383/article/details/7718352 最常使用JavaScript的场合就是表单的验证,而jQuery作为一个 ...
- 01-Java基础知识:数据类型与变量、标识符、运算符、表达式
Java基础知识:数据类型与变量.标识符.运算符.表达式 一.数据类型 Java定义了基本数据类型.引用数据类型.自定义类型. 八种基本数据类型:byte (1). short (2). int ( ...
- libc-glibc
glibc 和 libc 都是 Linux 下的 C 函数库. libc 是 Linux 下的 ANSI C 函数库:glibc 是 Linux 下的 GUN C 函数库. ANSI C 和 GNU ...
- .net framework4与其client profile版本的区别
简而言之,client profile版本是完整版本的一个子集.包含大多数最常用的功能并减小了体积,以便更容易的安装和发布,同时还有微软所声称的一些好处,比如因为减少了某些库,所以使其更加安全等等. ...
- 【性能诊断】八、并发场景的性能分析(windbg案例,连接泄露)
此前遇到一个项目反馈系统宕机问题,摘要描述如下: 系统不定期出现卡死现象,在多个模块不同功能上都出现过,未发现与特定功能相关的明显规律: 当系统出现卡死现象时,新的用户无法登陆系统: 跟踪应用服务器, ...