Bloomberg LP Interview Question Software Engineer / Developers

  • bloomberg-lp-interview-questions
    0
    of 0 votes
    13
    Answers

    what is the difference between

    T *o = new T
    and
    T *o = new T()

    - Anonymous on May 05, 2011 Report Duplicate | Flag
    Bloomberg LP Software Engineer / Developer C++



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

If there is a default constructor then both are same, otherwise different.
According to the latest C++ 2003 standard,
new T calls default initializer and,
new T() calls value initializer.

No constructor :
new T:
It causes default initialization of non-POD(*1) types.
POD types remain uninitialized. That is, indeterminate value.
new T():
For non-POD types default initialization.
POD types are zero initialized.

If there is a constructor which doesn't list the variables, then the same thing happens, default initialization for non-POD and zero/random values for POD according to new()/new.

*1. POD is Plain Old Data. That is a C-like struct. Ctors/Dtors/Funcs. etc make it non-POD.

- maX on June 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If T is a user defined class/struct, there is no difference.
However,
int *o = new int; // *o has not been initialized
int *o = new int(); // *o has been default initialized, ie *o == 0

- Anonymous on May 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Tested, the output is the same

1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 int *o = new int;
8 int *p = new int();
9 cout << *o << endl;
10 cout << *p << endl;
11 }

./a.out
0
0


~

- Anonymous on June 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey I tested that code too.. result is different..
int *w=new int; prints garbage and the other one prints zero

- rocks on September 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do you have an idea about why this should happen.

- Anonymous on March 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If T is POD, there is no difference between
new T;
new T ();
in both cases all members initialized.

But I was surprised to find that:
T t;

leaves all class members uninitialized

- Leo on March 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

both have the same functionality...in first case the constructor is called implicitly while in the second case constructor is explicitly called

- camSun on May 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If this is the case then below code should not compile:

class test
{
public:
	explicit test() {
		cout<<"constructor called"<<endl;
	}
};

void main()
{
	test *ptr1 = new test;
	test *ptr2 = new test();
	
	getchar();
}

- jassi on May 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you got it

- camSun on May 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

gud

- prashant on May 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

explicit has no effect on default constructors

- Anonymous on January 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice

- siva.sai.2020 on May 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

no you are wrong!! this code compiles and runs too!!

- Anonymous on November 10, 2012 | Flag


Add a Comment
Name:

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

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

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