Pointer life & scope

This is a discussion on Pointer life & scope within the C++ Programming forums, part of the General Programming Boards category; If I create a pointer using the "new" keyword inside a function and pass that to a class constructor, what ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Pointer life & scope

    If I create a pointer using the "new" keyword inside a function and pass that to a class constructor, what happens to the memory space and the object there? Will it live until I delete it in the class' destructor? The function leaving its scope shouldn't hurt it right? (The class object created is then returned from this function and used elsewhere)

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by 6tr6tr View Post
    If I create a pointer using the "new" keyword inside a function and pass that to a class constructor, what happens to the memory space and the object there? Will it live until I delete it in the class' destructor? The function leaving its scope shouldn't hurt it right?
    The only way an object created with "new" will ever go away is by a "delete." A bare pointer has no destructor, it just goes away, leaving the object it was pointing to hanging in space.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by brewbuck View Post
    The only way an object created with "new" will ever go away is by a "delete." A bare pointer has no destructor, it just goes away, leaving the object it was pointing to hanging in space.
    Cool, thanks!

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    For every new or new [] there is an equal and opposite delete or delete [].
    Function scope has nothing to do with heap memory, only stack variables (i.e. non-newed variables).

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by brewbuck View Post
    The only way an object created with "new" will ever go away is by a "delete." A bare pointer has no destructor, it just goes away, leaving the object it was pointing to hanging in space.
    Didn't he say he was deleting in it the class destructor?

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by medievalelks View Post
    Didn't he say he was deleting in it the class destructor?
    The question was "will it live until I delete it" which implies he is not presently deleting it, otherwise the question would not have been asked.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by brewbuck View Post
    The question was "will it live until I delete it" which implies he is not presently deleting it, otherwise the question would not have been asked.

    He's not presently deleting it as in as he typed the message? If he deletes it in the dtor, he deletes it.

    Personally, I'd rather see him new it in the ctor, but that's another story.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's also a highly use-case-specific one. For example, the class could expect to be passed a pointer to something polymorphic in the constructor, for its own exclusive use. (Of course, the constructor should then take a std::auto_ptr.) The class might not even know the particular implementation passed exists.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Replies: 5
    Last Post: 04-04-2009, 04:45 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 06:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21