Thread: How objects are stored

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    The objects I am dealing with are quite large, and often will be created in seperate functions. If you are supposed to use pointers for big objects, this brings back the problem of them going out of scope when the object-creating function returns.

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by MTK View Post
    The objects I am dealing with are quite large, and often will be created in seperate functions. If you are supposed to use pointers for big objects, this brings back the problem of them going out of scope when the object-creating function returns.
    When the object-creating function returns, the memory dynamically allocated by new will only be deallocated manually. So it works fine, you just need to handle memory management yourself. If for instance you didn't call delete, your program would probably still work, but with a lot of memory leaks. Instead of a pointer, use a smart pointer, and you can use it the same way as Java without any issues (object = create_object()). Most people will recommend you instead allocate an object, and pass it by reference to the object-creating function for initialization (create_object(object)). You get the same result, without pointers, and the function user can handle memory themselves. You can then omit the return value, or use a status value.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector of pointers to vector of objects
    By Litz in forum C++ Programming
    Replies: 10
    Last Post: 11-06-2009, 03:29 PM
  2. Objects as attributes of other objects.
    By kbro3 in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2009, 03:46 PM
  3. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM