Bloomberg LP Interview Question for Developer Program Engineers






Comment hidden because of low score. Click to expand.
0
of 0 vote

class A {
X *x;
Y *y;
Z *z;
A(){
x = new X;
y = new Y;
z = new (nothrow)Z;
}

'nothrow' is a special object defined in header <new>. If the memory allocation fails for Z, instead of throwing a bad_alloc exception (which must be handled), 'new' returns back a NULL pointer and continues the execution of the program.

- avadh.786 September 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We could also add new handler and return.

void out_of_store() {
cerr << "\noperator new failed: out of store\n";
return;
}
class A {
X *x;
Y *y;
Z *z;
A(){
std::set_new_handler(out_of_store);
x = new X;
y = new Y;
z = new (nothrow)Z;
}
};

- sushanth October 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void out_of_store() {
cerr << "\noperator new failed: out of store\n";
return;
}
class A {
X *x;
Y *y;
Z *z;
A(){
std::set_new_handler(out_of_store);
x = new X;
y = new Y;
z = new Z;
}
};

- ignore nothrow above October 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

set_new_handler() is not appropriate here as we want to continue processing. unless set_new_handler() is able to resolve memory issue (out of scope of this problem), it has no other choice than exiting the program.

- Anonymous January 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// nothrow example
#include <iostream>     // std::cout
#include <new>          // std::nothrow

int main () {
  std::cout << "Attempting to allocate 1 MiB...";
  char* p = new (std::nothrow) char [1048576];
  if (p==0) std::cout << "Failed!\n";
  else {
    std::cout << "Succeeded!\n";
    delete[] p;
  }
  return 0;

}

- Anonymous October 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// nothrow example
#include <iostream>     // std::cout
#include <new>          // std::nothrow

int main () {
  std::cout << "Attempting to allocate 1 MiB...";
  char* p = new (std::nothrow) char [1048576];
  if (p==0) std::cout << "Failed!\n";
  else {
    std::cout << "Succeeded!\n";
    delete[] p;
  }
  return 0;

}

- Anonymous October 19, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More