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

转载于:https://www.cnblogs.com/fdyang/p/4420274.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值