GeekInterview.com
Series: Subject: Topic:
Question: 70 of 267

Placement New Syntax

How can a C++ developer use the placement new syntax to make new allocate an object of class SomeClass at a particular memory address stored in a pointer type variable named pmem?

A. new SomeClass(pmem);
B. new(pmem) SomeClass;
C. new SomeClass pmem;
D. new pmem SomeClass;
E. new (pmem, SomeClass);
Asked by: dlee6565 | Member Since Nov-2008 | Asked on: Jul 22nd, 2009

View all questions by dlee6565

Showing Answers 1 - 6 of 6 Answers
bfeingold

Answered On : Jul 29th, 2009

View all answers by bfeingold

B. looks closest to correct.  I would use:

SomeClass* ptr = new(pmem)SomeClass();

Yes  4 Users have rated as useful.
  
Login to rate this answer.
krislogy

Answered On : Oct 13th, 2009

View all answers by krislogy

It is B , provided pmem is an allocated pointer (meaning memory already allocated to it).

See this link for more explanation: http://www.geekinterview.com/question_details/16559

Yes  2 Users have rated as useful.
  
Login to rate this answer.
tawofala

Answered On : Nov 14th, 2009

View all answers by tawofala

E. new (pmem, SomeClass);

  
Login to rate this answer.
sachin_mundhra

Answered On : Dec 18th, 2009

View all answers by sachin_mundhra

B) new(pmem) SomeClass;

Yes  2 Users have rated as useful.
  
Login to rate this answer.
akash_saxena

Answered On : Dec 22nd, 2009

View all answers by akash_saxena

It is
B. new(pmem) SomeClass;

pmem points to the memory which shall be used.

Yes  1 User has rated as useful.
  
Login to rate this answer.
ajrobb

Answered On : Sep 22nd, 2010

View all answers by ajrobb

B

Where you use placement new, it is important to explicitly call the destructor for the object before re-using the memory.

Fred * fred = new(pmem) Fred();
...
fred->~Fred();
fred = 0;

George * george(pmem) George();

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.