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.的更多相关文章

  1. (C++) Interview in English. - Constructors/Destructors

    Constructors/Destructors. 我们都知道,在C++中建立一个类,这个类中肯定会包括构造函数.析构函数.复制构造函数和重载赋值操作:即使在你没有明确定义的情况下,编译器也会给你生成 ...

  2. (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 ...

  3. (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 ...

  4. (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 ...

  5. (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 ...

  6. (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 ...

  7. 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 ...

  8. MS Chart 学习心得

    利用Chart控件对学生信息进行统计,最终结果如下: Chart图形控件主要由以下几个部份组成: 1.Annotations --图形注解集合2.ChartAreas  --图表区域集合3.Legen ...

  9. English interview!

    Q1:Why are you interested in working for our company?为什么有兴趣在我们公司工作?A1:Because your company has a goo ...

随机推荐

  1. 《C标准库》——之<stdarg.h>

    C语言有个很强大的功能,依靠它,实现了printf等这类有着变长参数列表的函数或者宏.它就是在<stdarg.h>里的变长参数. 内容: va_list :它是一个适合保存va_start ...

  2. Oracle--存储过程学习进阶

    例1:该存储过程是向xuesheng 表中插入一行数 create or replace procedure student_proc_no is begin , , ); commit; end s ...

  3. 【转】beancopy的替代方案

    链接:http://jingyan.baidu.com/article/215817f7d55b871edb14235b.html 最近在项目中接触到了BeanUtils.copyProperties ...

  4. activiti工作流数据库表详细介绍 (23张表)

    Activiti的后台是有数据库的支持,所有的表的表名都以ACT_开头,表名的第二部分是用来表示表的用途的两个字母标识. 用途也和服务的API对应. ACT_RE_*: 'RE'表示repositor ...

  5. 《苹果开发之Cocoa编程》挑战2 创建一个数据源 练习

    <苹果开发之Cocoa编程>第4版 P87 创建一个to-do list应用程序,在文本框中输入任务.当用户单击Add按钮时,添加字符串到一个变长队列,新任务就出现在list的末尾. 关键 ...

  6. Linux驱动设计—— 中断与时钟

    中断和时钟技术可以提升驱动程序的效率 中断 中断在Linux中的实现 通常情况下,一个驱动程序只需要申请中断,并添加中断处理函数就可以了,中断的到达和中断函数的调用都是内核实现框架完成的.所以程序员只 ...

  7. jsonp跨域js

    http://www.cnblogs.com/yuzhongwusan/archive/2012/12/11/2812849.html window.opener用法 http://www.cnblo ...

  8. caffe: train error: Serializing 25 layers--- Check failed: proto.SerializeToOstream(&output)

    I0221 21:47:41.826748  6797 solver.cpp:259]     Train net output #0: loss = 0.00413362 (* 1 = 0.0041 ...

  9. Qt消息机制和事件(二)

    三,事件过滤器 有时候,对象需要查看.甚至要拦截发送到另外对象的事件.例如,对话框可能想要拦截按键事件,不让别的组件接收到:或者要修改回车键的默认处理. 通过前面的章节,我们已经知道,Qt 创建了QE ...

  10. HTTPS-使用Fiddler抓取HTTPS数据包原理

    最近想尝试基于Fiddler的录制功能做一些接口的获取和处理工作,碰到的一个问题就是简单连接Fiddler只能抓取HTTP协议,关键的登录请求等HTTPS协议都没有捕捉到,所以想让Fiddler能够同 ...