HCL Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

bad_alloc execption is thrown. But if nothrow is used then it returns NULL on failure.

int* p = new int[10]; // bad_alloc exception will be thrown if allocation fails

int* p = new (nothrow) int[10] // p will be NULL if fails

- Pankaj August 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a complete example below that you can compile

#include <new>

int main()
{
    unsigned long long n = 1ULL << 40;

    char *x = new (std::nothrow) char[n];
}

- Anonymous August 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you use normal new then if the allocation fails, NULL WILL NOT be assigned to the pointer because internally it uses temp pointer to allocate memory and only if it is successful, then its address is copied to your poiniter, else exception is thrown.

- crystal.rishi2 August 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the interviewer is looking for the answer wherein it calls the new_handler.

- Random February 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

set_new_handler allows you to specify a function to be called when new operator fails to allocate memory.
nothrow is limited utility as it applies only for memory allocation, subsequent call to constructor may throw an exception.

- amruta June 11, 2014 | 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