![]() |
| | #1 |
| Registered User Join Date: Apr 2009
Posts: 10
| Code: #include <stdio.h>
int main()
{
int i=1, j, *ip;
ip = &i;
printf("%d\n", ip);
return 0;
}
-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;
}
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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #5 |
| Registered User Join Date: Apr 2009
Posts: 10
| I understand now, thank you very much... |
| bahada is offline | |
![]() |
| Tags |
| address, code, pointer |
| Thread Tools | |
| Display Modes | |
|
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 |