Thread: global scope and pointers

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    Post global scope and pointers

    my code:

    Code:
    #include <stdio.h>
    
    int *pPointer;
    
    void
    my_function()
    {
     int nNumber;
     nNumber = 25;
     pPointer = &nNumber;
    }
    
    int main()
    {
     my_function();
     printf("Value of *pPointer: %d\n", *pPointer);
    }


    terminal:
    Code:
    grytskiv@ZXDSL831II:~/scope$ gcc -ansi function.c -o function && ./function
    Value of *pPointer: 25
    grytskiv@ZXDSL831II:~/scope$

    I recive value of "nNumber" variable. But "nNumber" is must deleted after complete execution function "my_function".

    Why I recive value of removed variable?
    May be this variable deleted but pice memory don't nulled, and this pice can modification other proccess or thread?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Is that your entire code?

    I ask, because what you gave us should have set off a cascade of compiler errors about missing entry points, mismatched braces and other things...

    Quote Originally Posted by grytskiv View Post
    my code:

    Code:
    #include <stdio.h>
    
    int *pPointer;
    
    void
    my_function()
    {
     int nNumber;
     nNumber = 25;
     pPointer = &nNumber;
    }
    
    int main()
    {
     my_function();
     printf("Value of *pPointer: %d\n", *pPointer);
    }


    terminal:
    Code:
    grytskiv@ZXDSL831II:~/scope$ gcc -ansi function.c -o function && ./function
    Value of *pPointer: 25
    grytskiv@ZXDSL831II:~/scope$

    I recive value of "nNumber" variable. But "nNumber" is must deleted after complete execution function "my_function".

    Why I recive value of removed variable?
    May be this variable deleted but pice memory don't nulled, and this pice can modification other proccess or thread?
    That's more like it...

    You are getting the value of nNumber simply because it hasn't been overwritten yet, and that could happen at any instant. It's called "undefined behavior" and it is to be avoided at all costs.

    C does not error trap these things... so you have to.
    Last edited by CommonTater; 03-13-2011 at 05:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of pointers not changing out of scope?
    By LaffyTaffy in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2010, 12:17 PM
  2. scope of global variable vs. static
    By chiefmonkey in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2009, 12:23 PM
  3. Global scope and passing pointer
    By Dave++ in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2007, 03:52 PM
  4. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM
  5. Vector of pointers and scope
    By krappa in forum C++ Programming
    Replies: 6
    Last Post: 11-22-2004, 02:10 PM

Tags for this Thread