Thread: simple question on pointers

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    3

    simple question on pointers

    hello,
    i have started to learn C. The code below is from Lesson 6 here on this site tutorial. It compiles and runs OK.
    After the code has been run is the pointer still pointing to the integer and pc memory not freed?
    Using Windows XP and Bloodshed Dev++ compiler.

    Thanks,
    aivars


    Code:
    #include <stdio.h>
    
    int main()
    { 
        int x;            /* A normal integer*/
        int *p;           /* A pointer to an integer ("*p" is an integer, so p
                           must be a pointer to an integer) */
    
        p = &x;           /* Read it, "assign the address of x to p" */
        scanf( "%d", &x );          /* Put a value in x, we could also use p here */
        printf( "%d\n", *p ); /* Note the use of the * to get the value */
        getchar();
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The integer is on the stack. It will be invalidated when the containing function, in this case main(), returns. The stack as a whole is allocated to your program starts, and freed when your program ends. You do not need to and should not try to free anything on the stack. Only things allocated with malloc() and calloc() need to be freed by the program explicitly.

    Also all memory is automatically freed when a program terminates in any modern OS.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    Thanks a lot,
    The reason for asking was that I modified the code to see the address of x (&x or p) is. And every time I run the code I get the same number - 2293620. Even after rebooting, after three days etc. I thought the address is assigned randomly and the number should change.
    I compiled the code on Linux (Ubuntu Hardy) and here the memory address of x changes after each run. It is is different format though and starts with - sign which also I do not understand why e.g -1075170336.
    Is it compiler who determines what memory address should be used? On linux I use gcc compiler but I understand Bloodshed Dev++ also uses kind of GCC.
    Or am I missing something big here?

    thanks,
    aivars

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    If you print out the address of x using the &#37;p (pointer) formatting option instead of %d (signed integer), you won't get a sign.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Keep in mind that freeing memory does not, in almost all cases, mean that the memory is zeroed or over-written. The memory manager simply marks the memory as available - it does not need to overwrite what's already there with nulls or anything similar as it'll be overwritten as he uses it.

    As to the address thing, that depends on what you're running at the time really. If you're using a lot of your ram, you can expect the address being different. But I assume that most of the time when you sit and program, you're running the same basic applications, so it wouldn't be strange if the address is the same.

    Perhaps someone can shed some more light on this and give a more detailed explanation.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    There's no reason to expect that, by necessity, the addresses of your functions/objects will change between runs. On virtual memory systems, each process thinks the entire address space of the machine belongs to it, so there's no problem using fixed addresses. For example, try:
    Code:
    int main(void)
    {
      printf("%p\n", (void *)main);
    }
    Not perfectly legal code, but that's OK, because on the systems we're interested in, it will work. On my system, main stays in the same place each time, which is perfectly fine, because regardless of where it resides in physical memory, it knows that this particular location will always be free in the virtual address space.

    As for why local variables keep changing on Ubuntu, that's likely because your system randomizes the stack, for security purposes. Look in /proc/sys/kernel/randomize_va_space; it's probably set to 1 or 2. If you do
    Code:
    echo 0 > /proc/sys/kernel/randomize_va_space
    and run your program a few times, odds are that your local variable will remain in the same location. Don't forget to set randomize_va_space back to its original value if you're so inclined.

    I think the most important thing to take away here is the idea that virtual memory is more than just using drive space to supplement RAM. While that is indeed an optional part of a virtual memory system, the fundamental part of VM is that each process is under the illusion that it is the only program on the system and that it has free range over memory. In reality, all the (virtual) addresses the program sees are translated into physical addresses, so it can share with everybody else. Thus the illusion is maintained.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    thanks to all who responded.
    I see I have a long way to go...

    aivars

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The reason that your address is negative on Linux is that the stack is allocated at a high memory address, which is above the 0x80000000 (2GB), in fact the number you gave is BFEA33E0, which is just under 3GB. Since the highest bit of a signed integer is the sign, anything above 2GB is "negative" if you interpret it as a signed integer.

    As suggested, using %p will show a hexadecimal, unsigned value representing the address.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by matsp View Post
    As suggested, using %p will show a hexadecimal, unsigned value representing the address.
    Strictly speaking, it would show an implementation-defined sequence of printable characters, not necessarily in that format.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers Question
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 09-08-2008, 10:17 AM
  2. simple question about pointers and casting
    By steve1_rm in forum C++ Programming
    Replies: 3
    Last Post: 03-28-2008, 02:25 PM
  3. Simple Half-Life Coding Question
    By bengreenwood in forum Game Programming
    Replies: 1
    Last Post: 11-07-2007, 02:18 PM
  4. 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
  5. Pointers Question.....Null Pointers!!!!
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2001, 11:13 PM