C++ 面试问题准备4

Memory Allocation/Deallocation

What is the difference between new/delete and malloc/free?
What is difference between new and malloc?

What is the difference between delete and delete[]?

What is difference between malloc()/free() and new/delete?
What is the difference between "new" and "operator new" ?
What is Memory alignment??

Is there a way to force new to allocate memory from a specific memory area?
How does free know the size of memory to be deleted.?
Can I free() pointers allocated with new? Can I delete pointers allocated with malloc()?
Why should I use new instead of trustworthy old malloc()?

Can I use realloc() on pointers allocated via new?
Do I need to check for NULL after p = new Fred()?
Do I need to check for NULL before delete p?
What are the two steps that happen when I say delete p?
In p = new Fred(), does the Fred memory "leak" if the Fred constructor throws an exception?

How do I allocate / unallocate an array of things?

What if I forget the [] when deleteing array allocated via new T[n]?

Can I drop the [] when deleteing array of some built-in type (char, int, etc)?

After p = new Fred[n], how does the compiler know there are n objects to be destructed
during delete[] p?
After p = new Fred[n], how does the compiler know there are n objects to be destructed
during delete[] p?

In p = new Fred(), does the Fred memory "leak" if the Fred constructor throws an exception?

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,951评论 0 23
  • 一方之地,咫尺之心。一语,道不尽世事沧桑;一剑,挥不尽万丈豪情。 这小小的一方之地,你可曾找到归宿,可曾,认得来时...
    一梦异时空阅读 132评论 0 0
  • 刚挂了电话,心里就倒腾了不停。给老爹买了个唱戏机。妈说他看的都瞌睡,还在看。还说,有个闺女真好…… 有点心酸...
    夏夜冬日阅读 249评论 5 3
  • 走近孔子 培植根的教育 垦利区第一实验小学 王洪梅 曲阜——孔子的故乡。带着对圣人的敬仰,在人间四月这个姹紫嫣红的...
    芦荻飞花阅读 335评论 0 1
  • 今天在微博上看到了这句挺逗逼的句子,于是脑海浮现了这副画面,一位提刀的侠客骑着马路过一棵松树下,问了一位正在松树下...
    荣大叔阅读 958评论 0 1