C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-16-2009, 04:29 AM   #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
bahada is offline   Reply With Quote
Old 04-16-2009, 04:38 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 04-16-2009, 04:58 AM   #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
bahada is offline   Reply With Quote
Old 04-16-2009, 05:10 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 04-16-2009, 05:15 AM   #5
Registered User
 
Join Date: Apr 2009
Posts: 10
I understand now, thank you very much...
bahada is offline   Reply With Quote
Reply

Tags
address, code, pointer

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointers Question HAssan C Programming 2 09-08-2008 10:17 AM
simple question about pointers and casting steve1_rm C++ Programming 3 03-28-2008 02:25 PM
Simple Half-Life Coding Question bengreenwood Game Programming 1 11-07-2007 02:18 PM
Binary Search Trees Part III Prelude A Brief History of Cprogramming.com 16 10-02-2004 03:00 PM
Pointers Question.....Null Pointers!!!! incognito C++ Programming 5 12-28-2001 11:13 PM


All times are GMT -6. The time now is 10:28 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22