AceTheInterview
Jobs in Pune | Work better in teams | Socialize with friends | Submit Q&A | Tell a friend
Search site for 

Top 100 Interview Questions & Answers in a convenient and easy to read book!

“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”

– Ravi, California

Read more comments...

Interview Questions And Answers RSS Feed

Answers »

  1. Submitted By: rithika — October 16, 2006
    -45 votes
      + -

    1)ptr=new object

    whenever a new object is created the ptr points to that object

    2)object o;ptr=&o;

    In this the pointer ptr points to the address of the object o and not the object itself

  2. Submitted By: Nirvahna — October 17, 2006
    +68 votes
      + -

    The first answer is wrong. The correct answer is:

    In case 1, the object is on the heap.
    In case 2, the object is on the stack.

  3. Submitted By: Vitaliy Platonov — November 18, 2006
    -6 votes
      + -

    It would create a memory leak, becase after assignment on 2 wouldn’t be possible to delete object created on heap.

  4. Submitted By: Chini — January 30, 2007
    +0 votes
      + -

    The second answer is correct , but in 3rd answer, i think its otherway around, memory management in stack is automatic but not in heap, so after the program execution it will be deleted and memory will be released

  5. Submitted By: daniel — February 14, 2007
    +11 votes
      + -

    1. ptr = new Object;
    ptr is created on the stack
    Object is dynamically created on the heap
    This method is suitable for large data structure or unknown size Object. It can be accessed anytime.
    delete ptr is needed to prevent memory leak

    2. Object o; ptr = &o;
    ptr and Object are both created on the stack.
    both will be cleaned up automatically.
    While Object is NOT created dynamically, no more memory can be allocated for Object. Stack object is suitable for small or primitive type object.

    Please point to me if there is anything incorrect.

  6. Submitted By: Nasimudeen.A — March 30, 2007
    +2 votes
      + -

    In first stmt the object is created dynamically from heap area and the address assigned to ptr.

    Second case an object is created in the stack at compile time and then the address of this existing object assigned to the ptr.

    ptr is an object pointer so it can be initialized with any addres of object/dynamic.
    But the problem here the dynamically allocated memory wasted upto the program termination.

  7. Submitted By: FanZai — July 3, 2007
    -3 votes
      + -

    the 6rd is correct,
    but in case 2, if the function return ptr, there would be some uncertain error.

  8. Submitted By: Oleksandr — October 23, 2007
    not yet rated
      + -

    new Object does not necessary creates object in the heap, operator new can be overloaded.

    The main difference is that in first case object will be deleted when it will out the scope, and ptr will point to released memory (or to some trash).

    The second way requires to delete object manually by it’s address, stored in ptr or it will be automatically released after program unload (not in all operating systems)

  9. Submitted By: Varun — January 19, 2008
    +2 votes
      + -

    the ptr is always created on the stack.

    Case 1:
    Object *ptr;
    ptr is created on stack. 4 bytes of memory on stack is allocated for storing this ptr. But the value that this ptr holds is null (empty)

    ptr = new Object;

    A new object of Object class is created on the heap. The ptr now contains the value which is the address of the new object created on the heap.

    You will have to do an explicit delete p to release the memory from the heap. Else this results in a memory leak.

    Case 2
    Object *ptr;
    Same thing as above.

    Object O;
    A new object is created in the memory on stack. The runtime system will take care of freeing the memory of objects that are created on the stack.

    Now
    ptr = &O;

    Now the ptr (its value) will hold the address of the object O, but this address is a stack address.

    So when you get out of scope please donot do a delete. If you do a delete on ptr, the runtime system will try to deallocate / free the memory location on the stack and this will throw an exception.

    The reason why this is not allowed is probably because, stack memory is any way deallocated when an object goes out of scope, so an explicit delete on stack should not be allowed.

  10. Leave an Answer/Comment

    To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
    Click to hear an audio file of the anti-spam word

Our Sponsors
Our Sponsors
Contact Us | FAQ | Sitemap | Terms of Use | Privacy Policy | Tell a Friend

Copyright © 1999-2006 Jeeve Technologies LLC. All rights reserved.