IBM Interview Question for Software Engineer / Developers






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

The difference between "new" and "malloc" is that, if you use new, you are not only allocate the memory for the object, but also call the constructor for the object.

For example

Class Simple
{
public:
Simple()
{//constructor code.
}
}

If you "new" the class, the constructor code will be called, if you use malloc, only memory for the class will be allocated, no constructor code will be called.

- jianwei.sun September 13, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you directly cast the memory location, it will give you un-expected results.. during the construction of object, thing like virtual table pointers are set.
following should work..
if A is class
CreateInstance()
{
obj = (A*)malloc(size);
obj->A::A();
return obj;
}

- krishna September 18, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One key thing is: you need to know the size of memory in order to use malloc. Also, it may fail.
You also need to call free() to free the memory instead of using delete.

- kulang October 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i guess there is one more issue with using malloc...it doesnot take care of alignment issues, I mean that if you do malloc(40) for 10 integers, it doesnot guarantee 4-byte alignment..PLZ COMMENT if I am wrong

- nitesh October 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess if you use specific type of pointer to cast it, it would be fine, like (int *) pr = malloc( .. )

- shoushou January 16, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

malloc returns aligned addresses

- RKS June 28, 2009 | Flag
Comment hidden because of low score. Click to expand.
-1
of 0 vote

true, malloc will allocate the space you have requested. the question mentions c++. please note that malloc returns a void pointer. in c++ you cannot type cast a void pointer unlike c. hence we use new operator in c++ because new returns the pointer corresponding to the class being instantiated.

- burdell September 14, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the primary issue is the call to the constructor. there is no problem with the casting as mentioned above. infact, even if some compilers refuse to cast 'c-style', you could use a static_cast or a reinterpret_cast to achieve this. as long as the size given to malloc is right you will get a proper object (uninit ofcourse)

- meglo September 16, 2008 | Flag


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