Thread: Creating Objects

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Creating Objects

    Assume Emp is a class

    Code:
    Emp o;
    here, is o itself the object? , if so how is memory allocated for it, implicity via call to new or malloc() ?


    Code:
    Emp *o = new Emp();
    in C++ when objects go out of scope their destructors are automatically called

    so, what if i dont call
    Code:
    delete o;
    and let the pointer go out of scope, for it to call the destructor for the object automatically?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    here, is o itself the object? , if so how is memory allocated for it, implicity via call to new or malloc() ?
    Implicitly. If it's an ordinary automatic local variable like this:
    Code:
    void function() {
        Emp o;
    }
    then whenever function() is executed, enough memory is reserved on the stack to hold o. This enables each call to function to have its own copy of Emp, allowing recursion.

    [edit] You only create objects with new when you're storing those objects in pointers. [/edit]

    malloc() and new allocate memory on the heap rather than on the stack.

    Code:
    Emp *o = new Emp();
    in C++ when objects go out of scope their destructors are automatically called

    so, what if i dont call
    Code:
    delete o;
    and let the pointer go out of scope, for it to call the destructor for the object automatically?
    Destructors are only called for objects allocated on the stack. Objects allocated with new must be deleted for the destructor to be called and the memory to be freed. This allows one function to allocate some memory, and return a pointer to that memory. The calling function will be able to use that memory because it wasn't freed when the allocating function returned.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >here, is o itself the object?
    Yes.
    >if so how is memory allocated for it, implicity via call to new or malloc() ?
    Space is set aside for the object in the area used by the compiler to store automatic (local) variables, usually the stack.

    >in C++ when objects go out of scope their destructors are automatically called
    True for local (automatic) variables like the first o above. Not true for objects created by operator new like the second o above.

    >so, what if i dont call
    Code:
    >delete o;
    >and let the pointer go out of scope, for it to call the destructor for the object automatically?
    For every call to new you need a corresponding call to delete. Otherwise the memory for that object is never deallocated, until possibly when your program ends, and most operating systems will reclaim it.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    If the object is created using malloc()

    do i need to call the destructor for the object explicitly and then deallocate memory using free()?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, objects created with malloc() don't get their constructor called, so why should they get destructed? Don't allocate classes with malloc(). There's no reason to.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    Yeah, rite would be inappropriate to use malloc() for object creation

    However i m letting my mind flow and thought of this

    Code:
    int main()
    {
       demo *q = create();    // assume demo class
       cout<< q->show();     // assume show() in demo
       destroy(q);    
    }
    
    demo* create()
    {
       demo *p = (demo*)malloc(sizeof(demo));            // allocate space
       p -> demo();              // call constructor
       return(p);
    }
    
    void destroy(demo*q)
    {
       q -> ~demo();              // call destructor
       free(q);                        // deallocate space
    }
    any suggestions?

  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by vb.bajpai View Post
    any suggestions?
    yes. don't do that.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    int main()
    {
       demo *q = new demo;
       cout<< q->show();     
       delete q;    
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by vart View Post
    Code:
    int main()
    {
       demo *q = new demo;
       cout<< q->show();     
       delete q;    
    }
    or even more betterer...
    Code:
    int main()
    {
       auto_ptr<demo> q(new demo);
       cout<< q->show();     
    }
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Creating a map of objects with variable width and height
    By MrSparky in forum C++ Programming
    Replies: 6
    Last Post: 07-30-2007, 03:06 PM
  3. Creating array of objects w/o default constructor
    By QuestionC in forum C++ Programming
    Replies: 19
    Last Post: 05-02-2007, 08:03 PM
  4. A question about dynamically creating an array of objects
    By edd1986 in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2006, 12:30 PM
  5. creating objects in c++
    By sachitha in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2004, 12:19 PM