Thread: a simple question about pointers

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    10

    Post a simple question about pointers

    Hi, I am a freshman computer engineering student and I've just started programming in C, it may seem a bit simple but I couldn't find the answer of my question. Here is the code I write:
    Code:
    #include <stdio.h>
     int main()
     {
             int i=1, j, *ip;
             ip = &i;
             printf("%d\n", ip);
            return 0;
     }
    When I compile and run this code it gives me warning and prints random numbers like :
    -1079398400 or -1077595728 or -1080068272
    and when i change the code tho this :
    Code:
    #include <stdio.h>
     int main()
     {
             int i=1, j, *ip;
             ip = &i;
             printf("%p\n", ip);
            return 0;
     }
    it prints these (that I think they are adresses ) : 0xbfa20780 or 0xbfc0e170 or 0xbfaf6850

    Can you tell me why do I get these negative numbers and why do they change each time i run the code, and also what is the difference between these two codes?

    Thanks a lot
    bahada

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The negative numbers are because, techncially anything with the highest bit set which means for 32-bit numbers above decimally 2 billion and a bit is a negative number in computers. If you do not want "large" numbers to appear negative, then you should use %u instead of %d - %u stands for "unsigned", which means "do not treat the highest bit as sign" - then you get a number in the 3 billion range.

    The reason it changes is because Linux has a scheme to randomly move the stack around to prevent people from using known locations on the stack for stack-smashing attacks. Here is a Wiki article on why this is a meaningfull:
    Address space layout randomization - Wikipedia, the free encyclopedia

    --
    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.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    Yeah I understand now, thanks! Can I also learn why do these two codes behave differently when i change the line
    printf("%d\n", ip);
    to the line:
    printf("%p\n", ip);

    thanks.

    bahada

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    They do not behave differently in any other way than the actual representation of the value in the output. If you treat a memory address as a signed decimal number, and the value is above 2^31, then it will show as a negative number (and 0xbfffxxxx is about 1 billion/1GB into the negative region). If you use %x, it will look similar to your %p format - the difference here is that %p knows about the size of a pointer, and %x will show "an integer size" value (unless you specify %lx or %llx for long and long long values respectively - and one of these three options is MOST LIKELY right for a pointer, but the trick is to know which - %p knows that - it would even know architectures that have strange pointers and represent that in a sensible manner).

    [And for the pedants, yes, we should cast the pointer to void *].

    --
    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.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    I understand now, thank you very much...

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

Tags for this Thread