sagar474
Full Member level 5
- Joined
- Oct 18, 2009
- Messages
- 285
- Helped
- 5
- Reputation
- 10
- Reaction score
- 5
- Trophy points
- 1,318
- Location
- India,kakinada
- Activity points
- 3,123
> 13
-108171742-1081717432
what creating the difference? the compiler or Linux operating system.
What Alex is telling you is your pointers are not necessarily pointing at anything. When you use pointers to access variables you must have the variable at the address specified in the pointer, by advancing the pointer to the next address you may well be pointing to something else which is completely irrelevant. You would be picking up an almost random value. Remember that pointers are not the variables - they are the addresses where variables are saved in memory.
It may well be that in Windows and on the occasion you ran the program it gave the result you expected. You might run it again and get a completely different result. There is no guarantee where variables will be placed in memory in either operating system so targetting an address beyond where a known variable is doesn't mean another variable will be there instead. There is no need for any opeating system to allocate consecutive addresses to variables, you were just lucky on that occasion, the linker program could just of easily located them somewhere else.
Most likely both, but do in large part to the differences in compilers/linkers, as betwixt pointed out, the results are unpredictable.
If your still curious, have the both compilers generate a ASM listing during compilation. You can really see the differences in the way each compiled program is structured.
Or better yet open the object files with a hex editor.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include<stdio.h> int main() { int b,c,d; int *p; b=1; c=2; d=3; p=&d; printf("\n address of b is %p ",&b); printf("\n address of c is %p ",&c); printf("\n address of d is %p ",&d); printf("\n \n pointer points to %p it contains %d",p++,*p); printf("\n pointer points to %p it contains %d",p++,*p); printf("\n pointer points to %p it contains %d \n",p,*p); return 0; }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include<stdio.h> int main() { int b,c,d; int *p; b=1; c=2; d=3; p=&d; /* Now these lines are commented //printf("\n address of b is %p ",&b); //printf("\n address of c is %p ",&c); //printf("\n address of d is %p ",&d); */ printf("\n \n pointer points to %p it contains %d",p++,*p); printf("\n pointer points to %p it contains %d",p++,*p); printf("\n pointer points to %p it contains %d \n",p,*p); return 0; }
You are not understanding the nature of pointers or of the C language or the operating system.
The addresses are not hard coded into the program, they are allocated at run time. If you wanted to use fixed addresses you may as well not use pointers at all but write your own addresses in code. When you execute the program it contains code that requests memory from the operating system. The operating system should in turn allocate a free block of addresses and declare them 'out of bounds' to other programs. The OS decides which addresses are available for use and these will not be the same each time because other applications or OS routines are also requesting their own address blocks as well.
Brian.
1) the memory is allocating at random each time the program is executing.
2) the memory is allocating in successive memory locations for each variable.
these are only my observations
2) the memory is allocating in successive memory locations for each variable.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?