scope rule for pointers
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: scope rule for pointers

  1. #1
    Join Date
    Mar 2011
    Posts
    16

    Arrow scope rule for pointers

    <returnType> funct() {
    <type>* ptr=<address>;
    }

    at the end of the function ptr goes out of scope...does it also imply deletion of the <address> it pointed to? (by deletion i mean marked free)

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: scope rule for pointers

    It does not. If it did, and you had multiple pointers to the same object, the result would be multiple deletion.

    Raw pointers are dumb. You can google smart pointers (plenty of types of them are included by std::tr1 and C++0x) if you want a pointer that manages itself.

  3. #3
    Join Date
    Mar 2011
    Posts
    16

    Re: scope rule for pointers

    so if i ran such a program 1000 times the memory pointed each time (assuming new) would never be reclaimed and would result in memory leak?

  4. #4
    Join Date
    Aug 2008
    Posts
    902

    Re: scope rule for pointers

    Quote Originally Posted by ustulation View Post
    so if i ran such a program 1000 times the memory pointed each time (assuming new) would never be reclaimed and would result in memory leak?
    No. A memory leak (and most resource leaks) cannot persist after the application exits. The operating system is ultimately in control of those things, not your program.

  5. #5
    Join Date
    Mar 2011
    Posts
    16

    Re: scope rule for pointers

    i get confused abt scope/validity rules while guessing how stacks are popped (during runtime) when functions start returning references and pointers..thanks fr help

  6. #6
    Join Date
    Apr 1999
    Posts
    27,446

    Re: scope rule for pointers

    Quote Originally Posted by ustulation View Post
    i get confused abt scope/validity rules while guessing how stacks are popped (during runtime) when functions start returning references and pointers..thanks fr help
    The scope rules for pointers and references are no different than any other variable type. If that pointer or reference is declared locally, that local variable ceases to exist once the function returns.

    Regardsm

    Paul McKenzie

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: scope rule for pointers

    Quote Originally Posted by ustulation View Post
    i get confused abt scope/validity rules while guessing how stacks are popped (during runtime) when functions start returning references and pointers..thanks fr help
    No variable usage what so ever, pointer or otherwise, can ever leak memory. You can only get a memory leak if you actively allocate memory using one of the memory allocating functions (such as new) and then forget to deallocate it again using its matching deallocating function (in this case delete).

    All memory used by a program will be reclaimed by the OS when the program ends. So no memory leak will persist after the program has finished. If you allocate memory for the purpose of using it during the whole of a program you don't have to give it back actively. This is perfectly fine and doesn't even count as a leak because there's no element of mistake involved.
    Last edited by nuzzle; March 26th, 2011 at 02:43 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center