Thread: Pointers

  1. #1
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Pointers

    Quick question (that is probably a stupid one):

    Do pointers go out of the scope when a function/class ends?

    I just noticed when reading tutorials or ebooks, that sometimes the writer will set the deleted pointer to 0, and sometimes he will not (assuming the pointer is at the end of the function/class).
    ie. at the end of a function: delete pointer; }
    but in the class deconstructor: delete pointer; pointer = 0; }

    Wouldnt mind knowing so I can just leave out setting it to 0 in my code when its about to go out of the scope.

    Youd think I'd know this.. I would have had to have read about it once before... though most documents only tell you that after deleting the allocated memory the point points to it still points there so you should set it to NULL or 0 to make it safe, it also says not to call delete on a NULL or 0 pointer, and it says local variables go out of the scope.. but is a pointer (to a variable) a variable?
    Warning: Have doubt in anything I post.

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

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    did you try it? here's some sample code:
    Code:
    #include<iostream>
    
    struct test
    {
            int i;
    };
    
    int main()
    {
            {
                    test*f=new test;
                    f->i=50;
                    std::cout<<f->i<<std::endl;
                    delete test;
            }
            std::cout<<f->i<<std::endl;	//line 16
            return 0;
    }
    and this is what you get:
    Code:
    jshao@MCP ~/Programming/C++ $ g++ test.cpp -Wall -o test.exe
    test.cpp: In function `int main()':
    test.cpp:16: error: `f' undeclared (first use this function)
    test.cpp:16: error: (Each undeclared identifier is reported only once for each
       function it appears in.)
    basically, everything follows the rules of scope... you don't need to set the pointer to null because it doesn't exist after the code leaves the pointer's scope. you still need to free up the memory though--just because there's nothing holding it's location doesn't mean it's not there.

    in reality, the only reason you need to set a pointer to null (or 0) is so that you don't try to access a memory location with (theoretically) nothing of use in it.
    Last edited by major_small; 07-13-2005 at 04:51 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by major_small
    did you try it? here's some sample code:
    Code:
    #include<iostream>
    
    struct test
    {
            int i;
    };
    
    int main()
    {
            {
                    test*f=new test;
                    f->i=50;
                    std::cout<<f->i<<std::endl;
                    delete test;
            }
            std::cout<<f->i<<std::endl;	//line 16
            return 0;
    }
    and this is what you get:
    Code:
    jshao@MCP ~/Programming/C++ $ g++ test.cpp -Wall -o test.exe
    test.cpp: In function `int main()':
    test.cpp:16: error: `f' undeclared (first use this function)
    test.cpp:16: error: (Each undeclared identifier is reported only once for each
       function it appears in.)
    basically, everything follows the rules of scope... you don't need to set the pointer to null because it doesn't exist after the code leaves the pointer's scope. you still need to free up the memory though--just because there's nothing holding it's location doesn't mean it's not there.
    I didnt know you could use brackets like that, very nice for testing that pointer question.

    I've seen code using backets with no keywords or anything before, I had no idea what they were for though. They localize the code eh, thats awesome. Yeah stray memory is the devil, and so is ben franklin, and foosball!

    Thanks for the info on the pointers, and on the backets.
    Warning: Have doubt in anything I post.

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

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah, the bracket thing really is pretty neat... found out about them right here on these very boards
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM