Thread: simple scope question

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    It is not valid memory, at any time some other program can overwrite the memory and therefore the pointer points to bad memory. This is something you will have to consider when using pointers. This can be shown with this little example:
    Code:
    #include <iostream>
    using namespace std;
    
    class Foo
    {
    public:
        Foo() { cout << "Foo constructor" << endl; }
        ~Foo() { cout << "Foo destructor" << endl; }
    };
    
    int main()
    {
        Foo* bar;
        {
             cout << "New scope" << endl;
             Foo temp;
             bar = &temp;
             cout << "Back to main scope" << endl;
        }
        cout << "In main scope" << endl;
        // Now bar points to bad memory and should not ever be used here without being reassigned to different memory
        cin.get();
    }
    Last edited by Shakti; 01-29-2005 at 03:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  2. A simple question about scope
    By dwylie in forum C Programming
    Replies: 3
    Last Post: 12-10-2004, 01:16 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM
  5. Scope question
    By mikebrewsj in forum C++ Programming
    Replies: 1
    Last Post: 01-17-2002, 04:47 PM